aboutsummaryrefslogtreecommitdiff
path: root/demo/DemoAppD3D11/meshD3D11.cpp
blob: 2b119bbafeaf18acc84de4aad24e4d64d6e74291 (plain) (blame)
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
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
/*
 * 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.
 */

//direct3d headers
#include <d3d11.h>

// include the Direct3D Library file
#pragma comment (lib, "d3d11.lib")

#include <math.h>

#include "meshD3D11.h"

#include "meshVS.hlsl.h"
#include "meshPS.hlsl.h"

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

struct MeshContext
{
	ID3D11Device* m_device = nullptr;
	ID3D11DeviceContext* m_deviceContext = nullptr;

	ID3D11InputLayout* m_inputLayout = nullptr;
	ID3D11VertexShader* m_meshVS = nullptr;
	ID3D11PixelShader* m_meshPS = nullptr;
	ID3D11Buffer* m_constantBuffer = nullptr;
	ID3D11RasterizerState* m_rasterizerStateRH = nullptr;

	MeshContext() {}
	~MeshContext()
	{
		COMRelease(m_inputLayout);
		COMRelease(m_meshVS);
		COMRelease(m_meshPS);
		COMRelease(m_constantBuffer);
		COMRelease(m_rasterizerStateRH);
	}
};

struct MeshIndexBuffer
{
	ID3D11Buffer* m_buffer = nullptr;
	MeshUint m_numElements = 0u;

	MeshIndexBuffer() {}
	~MeshIndexBuffer()
	{
		COMRelease(m_buffer);
	}
};

struct MeshVertexBuffer
{
	ID3D11Buffer* m_buffer = nullptr;
	MeshUint m_numElements = 0u;

	MeshVertexBuffer() {}
	~MeshVertexBuffer()
	{
		COMRelease(m_buffer);
	}
};

MeshContext* MeshContextCreate(const MeshContextDesc* desc)
{
	MeshContext* context = new MeshContext;

	context->m_device = desc->device;
	context->m_deviceContext = desc->context;

	// create the input layout
	{
		D3D11_INPUT_ELEMENT_DESC inputElementDescs[] =
		{
			{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
			{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }
		};

		context->m_device->CreateInputLayout(inputElementDescs, 2, g_meshVS, sizeof(g_meshVS), &context->m_inputLayout);
	}

	// create the shaders
	context->m_device->CreateVertexShader(g_meshVS, sizeof(g_meshVS), nullptr, &context->m_meshVS);
	context->m_device->CreatePixelShader(g_meshPS, sizeof(g_meshPS), nullptr, &context->m_meshPS);

	// create a constant buffer
	{
		D3D11_BUFFER_DESC bufDesc;
		bufDesc.ByteWidth = 64 * sizeof(float);
		bufDesc.Usage = D3D11_USAGE_DYNAMIC;
		bufDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
		bufDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
		bufDesc.MiscFlags = 0;

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

	// 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 = FALSE;
		desc.MultisampleEnable = FALSE;
		desc.AntialiasedLineEnable = FALSE;

		context->m_device->CreateRasterizerState(&desc, &context->m_rasterizerStateRH);
	}

	return context;
}

void MeshContextUpdate(MeshContext* context, const MeshContextDesc* desc)
{
	context->m_device = desc->device;
	context->m_deviceContext = desc->context;
}

void MeshContextRelease(MeshContext* context)
{
	if (context == nullptr) return;

	delete context;
}

MeshIndexBuffer* MeshIndexBufferCreate(MeshContext* context, MeshUint* indices, MeshUint numIndices)
{
	MeshIndexBuffer* buffer = new MeshIndexBuffer;

	buffer->m_numElements = numIndices;
	// create an index buffer
	{
		UINT bufferSize = (UINT)(numIndices) * sizeof(UINT);

		D3D11_BUFFER_DESC bufDesc;
		bufDesc.ByteWidth = bufferSize;
		bufDesc.Usage = D3D11_USAGE_IMMUTABLE;
		bufDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
		bufDesc.CPUAccessFlags = DXGI_CPU_ACCESS_NONE;
		bufDesc.MiscFlags = 0;

		D3D11_SUBRESOURCE_DATA data = {};
		data.pSysMem = indices;

		context->m_device->CreateBuffer(&bufDesc, &data, &buffer->m_buffer);
	}

	return buffer;
}

void MeshIndexBufferRelease(MeshIndexBuffer* buffer)
{
	if (buffer == nullptr) return;

	delete buffer;
}

MeshVertexBuffer* MeshVertexBufferCreate(MeshContext* context, MeshVertex* vertices, MeshUint numVertices)
{
	MeshVertexBuffer* buffer = new MeshVertexBuffer;

	buffer->m_numElements = numVertices;
	// create a vertex buffer
	{
		UINT bufferSize = (UINT)(numVertices * sizeof(MeshVertex));

		D3D11_BUFFER_DESC bufDesc;
		bufDesc.ByteWidth = bufferSize;
		bufDesc.Usage = D3D11_USAGE_IMMUTABLE;
		bufDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
		bufDesc.CPUAccessFlags = DXGI_CPU_ACCESS_NONE;
		bufDesc.MiscFlags = 0;

		D3D11_SUBRESOURCE_DATA data = {};
		data.pSysMem = vertices;

		context->m_device->CreateBuffer(&bufDesc, &data, &buffer->m_buffer);
	}

	return buffer;
}

void MeshVertexBufferRelease(MeshVertexBuffer* buffer)
{
	if (buffer == nullptr) return;

	delete buffer;
}

void MeshContextDraw(MeshContext* context, const MeshContextDrawParams* params)
{
	using namespace DirectX;

	XMMATRIX matrix = XMMatrixTranspose(XMMatrixMultiply(XMMatrixMultiply(
		params->params->model, 
		params->params->view),
		params->params->projection)
		);

	ID3D11DeviceContext* deviceContext = context->m_deviceContext;

	// update constant buffer
	{
		D3D11_MAPPED_SUBRESOURCE mappedResource = {};
		if (S_OK == deviceContext->Map(context->m_constantBuffer, 0u, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource))
		{
			XMStoreFloat4x4((XMFLOAT4X4*)(mappedResource.pData), matrix);

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

	deviceContext->VSSetShader(context->m_meshVS, nullptr, 0u);
	deviceContext->PSSetShader(context->m_meshPS, nullptr, 0u);

	deviceContext->IASetInputLayout(context->m_inputLayout);

	deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

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

	UINT vertexStride = sizeof(MeshVertex);
	UINT offset = 0u;
	deviceContext->IASetVertexBuffers(0, 1, &params->vertexBuffer->m_buffer, &vertexStride, &offset);
	deviceContext->IASetIndexBuffer(params->indexBuffer->m_buffer, DXGI_FORMAT_R32_UINT, 0u);

	float depthSign = DirectX::XMVectorGetW(params->params->projection.r[2]);
	if (depthSign < 0.f)
	{
		deviceContext->RSSetState(context->m_rasterizerStateRH);
	}

	deviceContext->DrawIndexed((UINT)params->indexBuffer->m_numElements, 0, 0);

	if (depthSign < 0.f)
	{
		deviceContext->RSSetState(nullptr);
	}
}