1 //// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
2 //// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
3 //// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
4 //// PARTICULAR PURPOSE.
6 //// Copyright (c) Microsoft Corporation. All rights reserved
10 #include "BasicMath.h"
15 namespace BasicSprites
19 // The index buffer format for feature-level 9.1 devices may only be 16 bits.
20 // With 4 vertices per sprite, this allows a maximum of (1 << 16) / 4 sprites.
21 static unsigned int MaximumCapacityCompatible = (1 << 16) / 4;
24 enum class PositionUnits
26 DIPs, // Interpret position as device-independent pixel values.
27 Pixels, // Interpret position as pixel values.
28 Normalized, // Interpret position as a fraction of the render target dimensions.
29 UniformWidth, // Interpret position as a fraction of the render target width.
30 UniformHeight // Interpret position as a fraction of the render target height.
35 DIPs, // Interpret size as device-independent pixel values.
36 Pixels, // Interpret size as pixel values.
37 Normalized // Interpret size as a multiplier of the pixel size of the sprite.
42 Alpha, // Use alpha blending (out = old * (1 - new.a) + new * new.a).
43 Additive // Use additive blending (out = old + new * new.a).
46 enum class RenderTechnique
53 struct ReplicationVertex
60 struct InstancingVertex
74 struct TextureMapElement
76 Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> srv;
82 ID3D11ShaderResourceView* textureView;
83 ID3D11BlendState1* blendState;
84 unsigned int numSprites;
92 _In_ ID3D11Device1* d3dDevice,
96 _In_ ID3D11Texture2D* texture
99 _In_ ID3D11Texture2D* texture
104 _In_ ID3D11Texture2D* texture,
105 _In_ float2 position,
106 _In_ PositionUnits positionUnits
109 _In_ ID3D11Texture2D* texture,
110 _In_ float2 position,
111 _In_ PositionUnits positionUnits,
113 _In_ SizeUnits sizeUnits
116 _In_ ID3D11Texture2D* texture,
117 _In_ float2 position,
118 _In_ PositionUnits positionUnits,
120 _In_ SizeUnits sizeUnits,
124 _In_ ID3D11Texture2D* texture,
125 _In_ float2 position,
126 _In_ PositionUnits positionUnits,
128 _In_ SizeUnits sizeUnits,
133 _In_ ID3D11Texture2D* texture,
134 _In_ float2 position,
135 _In_ PositionUnits positionUnits,
137 _In_ SizeUnits sizeUnits,
140 _In_ BlendMode blendMode
144 unsigned int MakeUnorm(float4 color);
145 float2 StandardOrigin(float2 position, PositionUnits positionUnits, float2 renderTargetSize, float dpi);
146 float2 StandardOffset(float2 size, SizeUnits sizeUnits, float2 spriteSize, float dpi);
148 Microsoft::WRL::ComPtr<ID3D11Device1> m_d3dDevice;
149 Microsoft::WRL::ComPtr<ID3D11DeviceContext1> m_d3dContext;
150 Microsoft::WRL::ComPtr<ID3D11Buffer> m_vertexBuffer;
151 Microsoft::WRL::ComPtr<ID3D11Buffer> m_instanceDataBuffer;
152 Microsoft::WRL::ComPtr<ID3D11Buffer> m_indexBuffer;
153 Microsoft::WRL::ComPtr<ID3D11BlendState1> m_blendStateAlpha;
154 Microsoft::WRL::ComPtr<ID3D11BlendState1> m_blendStateAdditive;
155 Microsoft::WRL::ComPtr<ID3D11InputLayout> m_inputLayout;
156 Microsoft::WRL::ComPtr<ID3D11VertexShader> m_vertexShader;
157 Microsoft::WRL::ComPtr<ID3D11GeometryShader> m_geometryShader;
158 Microsoft::WRL::ComPtr<ID3D11PixelShader> m_pixelShader;
159 Microsoft::WRL::ComPtr<ID3D11SamplerState> m_linearSampler;
160 Microsoft::WRL::ComPtr<ID3D11Buffer> m_renderTargetInfoCbuffer;
162 std::unique_ptr<ReplicationVertex[]> m_vertexData;
163 std::unique_ptr<InstanceData[]> m_instanceData;
164 std::map<ID3D11Texture2D*, TextureMapElement> m_textureMap;
165 std::vector<SpriteRunInfo> m_spriteRuns;
167 RenderTechnique m_technique;
168 ID3D11ShaderResourceView* m_currentTextureView;
169 ID3D11BlendState1* m_currentBlendState;
170 float2 m_renderTargetSize;
173 int m_numSpritesDrawn;