-
Notifications
You must be signed in to change notification settings - Fork 411
/
SpriteBatch.h
157 lines (132 loc) · 7.26 KB
/
SpriteBatch.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//--------------------------------------------------------------------------------------
// File: SpriteBatch.h
//
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//
// http://go.microsoft.com/fwlink/?LinkID=615561
//--------------------------------------------------------------------------------------
#pragma once
#ifdef _GAMING_XBOX_SCARLETT
#include <d3d12_xs.h>
#elif (defined(_XBOX_ONE) && defined(_TITLE)) || defined(_GAMING_XBOX)
#include <d3d12_x.h>
#else
#ifdef USING_DIRECTX_HEADERS
#include <directx/d3d12.h>
#include <dxguids/dxguids.h>
#else
#include <d3d12.h>
#endif
#include <dxgi1_4.h>
#endif
#include <cstdint>
#include <functional>
#include <memory>
#include <DirectXMath.h>
#include <DirectXColors.h>
#include "RenderTargetState.h"
namespace DirectX
{
class ResourceUploadBatch;
inline namespace DX12
{
enum SpriteSortMode : uint32_t
{
SpriteSortMode_Deferred,
SpriteSortMode_Immediate,
SpriteSortMode_Texture,
SpriteSortMode_BackToFront,
SpriteSortMode_FrontToBack,
};
enum SpriteEffects : uint32_t
{
SpriteEffects_None = 0,
SpriteEffects_FlipHorizontally = 1,
SpriteEffects_FlipVertically = 2,
SpriteEffects_FlipBoth = SpriteEffects_FlipHorizontally | SpriteEffects_FlipVertically,
};
class SpriteBatchPipelineStateDescription
{
public:
explicit SpriteBatchPipelineStateDescription(
const RenderTargetState& renderTarget,
_In_opt_ const D3D12_BLEND_DESC* blend = nullptr,
_In_opt_ const D3D12_DEPTH_STENCIL_DESC* depthStencil = nullptr,
_In_opt_ const D3D12_RASTERIZER_DESC* rasterizer = nullptr,
_In_opt_ const D3D12_GPU_DESCRIPTOR_HANDLE* isamplerDescriptor = nullptr) noexcept
:
blendDesc(blend ? *blend : s_DefaultBlendDesc),
depthStencilDesc(depthStencil ? *depthStencil : s_DefaultDepthStencilDesc),
rasterizerDesc(rasterizer ? *rasterizer : s_DefaultRasterizerDesc),
renderTargetState(renderTarget),
samplerDescriptor{},
customRootSignature(nullptr),
customVertexShader{},
customPixelShader{}
{
if (isamplerDescriptor)
this->samplerDescriptor = *isamplerDescriptor;
}
D3D12_BLEND_DESC blendDesc;
D3D12_DEPTH_STENCIL_DESC depthStencilDesc;
D3D12_RASTERIZER_DESC rasterizerDesc;
RenderTargetState renderTargetState;
D3D12_GPU_DESCRIPTOR_HANDLE samplerDescriptor;
ID3D12RootSignature* customRootSignature;
D3D12_SHADER_BYTECODE customVertexShader;
D3D12_SHADER_BYTECODE customPixelShader;
private:
static const D3D12_BLEND_DESC s_DefaultBlendDesc;
static const D3D12_RASTERIZER_DESC s_DefaultRasterizerDesc;
static const D3D12_DEPTH_STENCIL_DESC s_DefaultDepthStencilDesc;
};
class SpriteBatch
{
public:
SpriteBatch(_In_ ID3D12Device* device, ResourceUploadBatch& upload,
const SpriteBatchPipelineStateDescription& psoDesc,
_In_opt_ const D3D12_VIEWPORT* viewport = nullptr);
SpriteBatch(SpriteBatch&&) noexcept;
SpriteBatch& operator= (SpriteBatch&&) noexcept;
SpriteBatch(SpriteBatch const&) = delete;
SpriteBatch& operator= (SpriteBatch const&) = delete;
virtual ~SpriteBatch();
// Begin/End a batch of sprite drawing operations.
void XM_CALLCONV Begin(
_In_ ID3D12GraphicsCommandList* commandList,
SpriteSortMode sortMode = SpriteSortMode_Deferred,
FXMMATRIX transformMatrix = MatrixIdentity);
void XM_CALLCONV Begin(
_In_ ID3D12GraphicsCommandList* commandList,
D3D12_GPU_DESCRIPTOR_HANDLE sampler,
SpriteSortMode sortMode = SpriteSortMode_Deferred,
FXMMATRIX transformMatrix = MatrixIdentity);
void __cdecl End();
// Draw overloads specifying position, origin and scale as XMFLOAT2.
void XM_CALLCONV Draw(D3D12_GPU_DESCRIPTOR_HANDLE textureSRV, XMUINT2 const& textureSize, XMFLOAT2 const& position, FXMVECTOR color = Colors::White);
void XM_CALLCONV Draw(D3D12_GPU_DESCRIPTOR_HANDLE textureSRV, XMUINT2 const& textureSize, XMFLOAT2 const& position, _In_opt_ RECT const* sourceRectangle, FXMVECTOR color = Colors::White, float rotation = 0, XMFLOAT2 const& origin = Float2Zero, float scale = 1, SpriteEffects effects = SpriteEffects_None, float layerDepth = 0);
void XM_CALLCONV Draw(D3D12_GPU_DESCRIPTOR_HANDLE textureSRV, XMUINT2 const& textureSize, XMFLOAT2 const& position, _In_opt_ RECT const* sourceRectangle, FXMVECTOR color, float rotation, XMFLOAT2 const& origin, XMFLOAT2 const& scale, SpriteEffects effects = SpriteEffects_None, float layerDepth = 0);
// Draw overloads specifying position, origin and scale via the first two components of an XMVECTOR.
void XM_CALLCONV Draw(D3D12_GPU_DESCRIPTOR_HANDLE textureSRV, XMUINT2 const& textureSize, FXMVECTOR position, FXMVECTOR color = Colors::White);
void XM_CALLCONV Draw(D3D12_GPU_DESCRIPTOR_HANDLE textureSRV, XMUINT2 const& textureSize, FXMVECTOR position, _In_opt_ RECT const* sourceRectangle, FXMVECTOR color = Colors::White, float rotation = 0, FXMVECTOR origin = g_XMZero, float scale = 1, SpriteEffects effects = SpriteEffects_None, float layerDepth = 0);
void XM_CALLCONV Draw(D3D12_GPU_DESCRIPTOR_HANDLE textureSRV, XMUINT2 const& textureSize, FXMVECTOR position, _In_opt_ RECT const* sourceRectangle, FXMVECTOR color, float rotation, FXMVECTOR origin, GXMVECTOR scale, SpriteEffects effects = SpriteEffects_None, float layerDepth = 0);
// Draw overloads specifying position as a RECT.
void XM_CALLCONV Draw(D3D12_GPU_DESCRIPTOR_HANDLE textureSRV, XMUINT2 const& textureSize, RECT const& destinationRectangle, FXMVECTOR color = Colors::White);
void XM_CALLCONV Draw(D3D12_GPU_DESCRIPTOR_HANDLE textureSRV, XMUINT2 const& textureSize, RECT const& destinationRectangle, _In_opt_ RECT const* sourceRectangle, FXMVECTOR color = Colors::White, float rotation = 0, XMFLOAT2 const& origin = Float2Zero, SpriteEffects effects = SpriteEffects_None, float layerDepth = 0);
// Rotation mode to be applied to the sprite transformation
#if defined(__dxgi1_2_h__) || defined(__d3d11_x_h__) || defined(__d3d12_x_h__) || defined(__XBOX_D3D12_X__)
void __cdecl SetRotation(DXGI_MODE_ROTATION mode);
DXGI_MODE_ROTATION __cdecl GetRotation() const noexcept;
#endif
// Set viewport for sprite transformation
void __cdecl SetViewport(const D3D12_VIEWPORT& viewPort);
private:
// Private implementation.
struct Impl;
std::unique_ptr<Impl> pImpl;
static const XMMATRIX MatrixIdentity;
static const XMFLOAT2 Float2Zero;
};
}
}