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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
 * Copyright (c) 2014-2017, NVIDIA CORPORATION.  All rights reserved.
 *
 * NVIDIA CORPORATION and its licensors retain all intellectual property
 * and proprietary rights in and to this software, related documentation
 * and any modifications thereto.  Any use, reproduction, disclosure or
 * distribution of this software and related documentation without an express
 * license agreement from NVIDIA CORPORATION is strictly prohibited.
 */

#include "imguiGraphD3D11.h"

//direct3d headers
#include <d3d11.h>

#include "../d3d/shaders/imguiVS.hlsl.h"
#include "../d3d/shaders/imguiPS.hlsl.h"

namespace
{
	template <class T>
	void inline COMRelease(T& t)
	{
		if (t) t->Release();
		t = nullptr;
	}
}

namespace
{
	ID3D11Device* m_device = nullptr;
	ID3D11DeviceContext* m_deviceContext = nullptr;
	int m_winW = 0;
	int m_winH = 0;

	uint32_t m_maxVertices = 0;

	struct Vertex
	{
		float x, y;
		float u, v;
		uint8_t rgba[4];
	};

	ID3D11RasterizerState* m_rasterizerState = nullptr;
	ID3D11SamplerState* m_samplerState = nullptr;
	ID3D11InputLayout* m_inputLayout = nullptr;
	ID3D11BlendState* m_blendState = nullptr;
	ID3D11VertexShader* m_imguiVS = nullptr;
	ID3D11PixelShader* m_imguiPS = nullptr;
	ID3D11Buffer* m_constantBuffer = nullptr;
	ID3D11Buffer* m_vertexBuffer = nullptr;
	Vertex* m_vertexBufferData = nullptr;

	struct Scissor
	{
		int beginIdx;
		int stopIdx;
		int x;
		int y;
		int width;
		int height;
	};
	Scissor m_stateScissor = {};

	ID3D11Texture2D* m_texture = nullptr;
	ID3D11ShaderResourceView* m_textureSRV = nullptr;

	Vertex m_stateVert;
	uint32_t m_stateVertIdx = 0u;

	struct Params
	{
		float projection[16];

		float padding[64 - 16];
	};
}

void imguiGraphContextDestroyD3D11()
{
	COMRelease(m_rasterizerState);
	COMRelease(m_samplerState);
	COMRelease(m_inputLayout);
	COMRelease(m_blendState);
	COMRelease(m_imguiVS);
	COMRelease(m_imguiPS);
	COMRelease(m_constantBuffer);
	COMRelease(m_vertexBuffer);
}

void imguiGraphContextInitD3D11(const ImguiGraphDesc* descIn)
{
	const auto desc = cast_to_imguiGraphDescD3D11(descIn);

	m_device = desc->device;
	m_deviceContext = desc->deviceContext;
	m_winW = desc->winW;
	m_winH = desc->winH;

	m_maxVertices = desc->maxVertices;

	// create the rastersizer state
	{
		D3D11_RASTERIZER_DESC desc = {};
		desc.FillMode = D3D11_FILL_SOLID;
		desc.CullMode = D3D11_CULL_BACK;
		desc.FrontCounterClockwise = TRUE;	// This is non-default
		desc.DepthBias = 0;
		desc.DepthBiasClamp = 0.f;
		desc.SlopeScaledDepthBias = 0.f;
		desc.DepthClipEnable = TRUE;
		desc.ScissorEnable = TRUE;		// This is non-default
		desc.MultisampleEnable = FALSE;
		desc.AntialiasedLineEnable = FALSE;

		m_device->CreateRasterizerState(&desc, &m_rasterizerState);
	}

	// create the sampler state
	{
		D3D11_SAMPLER_DESC sampler = {};
		sampler.Filter = D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT;
		sampler.AddressU = D3D11_TEXTURE_ADDRESS_BORDER;
		sampler.AddressV = D3D11_TEXTURE_ADDRESS_BORDER;
		sampler.AddressW = D3D11_TEXTURE_ADDRESS_BORDER;
		sampler.MipLODBias = 0;
		sampler.MaxAnisotropy = 0;
		sampler.ComparisonFunc = D3D11_COMPARISON_NEVER;
		//sampler.BorderColor = D3D11_BORDER_COLOR_TRANSPARENT_BLACK;
		sampler.MinLOD = 0.f;
		sampler.MaxLOD = D3D11_FLOAT32_MAX;

		m_device->CreateSamplerState(&sampler, &m_samplerState);
	}

	// create the input layout
	{
		D3D11_INPUT_ELEMENT_DESC inputElementDescs[] =
		{
			{ "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
			{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
			{ "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
		};

		m_device->CreateInputLayout(inputElementDescs, 3, g_imguiVS, sizeof(g_imguiVS), &m_inputLayout);
	}

	// create the blend state
	{
		D3D11_BLEND_DESC blendDesc = {};
		blendDesc.AlphaToCoverageEnable = false;
		blendDesc.IndependentBlendEnable = false;
		blendDesc.RenderTarget[0].BlendEnable = true;
		blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
		blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
		blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
		blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
		blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
		blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
		blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;

		m_device->CreateBlendState(&blendDesc, &m_blendState);
	}

	// create the shaders
	m_device->CreateVertexShader(g_imguiVS, sizeof(g_imguiVS), nullptr, &m_imguiVS);
	m_device->CreatePixelShader(g_imguiPS, sizeof(g_imguiPS), nullptr, &m_imguiPS);

	// create a constant buffer
	{
		Params params = {
			1.f, 0.f, 0.f, 0.f,
			0.f, 1.f, 0.f, 0.f,
			0.f, 0.f, 1.f, 0.f,
			0.f, 0.f, 0.f, 1.f
		};

		D3D11_BUFFER_DESC bufDesc;
		bufDesc.ByteWidth = sizeof(params);
		bufDesc.Usage = D3D11_USAGE_DYNAMIC;
		bufDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
		bufDesc.CPUAccessFlags  = D3D11_CPU_ACCESS_WRITE;
		bufDesc.MiscFlags = 0;

		D3D11_SUBRESOURCE_DATA data = {};
		data.pSysMem = &params;

		m_device->CreateBuffer(&bufDesc, &data, &m_constantBuffer);
	}

	// create a vertex buffer
	{
		UINT bufferSize = (UINT)(m_maxVertices) * sizeof(Vertex);

		D3D11_BUFFER_DESC bufDesc;
		bufDesc.ByteWidth = bufferSize;
		bufDesc.Usage = D3D11_USAGE_DYNAMIC;
		bufDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
		bufDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
		bufDesc.MiscFlags = 0;

		m_device->CreateBuffer(&bufDesc, nullptr, &m_vertexBuffer);
	}
}

void imguiGraphContextUpdateD3D11(const ImguiGraphDesc* descIn)
{
	const auto desc = cast_to_imguiGraphDescD3D11(descIn);

	m_device = desc->device;
	m_deviceContext = desc->deviceContext;
	m_winW = desc->winW;
	m_winH = desc->winH;
}

void imguiGraphRecordBeginD3D11()
{
	Params params = {
		2.f / float(m_winW), 0.f, 0.f, -1.f,
		0.f, 2.f / float(m_winH), 0.f, -1.f,
		0.f, 0.f, 1.f, 0.f,
		0.f, 0.f, 0.f, 1.f
	};

	ID3D11DeviceContext* context = m_deviceContext;

	// update constant buffer
	{
		D3D11_MAPPED_SUBRESOURCE mappedResource = {};
		if (S_OK == context->Map(m_constantBuffer, 0u, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource))
		{
			memcpy(mappedResource.pData, &params, sizeof(Params));

			context->Unmap(m_constantBuffer, 0u);
		}
	}

	// clear state
	m_stateVert = Vertex{ 0.f, 0.f, -1.f, -1.f, 0,0,0,0 };
	m_stateVertIdx = 0u;

	m_stateScissor = Scissor { 0, 0, 0, 0, m_winW, m_winH };

	// configure for triangle renderering
	context->VSSetShader(m_imguiVS, nullptr, 0u);
	context->GSSetShader(nullptr, nullptr, 0u);
	context->PSSetShader(m_imguiPS, nullptr, 0u);

	context->IASetInputLayout(m_inputLayout);
	context->OMSetBlendState(m_blendState, nullptr, 0xFFFFFFFF);
	context->PSSetSamplers(0, 1, &m_samplerState);

	context->VSSetConstantBuffers(0, 1, &m_constantBuffer);

	context->PSSetShaderResources(0, 1, &m_textureSRV);

	context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

	context->RSSetState(m_rasterizerState);

	// trigger allocation of new vertex buffer as needed
	{
		D3D11_MAPPED_SUBRESOURCE mappedResource = {};
		if (S_OK == context->Map(m_vertexBuffer, 0u, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource))
		{
			context->Unmap(m_vertexBuffer, 0u);
		}
	}

	UINT vertexStride = sizeof(Vertex);
	UINT offset = 0u;
	context->IASetVertexBuffers(0, 1, &m_vertexBuffer, &vertexStride, &offset);

	// map allocated vertex buffer
	{
		D3D11_MAPPED_SUBRESOURCE mappedResource = {};
		if (S_OK == context->Map(m_vertexBuffer, 0u, D3D11_MAP_WRITE_NO_OVERWRITE, 0, &mappedResource))
		{
			m_vertexBufferData = (Vertex*)mappedResource.pData;
		}
		else
		{
			m_vertexBufferData = nullptr;
		}
	}
}

static void imguiGraphFlush()
{
	ID3D11DeviceContext* context = m_deviceContext;

	// unmap vertex buffer
	context->Unmap(m_vertexBuffer, 0u);

	Scissor& p = m_stateScissor;
	if (p.beginIdx < p.stopIdx)
	{
		int winH = m_winH;
		D3D11_RECT rect;
		rect.left = p.x;
		rect.right = p.x + p.width;
		rect.top = (winH) - (p.y + p.height);
		rect.bottom = (winH) - (p.y);
		context->RSSetScissorRects(1, &rect);

		UINT vertexCount = (p.stopIdx - p.beginIdx);
		UINT startIndex = p.beginIdx;
		context->DrawInstanced(vertexCount, 1, startIndex, 0);
	}

	// map allocated vertex buffer
	{
		D3D11_MAPPED_SUBRESOURCE mappedResource = {};
		if (S_OK == context->Map(m_vertexBuffer, 0u, D3D11_MAP_WRITE_NO_OVERWRITE, 0, &mappedResource))
		{
			m_vertexBufferData = (Vertex*)mappedResource.pData;
		}
		else
		{
			m_vertexBufferData = nullptr;
		}
	}
}

void imguiGraphRecordEndD3D11()
{
	ID3D11DeviceContext* context = m_deviceContext;

	context->OMSetBlendState(nullptr,nullptr,0xFFFFFFFF);

	// unmap vertex buffer
	context->Unmap(m_vertexBuffer, 0u);

	// restore scissor
	Scissor& p = m_stateScissor;
	int winH = m_winH;
	D3D11_RECT rect;
	rect.left = p.x;
	rect.right = p.x + p.width;
	rect.top = (winH) - (p.y + p.height);
	rect.bottom = (winH) - (p.y);
	context->RSSetScissorRects(1, &rect);

	context->RSSetState(nullptr);
}

void imguiGraphEnableScissorD3D11(int x, int y, int width, int height)
{
	// mark end of last region
	m_stateScissor.stopIdx = m_stateVertIdx;

	imguiGraphFlush();

	m_stateScissor.beginIdx = m_stateVertIdx;
	m_stateScissor.stopIdx = m_stateVertIdx;
	m_stateScissor.x = x;
	m_stateScissor.y = y;
	m_stateScissor.width = width;
	m_stateScissor.height = height;
}

void imguiGraphDisableScissorD3D11()
{
	if (m_stateVertIdx == 0) return;

	// mark end of last region
	m_stateScissor.stopIdx = m_stateVertIdx;

	imguiGraphFlush();

	m_stateScissor.beginIdx = m_stateVertIdx;
	m_stateScissor.stopIdx = m_stateVertIdx;
	m_stateScissor.x = 0;
	m_stateScissor.y = 0;
	m_stateScissor.width = m_winW;
	m_stateScissor.height = m_winH;
}

void imguiGraphVertex2fD3D11(float x, float y)
{
	float v[2] = { x,y };
	imguiGraphVertex2fvD3D11(v);
}

void imguiGraphVertex2fvD3D11(const float* v)
{
	// update state
	m_stateVert.x = v[0];
	m_stateVert.y = v[1];

	Vertex* vdata = m_vertexBufferData;

	// push vertex
	if ((m_stateVertIdx) < m_maxVertices)
	{
		vdata[m_stateVertIdx++] = m_stateVert;
	}
}

void imguiGraphTexCoord2fD3D11(float u, float v)
{
	m_stateVert.u = u;
	m_stateVert.v = v;
}

void imguiGraphColor4ubD3D11(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
{
	m_stateVert.rgba[0] = red;
	m_stateVert.rgba[1] = green;
	m_stateVert.rgba[2] = blue;
	m_stateVert.rgba[3] = alpha;
}

void imguiGraphColor4ubvD3D11(const uint8_t* v)
{
	m_stateVert.rgba[0] = v[0];
	m_stateVert.rgba[1] = v[1];
	m_stateVert.rgba[2] = v[2];
	m_stateVert.rgba[3] = v[3];
}

void imguiGraphFontTextureEnableD3D11()
{

}

void imguiGraphFontTextureDisableD3D11()
{
	m_stateVert.u = -1.f;
	m_stateVert.v = -1.f;
}

void imguiGraphFontTextureInitD3D11(unsigned char* data)
{
	ID3D11DeviceContext* context = m_deviceContext;

	// create texture
	{
		UINT width = 512;
		UINT height = 512;

		D3D11_TEXTURE2D_DESC texDesc = {};
		texDesc.Width = width;
		texDesc.Height = height;
		texDesc.MipLevels = 1;
		texDesc.ArraySize = 1;
		texDesc.Format = DXGI_FORMAT_R8_UNORM;
		texDesc.SampleDesc.Count = 1;
		texDesc.SampleDesc.Quality = 0u;
		texDesc.Usage = D3D11_USAGE_IMMUTABLE;
		texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
		texDesc.CPUAccessFlags = 0;
		texDesc.MiscFlags = 0;

		D3D11_SUBRESOURCE_DATA initData = {};
		initData.pSysMem = data;
		initData.SysMemPitch = width;

		if (m_device->CreateTexture2D(&texDesc, &initData, &m_texture))
		{
			return;
		}

		if (m_device->CreateShaderResourceView(m_texture, nullptr, &m_textureSRV))
		{
			return;
		}
	}

}

void imguiGraphFontTextureReleaseD3D11()
{
	COMRelease(m_texture);
	COMRelease(m_textureSRV);
}