34 #include <d3dcompiler.h>
36 #pragma comment(lib, "d3dcompiler") // Automatically link with d3dcompiler.lib as we are using D3DCompile() below.
66 memset(&vp, 0,
sizeof(D3D10_VIEWPORT));
71 vp.TopLeftX = vp.TopLeftY = 0;
72 ctx->RSSetViewports(1, &vp);
79 ctx->IASetIndexBuffer(
g_pIB,
sizeof(
ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT, 0);
80 ctx->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
85 ctx->GSSetShader(
NULL);
88 const float blend_factor[4] = { 0.f, 0.f, 0.f, 0.f };
89 ctx->OMSetBlendState(
g_pBlendState, blend_factor, 0xffffffff);
105 if (!
g_pVB || g_VertexBufferSize < draw_data->TotalVtxCount)
109 D3D10_BUFFER_DESC
desc;
110 memset(&
desc, 0,
sizeof(D3D10_BUFFER_DESC));
111 desc.Usage = D3D10_USAGE_DYNAMIC;
113 desc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
114 desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
120 if (!
g_pIB || g_IndexBufferSize < draw_data->TotalIdxCount)
124 D3D10_BUFFER_DESC
desc;
125 memset(&
desc, 0,
sizeof(D3D10_BUFFER_DESC));
126 desc.Usage = D3D10_USAGE_DYNAMIC;
128 desc.BindFlags = D3D10_BIND_INDEX_BUFFER;
129 desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
137 g_pVB->Map(D3D10_MAP_WRITE_DISCARD, 0, (
void**)&vtx_dst);
138 g_pIB->Map(D3D10_MAP_WRITE_DISCARD, 0, (
void**)&idx_dst);
153 void* mapped_resource;
163 { 2.0f/(R-L), 0.0
f, 0.0
f, 0.0
f },
164 { 0.0f, 2.0f/(
T-B), 0.0
f, 0.0
f },
165 { 0.0f, 0.0f, 0.5f, 0.0f },
166 { (R+L)/(L-R), (
T+B)/(B-
T), 0.5f, 1.0f },
168 memcpy(&constant_buffer->
mvp, mvp,
sizeof(mvp));
173 struct BACKUP_DX10_STATE
175 UINT ScissorRectsCount, ViewportsCount;
176 D3D10_RECT ScissorRects[D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
177 D3D10_VIEWPORT Viewports[D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
178 ID3D10RasterizerState* RS;
179 ID3D10BlendState* BlendState;
180 FLOAT BlendFactor[4];
183 ID3D10DepthStencilState* DepthStencilState;
184 ID3D10ShaderResourceView* PSShaderResource;
185 ID3D10SamplerState* PSSampler;
186 ID3D10PixelShader* PS;
187 ID3D10VertexShader* VS;
188 ID3D10GeometryShader* GS;
189 D3D10_PRIMITIVE_TOPOLOGY PrimitiveTopology;
190 ID3D10Buffer* IndexBuffer, *VertexBuffer, *VSConstantBuffer;
191 UINT IndexBufferOffset, VertexBufferStride, VertexBufferOffset;
192 DXGI_FORMAT IndexBufferFormat;
193 ID3D10InputLayout* InputLayout;
195 BACKUP_DX10_STATE old;
196 old.ScissorRectsCount = old.ViewportsCount = D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE;
197 ctx->RSGetScissorRects(&old.ScissorRectsCount, old.ScissorRects);
198 ctx->RSGetViewports(&old.ViewportsCount, old.Viewports);
199 ctx->RSGetState(&old.RS);
200 ctx->OMGetBlendState(&old.BlendState, old.BlendFactor, &old.SampleMask);
201 ctx->OMGetDepthStencilState(&old.DepthStencilState, &old.StencilRef);
202 ctx->PSGetShaderResources(0, 1, &old.PSShaderResource);
203 ctx->PSGetSamplers(0, 1, &old.PSSampler);
204 ctx->PSGetShader(&old.PS);
205 ctx->VSGetShader(&old.VS);
206 ctx->VSGetConstantBuffers(0, 1, &old.VSConstantBuffer);
207 ctx->GSGetShader(&old.GS);
208 ctx->IAGetPrimitiveTopology(&old.PrimitiveTopology);
209 ctx->IAGetIndexBuffer(&old.IndexBuffer, &old.IndexBufferFormat, &old.IndexBufferOffset);
210 ctx->IAGetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset);
211 ctx->IAGetInputLayout(&old.InputLayout);
218 int global_vtx_offset = 0;
219 int global_idx_offset = 0;
224 for (
int cmd_i = 0; cmd_i < cmd_list->
CmdBuffer.
Size; cmd_i++)
240 ctx->RSSetScissorRects(1, &
r);
243 ID3D10ShaderResourceView* texture_srv = (ID3D10ShaderResourceView*)pcmd->
TextureId;
244 ctx->PSSetShaderResources(0, 1, &texture_srv);
253 ctx->RSSetScissorRects(old.ScissorRectsCount, old.ScissorRects);
254 ctx->RSSetViewports(old.ViewportsCount, old.Viewports);
255 ctx->RSSetState(old.RS);
if (old.RS) old.RS->Release();
256 ctx->OMSetBlendState(old.BlendState, old.BlendFactor, old.SampleMask);
if (old.BlendState) old.BlendState->Release();
257 ctx->OMSetDepthStencilState(old.DepthStencilState, old.StencilRef);
if (old.DepthStencilState) old.DepthStencilState->Release();
258 ctx->PSSetShaderResources(0, 1, &old.PSShaderResource);
if (old.PSShaderResource) old.PSShaderResource->Release();
259 ctx->PSSetSamplers(0, 1, &old.PSSampler);
if (old.PSSampler) old.PSSampler->Release();
260 ctx->PSSetShader(old.PS);
if (old.PS) old.PS->Release();
261 ctx->VSSetShader(old.VS);
if (old.VS) old.VS->Release();
262 ctx->GSSetShader(old.GS);
if (old.GS) old.GS->Release();
263 ctx->VSSetConstantBuffers(0, 1, &old.VSConstantBuffer);
if (old.VSConstantBuffer) old.VSConstantBuffer->Release();
264 ctx->IASetPrimitiveTopology(old.PrimitiveTopology);
265 ctx->IASetIndexBuffer(old.IndexBuffer, old.IndexBufferFormat, old.IndexBufferOffset);
if (old.IndexBuffer) old.IndexBuffer->Release();
266 ctx->IASetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset);
if (old.VertexBuffer) old.VertexBuffer->Release();
267 ctx->IASetInputLayout(old.InputLayout);
if (old.InputLayout) old.InputLayout->Release();
280 D3D10_TEXTURE2D_DESC
desc;
286 desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
287 desc.SampleDesc.Count = 1;
288 desc.Usage = D3D10_USAGE_DEFAULT;
289 desc.BindFlags = D3D10_BIND_SHADER_RESOURCE;
290 desc.CPUAccessFlags = 0;
292 ID3D10Texture2D *pTexture =
NULL;
293 D3D10_SUBRESOURCE_DATA subResource;
294 subResource.pSysMem =
pixels;
295 subResource.SysMemPitch =
desc.Width * 4;
296 subResource.SysMemSlicePitch = 0;
300 D3D10_SHADER_RESOURCE_VIEW_DESC srv_desc;
301 ZeroMemory(&srv_desc,
sizeof(srv_desc));
302 srv_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
303 srv_desc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
304 srv_desc.Texture2D.MipLevels =
desc.MipLevels;
305 srv_desc.Texture2D.MostDetailedMip = 0;
315 D3D10_SAMPLER_DESC
desc;
317 desc.Filter = D3D10_FILTER_MIN_MAG_MIP_LINEAR;
318 desc.AddressU = D3D10_TEXTURE_ADDRESS_WRAP;
319 desc.AddressV = D3D10_TEXTURE_ADDRESS_WRAP;
320 desc.AddressW = D3D10_TEXTURE_ADDRESS_WRAP;
321 desc.MipLODBias = 0.f;
322 desc.ComparisonFunc = D3D10_COMPARISON_ALWAYS;
344 static const char* vertexShader =
345 "cbuffer vertexBuffer : register(b0) \
347 float4x4 ProjectionMatrix; \
351 float2 pos : POSITION;\
352 float4 col : COLOR0;\
353 float2 uv : TEXCOORD0;\
358 float4 pos : SV_POSITION;\
359 float4 col : COLOR0;\
360 float2 uv : TEXCOORD0;\
363 PS_INPUT main(VS_INPUT input)\
366 output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\
367 output.col = input.col;\
368 output.uv = input.uv;\
372 D3DCompile(vertexShader, strlen(vertexShader),
NULL,
NULL,
NULL,
"main",
"vs_4_0", 0, 0, &
g_pVertexShaderBlob,
NULL);
379 D3D10_INPUT_ELEMENT_DESC local_layout[] =
381 {
"POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((
ImDrawVert*)0)->pos), D3D10_INPUT_PER_VERTEX_DATA, 0 },
382 {
"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((
ImDrawVert*)0)->uv), D3D10_INPUT_PER_VERTEX_DATA, 0 },
383 {
"COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (size_t)(&((
ImDrawVert*)0)->col), D3D10_INPUT_PER_VERTEX_DATA, 0 },
390 D3D10_BUFFER_DESC
desc;
392 desc.Usage = D3D10_USAGE_DYNAMIC;
393 desc.BindFlags = D3D10_BIND_CONSTANT_BUFFER;
394 desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
402 static const char* pixelShader =
405 float4 pos : SV_POSITION;\
406 float4 col : COLOR0;\
407 float2 uv : TEXCOORD0;\
412 float4 main(PS_INPUT input) : SV_Target\
414 float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \
418 D3DCompile(pixelShader, strlen(pixelShader),
NULL,
NULL,
NULL,
"main",
"ps_4_0", 0, 0, &
g_pPixelShaderBlob,
NULL);
427 D3D10_BLEND_DESC
desc;
429 desc.AlphaToCoverageEnable =
false;
430 desc.BlendEnable[0] =
true;
431 desc.SrcBlend = D3D10_BLEND_SRC_ALPHA;
432 desc.DestBlend = D3D10_BLEND_INV_SRC_ALPHA;
433 desc.BlendOp = D3D10_BLEND_OP_ADD;
434 desc.SrcBlendAlpha = D3D10_BLEND_INV_SRC_ALPHA;
435 desc.DestBlendAlpha = D3D10_BLEND_ZERO;
436 desc.BlendOpAlpha = D3D10_BLEND_OP_ADD;
437 desc.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL;
443 D3D10_RASTERIZER_DESC
desc;
445 desc.FillMode = D3D10_FILL_SOLID;
446 desc.CullMode = D3D10_CULL_NONE;
447 desc.ScissorEnable =
true;
448 desc.DepthClipEnable =
true;
454 D3D10_DEPTH_STENCIL_DESC
desc;
456 desc.DepthEnable =
false;
457 desc.DepthWriteMask = D3D10_DEPTH_WRITE_MASK_ALL;
458 desc.DepthFunc = D3D10_COMPARISON_ALWAYS;
459 desc.StencilEnable =
false;
460 desc.FrontFace.StencilFailOp =
desc.FrontFace.StencilDepthFailOp =
desc.FrontFace.StencilPassOp = D3D10_STENCIL_OP_KEEP;
461 desc.FrontFace.StencilFunc = D3D10_COMPARISON_ALWAYS;
500 IDXGIDevice* pDXGIDevice =
NULL;
501 IDXGIAdapter* pDXGIAdapter =
NULL;
502 IDXGIFactory* pFactory =
NULL;
504 if (device->QueryInterface(IID_PPV_ARGS(&pDXGIDevice)) == S_OK)
505 if (pDXGIDevice->GetParent(IID_PPV_ARGS(&pDXGIAdapter)) == S_OK)
506 if (pDXGIAdapter->GetParent(IID_PPV_ARGS(&pFactory)) == S_OK)
511 if (pDXGIDevice) pDXGIDevice->Release();
512 if (pDXGIAdapter) pDXGIAdapter->Release();