34 #include <d3dcompiler.h>
36 #pragma comment(lib, "d3dcompiler") // Automatically link with d3dcompiler.lib as we are using D3DCompile() below.
67 memset(&vp, 0,
sizeof(D3D11_VIEWPORT));
72 vp.TopLeftX = vp.TopLeftY = 0;
73 ctx->RSSetViewports(1, &vp);
80 ctx->IASetIndexBuffer(
g_pIB,
sizeof(
ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT, 0);
81 ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
92 const float blend_factor[4] = { 0.f, 0.f, 0.f, 0.f };
93 ctx->OMSetBlendState(
g_pBlendState, blend_factor, 0xffffffff);
109 if (!
g_pVB || g_VertexBufferSize < draw_data->TotalVtxCount)
113 D3D11_BUFFER_DESC
desc;
114 memset(&
desc, 0,
sizeof(D3D11_BUFFER_DESC));
115 desc.Usage = D3D11_USAGE_DYNAMIC;
117 desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
118 desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
123 if (!
g_pIB || g_IndexBufferSize < draw_data->TotalIdxCount)
127 D3D11_BUFFER_DESC
desc;
128 memset(&
desc, 0,
sizeof(D3D11_BUFFER_DESC));
129 desc.Usage = D3D11_USAGE_DYNAMIC;
131 desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
132 desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
138 D3D11_MAPPED_SUBRESOURCE vtx_resource, idx_resource;
139 if (ctx->Map(
g_pVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &vtx_resource) != S_OK)
141 if (ctx->Map(
g_pIB, 0, D3D11_MAP_WRITE_DISCARD, 0, &idx_resource) != S_OK)
153 ctx->Unmap(
g_pVB, 0);
154 ctx->Unmap(
g_pIB, 0);
159 D3D11_MAPPED_SUBRESOURCE mapped_resource;
169 { 2.0f/(R-L), 0.0f, 0.0f, 0.0f },
170 { 0.0f, 2.0f/(
T-B), 0.0
f, 0.0
f },
171 { 0.0f, 0.0f, 0.5f, 0.0f },
172 { (R+L)/(L-R), (
T+B)/(B-
T), 0.5f, 1.0f },
174 memcpy(&constant_buffer->mvp, mvp,
sizeof(mvp));
179 struct BACKUP_DX11_STATE
181 UINT ScissorRectsCount, ViewportsCount;
182 D3D11_RECT ScissorRects[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
183 D3D11_VIEWPORT Viewports[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
184 ID3D11RasterizerState* RS;
185 ID3D11BlendState* BlendState;
186 FLOAT BlendFactor[4];
189 ID3D11DepthStencilState* DepthStencilState;
190 ID3D11ShaderResourceView* PSShaderResource;
191 ID3D11SamplerState* PSSampler;
192 ID3D11PixelShader* PS;
193 ID3D11VertexShader* VS;
194 ID3D11GeometryShader* GS;
195 UINT PSInstancesCount, VSInstancesCount, GSInstancesCount;
196 ID3D11ClassInstance *PSInstances[256], *VSInstances[256], *GSInstances[256];
197 D3D11_PRIMITIVE_TOPOLOGY PrimitiveTopology;
198 ID3D11Buffer* IndexBuffer, *VertexBuffer, *VSConstantBuffer;
199 UINT IndexBufferOffset, VertexBufferStride, VertexBufferOffset;
200 DXGI_FORMAT IndexBufferFormat;
201 ID3D11InputLayout* InputLayout;
203 BACKUP_DX11_STATE old;
204 old.ScissorRectsCount = old.ViewportsCount = D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE;
205 ctx->RSGetScissorRects(&old.ScissorRectsCount, old.ScissorRects);
206 ctx->RSGetViewports(&old.ViewportsCount, old.Viewports);
207 ctx->RSGetState(&old.RS);
208 ctx->OMGetBlendState(&old.BlendState, old.BlendFactor, &old.SampleMask);
209 ctx->OMGetDepthStencilState(&old.DepthStencilState, &old.StencilRef);
210 ctx->PSGetShaderResources(0, 1, &old.PSShaderResource);
211 ctx->PSGetSamplers(0, 1, &old.PSSampler);
212 old.PSInstancesCount = old.VSInstancesCount = old.GSInstancesCount = 256;
213 ctx->PSGetShader(&old.PS, old.PSInstances, &old.PSInstancesCount);
214 ctx->VSGetShader(&old.VS, old.VSInstances, &old.VSInstancesCount);
215 ctx->VSGetConstantBuffers(0, 1, &old.VSConstantBuffer);
216 ctx->GSGetShader(&old.GS, old.GSInstances, &old.GSInstancesCount);
218 ctx->IAGetPrimitiveTopology(&old.PrimitiveTopology);
219 ctx->IAGetIndexBuffer(&old.IndexBuffer, &old.IndexBufferFormat, &old.IndexBufferOffset);
220 ctx->IAGetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset);
221 ctx->IAGetInputLayout(&old.InputLayout);
228 int global_idx_offset = 0;
229 int global_vtx_offset = 0;
230 ImVec2 clip_off = draw_data->DisplayPos;
231 for (
int n = 0;
n < draw_data->CmdListsCount;
n++)
233 const ImDrawList* cmd_list = draw_data->CmdLists[
n];
234 for (
int cmd_i = 0; cmd_i < cmd_list->
CmdBuffer.
Size; cmd_i++)
250 ctx->RSSetScissorRects(1, &
r);
253 ID3D11ShaderResourceView* texture_srv = (ID3D11ShaderResourceView*)pcmd->
TextureId;
254 ctx->PSSetShaderResources(0, 1, &texture_srv);
263 ctx->RSSetScissorRects(old.ScissorRectsCount, old.ScissorRects);
264 ctx->RSSetViewports(old.ViewportsCount, old.Viewports);
265 ctx->RSSetState(old.RS);
if (old.RS) old.RS->Release();
266 ctx->OMSetBlendState(old.BlendState, old.BlendFactor, old.SampleMask);
if (old.BlendState) old.BlendState->Release();
267 ctx->OMSetDepthStencilState(old.DepthStencilState, old.StencilRef);
if (old.DepthStencilState) old.DepthStencilState->Release();
268 ctx->PSSetShaderResources(0, 1, &old.PSShaderResource);
if (old.PSShaderResource) old.PSShaderResource->Release();
269 ctx->PSSetSamplers(0, 1, &old.PSSampler);
if (old.PSSampler) old.PSSampler->Release();
270 ctx->PSSetShader(old.PS, old.PSInstances, old.PSInstancesCount);
if (old.PS) old.PS->Release();
271 for (UINT
i = 0;
i < old.PSInstancesCount;
i++)
if (old.PSInstances[
i]) old.PSInstances[
i]->Release();
272 ctx->VSSetShader(old.VS, old.VSInstances, old.VSInstancesCount);
if (old.VS) old.VS->Release();
273 ctx->VSSetConstantBuffers(0, 1, &old.VSConstantBuffer);
if (old.VSConstantBuffer) old.VSConstantBuffer->Release();
274 ctx->GSSetShader(old.GS, old.GSInstances, old.GSInstancesCount);
if (old.GS) old.GS->Release();
275 for (UINT
i = 0;
i < old.VSInstancesCount;
i++)
if (old.VSInstances[
i]) old.VSInstances[
i]->Release();
276 ctx->IASetPrimitiveTopology(old.PrimitiveTopology);
277 ctx->IASetIndexBuffer(old.IndexBuffer, old.IndexBufferFormat, old.IndexBufferOffset);
if (old.IndexBuffer) old.IndexBuffer->Release();
278 ctx->IASetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset);
if (old.VertexBuffer) old.VertexBuffer->Release();
279 ctx->IASetInputLayout(old.InputLayout);
if (old.InputLayout) old.InputLayout->Release();
292 D3D11_TEXTURE2D_DESC
desc;
298 desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
299 desc.SampleDesc.Count = 1;
300 desc.Usage = D3D11_USAGE_DEFAULT;
301 desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
302 desc.CPUAccessFlags = 0;
304 ID3D11Texture2D *pTexture =
NULL;
305 D3D11_SUBRESOURCE_DATA subResource;
306 subResource.pSysMem =
pixels;
307 subResource.SysMemPitch =
desc.Width * 4;
308 subResource.SysMemSlicePitch = 0;
312 D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
313 ZeroMemory(&srvDesc,
sizeof(srvDesc));
314 srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
315 srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
316 srvDesc.Texture2D.MipLevels =
desc.MipLevels;
317 srvDesc.Texture2D.MostDetailedMip = 0;
327 D3D11_SAMPLER_DESC
desc;
329 desc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
330 desc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
331 desc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
332 desc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
333 desc.MipLODBias = 0.f;
334 desc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
356 static const char* vertexShader =
357 "cbuffer vertexBuffer : register(b0) \
359 float4x4 ProjectionMatrix; \
363 float2 pos : POSITION;\
364 float4 col : COLOR0;\
365 float2 uv : TEXCOORD0;\
370 float4 pos : SV_POSITION;\
371 float4 col : COLOR0;\
372 float2 uv : TEXCOORD0;\
375 PS_INPUT main(VS_INPUT input)\
378 output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\
379 output.col = input.col;\
380 output.uv = input.uv;\
384 D3DCompile(vertexShader, strlen(vertexShader),
NULL,
NULL,
NULL,
"main",
"vs_4_0", 0, 0, &
g_pVertexShaderBlob,
NULL);
391 D3D11_INPUT_ELEMENT_DESC local_layout[] =
393 {
"POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((
ImDrawVert*)0)->pos), D3D11_INPUT_PER_VERTEX_DATA, 0 },
394 {
"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((
ImDrawVert*)0)->uv), D3D11_INPUT_PER_VERTEX_DATA, 0 },
395 {
"COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (size_t)(&((
ImDrawVert*)0)->col), D3D11_INPUT_PER_VERTEX_DATA, 0 },
402 D3D11_BUFFER_DESC
desc;
404 desc.Usage = D3D11_USAGE_DYNAMIC;
405 desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
406 desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
414 static const char* pixelShader =
417 float4 pos : SV_POSITION;\
418 float4 col : COLOR0;\
419 float2 uv : TEXCOORD0;\
424 float4 main(PS_INPUT input) : SV_Target\
426 float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \
430 D3DCompile(pixelShader, strlen(pixelShader),
NULL,
NULL,
NULL,
"main",
"ps_4_0", 0, 0, &
g_pPixelShaderBlob,
NULL);
439 D3D11_BLEND_DESC
desc;
441 desc.AlphaToCoverageEnable =
false;
442 desc.RenderTarget[0].BlendEnable =
true;
443 desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
444 desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
445 desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
446 desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
447 desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
448 desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
449 desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
455 D3D11_RASTERIZER_DESC
desc;
457 desc.FillMode = D3D11_FILL_SOLID;
458 desc.CullMode = D3D11_CULL_NONE;
459 desc.ScissorEnable =
true;
460 desc.DepthClipEnable =
true;
466 D3D11_DEPTH_STENCIL_DESC
desc;
468 desc.DepthEnable =
false;
469 desc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
470 desc.DepthFunc = D3D11_COMPARISON_ALWAYS;
471 desc.StencilEnable =
false;
472 desc.FrontFace.StencilFailOp =
desc.FrontFace.StencilDepthFailOp =
desc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
473 desc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
512 IDXGIDevice* pDXGIDevice =
NULL;
513 IDXGIAdapter* pDXGIAdapter =
NULL;
514 IDXGIFactory* pFactory =
NULL;
516 if (device->QueryInterface(IID_PPV_ARGS(&pDXGIDevice)) == S_OK)
517 if (pDXGIDevice->GetParent(IID_PPV_ARGS(&pDXGIAdapter)) == S_OK)
518 if (pDXGIAdapter->GetParent(IID_PPV_ARGS(&pFactory)) == S_OK)
524 if (pDXGIDevice) pDXGIDevice->Release();
525 if (pDXGIAdapter) pDXGIAdapter->Release();