|
@@ -1,468 +0,0 @@
|
1
|
|
-#include <cstddef>
|
2
|
|
-#include "app.h"
|
3
|
|
-#include "util.h"
|
4
|
|
-
|
5
|
|
-#define TINYOBJLOADER_IMPLMENTATION
|
6
|
|
-#include "tiny_obj_loader.h"
|
7
|
|
-#include <unordered_map>
|
8
|
|
-
|
9
|
|
-
|
10
|
|
-class SampleObjMesh
|
11
|
|
-{
|
12
|
|
-public:
|
13
|
|
- struct Vertex
|
14
|
|
- {
|
15
|
|
- DKVector3 inPos;
|
16
|
|
- DKVector3 inColor;
|
17
|
|
- DKVector2 intexCoord;
|
18
|
|
-
|
19
|
|
- int Compare(const Vertex& Other) const
|
20
|
|
- {
|
21
|
|
- const float pp[] = { this->inPos.x, this->inPos.y, this->inPos.z,
|
22
|
|
- this->inColor.x, this->inColor.y, this->inColor.z,
|
23
|
|
- this->intexCoord.x, this->intexCoord.y};
|
24
|
|
- const float rr[] = { Other.inPos.x, Other.inPos.y, Other.inPos.z,
|
25
|
|
- Other.inColor.x, Other.inColor.y, Other.inColor.z,
|
26
|
|
- Other.intexCoord.x, Other.intexCoord.y};
|
27
|
|
-
|
28
|
|
- for (int i = 0; i < std::size(pp); ++i)
|
29
|
|
- {
|
30
|
|
- auto k = pp[i] - rr[i];
|
31
|
|
- if (abs(k) < 0.0001)
|
32
|
|
- continue;
|
33
|
|
- if (k > 0)
|
34
|
|
- return 1;
|
35
|
|
- else if (k < 0)
|
36
|
|
- return -1;
|
37
|
|
- }
|
38
|
|
- return 0;
|
39
|
|
- }
|
40
|
|
-
|
41
|
|
- bool operator > (const Vertex& Other) const
|
42
|
|
- {
|
43
|
|
- return Compare(Other) > 0;
|
44
|
|
- }
|
45
|
|
-
|
46
|
|
- bool operator < (const Vertex& Other) const
|
47
|
|
- {
|
48
|
|
- return Compare(Other) < 0;
|
49
|
|
- }
|
50
|
|
- };
|
51
|
|
-
|
52
|
|
- SampleObjMesh()
|
53
|
|
- {
|
54
|
|
- vertices.Reserve(100);
|
55
|
|
- indices.Reserve(100);
|
56
|
|
- }
|
57
|
|
-
|
58
|
|
- void LoadFromObjFile(const char* InPath)
|
59
|
|
- {
|
60
|
|
- tinyobj::attrib_t attrib;
|
61
|
|
- std::vector<tinyobj::shape_t> shapes;
|
62
|
|
- std::vector<tinyobj::material_t> materials;
|
63
|
|
- std::string err;
|
64
|
|
- if (!tinyobj::LoadObj(&attrib, &shapes, &materials, &err, InPath)) {
|
65
|
|
- throw std::runtime_error(err);
|
66
|
|
- }
|
67
|
|
-
|
68
|
|
- DKMap<Vertex, uint32_t> uniqueVertices;
|
69
|
|
- DKLog("Save to Container");
|
70
|
|
- for (const auto& shape : shapes)
|
71
|
|
- {
|
72
|
|
- for (const auto& index : shape.mesh.indices)
|
73
|
|
- {
|
74
|
|
- Vertex vertex = {};
|
75
|
|
-
|
76
|
|
- vertex.inPos = {
|
77
|
|
- attrib.vertices[3 * index.vertex_index + 0],
|
78
|
|
- attrib.vertices[3 * index.vertex_index + 1],
|
79
|
|
- attrib.vertices[3 * index.vertex_index + 2]
|
80
|
|
- };
|
81
|
|
-
|
82
|
|
- if (attrib.texcoords.size())
|
83
|
|
- {
|
84
|
|
- vertex.intexCoord = {
|
85
|
|
- attrib.texcoords[2 * index.texcoord_index + 0],
|
86
|
|
- 1.0f - attrib.texcoords[2 * index.texcoord_index + 1]
|
87
|
|
- };
|
88
|
|
- }
|
89
|
|
-
|
90
|
|
- vertex.inColor = { 1.0f, 1.0f, 1.0f };
|
91
|
|
-
|
92
|
|
- if (uniqueVertices.Find(vertex) == nullptr)
|
93
|
|
- {
|
94
|
|
- uniqueVertices.Insert(vertex, static_cast<uint32_t>(vertices.Count()));
|
95
|
|
- vertices.Add(vertex);
|
96
|
|
- }
|
97
|
|
-
|
98
|
|
- indices.Add(uniqueVertices.Value(vertex));
|
99
|
|
-
|
100
|
|
- aabb.Expand(vertex.inPos);
|
101
|
|
- }
|
102
|
|
- }
|
103
|
|
- }
|
104
|
|
-
|
105
|
|
- uint32_t GetVerticesCount() const {
|
106
|
|
- return static_cast<uint32_t>(vertices.Count()); };
|
107
|
|
- uint32_t GetIndicesCount() const {
|
108
|
|
- return static_cast<uint32_t>(indices.Count()); };
|
109
|
|
- const Vertex* GetVerticesData() const {
|
110
|
|
- return vertices; }
|
111
|
|
- const uint32_t* GetIndicesData() const {
|
112
|
|
- return indices; }
|
113
|
|
-
|
114
|
|
- DKAabb aabb;
|
115
|
|
-private:
|
116
|
|
- DKArray<Vertex> vertices;
|
117
|
|
- DKArray<uint32_t> indices;
|
118
|
|
- DKSpinLock MeshLock;
|
119
|
|
-};
|
120
|
|
-
|
121
|
|
-///// Template Spealization for DKString. (for DKMap, DKSet)
|
122
|
|
-//template <> struct DKMapKeyComparator<SampleObjMesh::Vertex>
|
123
|
|
-//{
|
124
|
|
-// int operator () (const DKStringW& lhs, const DKStringW& rhs) const
|
125
|
|
-// {
|
126
|
|
-// return lhs.Compare(rhs);
|
127
|
|
-// }
|
128
|
|
-//};
|
129
|
|
-
|
130
|
|
-
|
131
|
|
-class MeshDemo : public SampleApp
|
132
|
|
-{
|
133
|
|
- DKObject<DKWindow> window;
|
134
|
|
- DKObject<DKThread> renderThread;
|
135
|
|
- DKAtomicNumber32 runningRenderThread;
|
136
|
|
- DKObject<SampleObjMesh> SampleMesh;
|
137
|
|
-
|
138
|
|
-public:
|
139
|
|
- void LoadMesh()
|
140
|
|
- {
|
141
|
|
-
|
142
|
|
- DKLog("Loading Mesh");
|
143
|
|
- DKString path = resourcePool.ResourceFilePath("meshes/chalet/chalet.obj");
|
144
|
|
- SampleMesh->LoadFromObjFile(DKStringU8(path));
|
145
|
|
- }
|
146
|
|
-
|
147
|
|
- DKObject<DKTexture> LoadTexture2D(DKCommandQueue* queue, DKData* data)
|
148
|
|
- {
|
149
|
|
- DKObject<DKImage> image = DKImage::Create(data);
|
150
|
|
- if (image)
|
151
|
|
- {
|
152
|
|
- DKGraphicsDevice* device = queue->Device();
|
153
|
|
- DKTextureDescriptor texDesc = {};
|
154
|
|
- texDesc.textureType = DKTexture::Type2D;
|
155
|
|
- texDesc.pixelFormat = DKPixelFormat::RGBA8Unorm;
|
156
|
|
- texDesc.width = image->Width();
|
157
|
|
- texDesc.height = image->Height();
|
158
|
|
- texDesc.depth = 1;
|
159
|
|
- texDesc.mipmapLevels = 1;
|
160
|
|
- texDesc.sampleCount = 1;
|
161
|
|
- texDesc.arrayLength = 1;
|
162
|
|
- texDesc.usage = DKTexture::UsageCopyDestination | DKTexture::UsageSampled;
|
163
|
|
- DKObject<DKTexture> tex = device->CreateTexture(texDesc);
|
164
|
|
- if (tex)
|
165
|
|
- {
|
166
|
|
- size_t bytesPerPixel = image->BytesPerPixel();
|
167
|
|
- DKASSERT_DESC(bytesPerPixel == DKPixelFormatBytesPerPixel(texDesc.pixelFormat), "BytesPerPixel mismatch!");
|
168
|
|
-
|
169
|
|
- uint32_t width = image->Width();
|
170
|
|
- uint32_t height = image->Height();
|
171
|
|
-
|
172
|
|
- size_t bufferLength = bytesPerPixel * width * height;
|
173
|
|
- DKObject<DKGpuBuffer> stagingBuffer = device->CreateBuffer(bufferLength, DKGpuBuffer::StorageModeShared, DKCpuCacheModeReadWrite);
|
174
|
|
-
|
175
|
|
- memcpy(stagingBuffer->Contents(), image->Contents(), bufferLength);
|
176
|
|
- stagingBuffer->Flush();
|
177
|
|
-
|
178
|
|
- DKObject<DKCommandBuffer> cb = queue->CreateCommandBuffer();
|
179
|
|
- DKObject<DKCopyCommandEncoder> encoder = cb->CreateCopyCommandEncoder();
|
180
|
|
- encoder->CopyFromBufferToTexture(stagingBuffer,
|
181
|
|
- { 0, width, height },
|
182
|
|
- tex,
|
183
|
|
- { 0,0, 0,0,0 },
|
184
|
|
- { width,height,1 });
|
185
|
|
- encoder->EndEncoding();
|
186
|
|
- cb->Commit();
|
187
|
|
-
|
188
|
|
- DKLog("Texture created!");
|
189
|
|
- return tex;
|
190
|
|
- }
|
191
|
|
- }
|
192
|
|
- return nullptr;
|
193
|
|
- }
|
194
|
|
- void RenderThread(void)
|
195
|
|
- {
|
196
|
|
- DKObject<DKData> vertData = resourcePool.LoadResourceData("shaders/mesh.vert.spv");
|
197
|
|
- DKObject<DKData> fragData = resourcePool.LoadResourceData("shaders/mesh.frag.spv");
|
198
|
|
- DKShader vertShader(vertData);
|
199
|
|
- DKShader fragShader(fragData);
|
200
|
|
-
|
201
|
|
- DKObject<DKGraphicsDevice> device = DKGraphicsDevice::SharedInstance();
|
202
|
|
- DKObject<DKCommandQueue> queue = device->CreateCommandQueue(DKCommandQueue::Graphics);
|
203
|
|
-
|
204
|
|
- // create texture
|
205
|
|
- DKObject<DKTexture> texture = LoadTexture2D(queue, resourcePool.LoadResourceData("meshes/chalet.png"));
|
206
|
|
- // create sampler
|
207
|
|
- DKSamplerDescriptor samplerDesc = {};
|
208
|
|
- samplerDesc.magFilter = DKSamplerDescriptor::MinMagFilterLinear;
|
209
|
|
- samplerDesc.minFilter = DKSamplerDescriptor::MinMagFilterLinear;
|
210
|
|
- samplerDesc.addressModeU = DKSamplerDescriptor::AddressModeClampToEdge;
|
211
|
|
- samplerDesc.addressModeV = DKSamplerDescriptor::AddressModeClampToEdge;
|
212
|
|
- samplerDesc.addressModeW = DKSamplerDescriptor::AddressModeClampToEdge;
|
213
|
|
- samplerDesc.maxAnisotropy = 16;
|
214
|
|
-
|
215
|
|
- DKObject<DKSamplerState> sampler = device->CreateSamplerState(samplerDesc);
|
216
|
|
-
|
217
|
|
- // create shaders
|
218
|
|
- DKObject<DKShaderModule> vertShaderModule = device->CreateShaderModule(&vertShader);
|
219
|
|
- DKObject<DKShaderModule> fragShaderModule = device->CreateShaderModule(&fragShader);
|
220
|
|
-
|
221
|
|
- DKObject<DKShaderFunction> vertShaderFunction = vertShaderModule->CreateFunction(vertShaderModule->FunctionNames().Value(0));
|
222
|
|
- DKObject<DKShaderFunction> fragShaderFunction = fragShaderModule->CreateFunction(fragShaderModule->FunctionNames().Value(0));
|
223
|
|
-
|
224
|
|
- DKObject<DKSwapChain> swapChain = queue->CreateSwapChain(window);
|
225
|
|
-
|
226
|
|
- DKLog("VertexFunction.VertexAttributes: %d", vertShaderFunction->StageInputAttributes().Count());
|
227
|
|
- for (int i = 0; i < vertShaderFunction->StageInputAttributes().Count(); ++i)
|
228
|
|
- {
|
229
|
|
- const DKShaderAttribute& attr = vertShaderFunction->StageInputAttributes().Value(i);
|
230
|
|
- DKLog(" --> VertexAttribute[%d]: \"%ls\" (location:%u)", i, (const wchar_t*)attr.name, attr.location);
|
231
|
|
- }
|
232
|
|
-
|
233
|
|
-
|
234
|
|
- uint32_t vertexBufferSize = static_cast<uint32_t>(SampleMesh->GetVerticesCount()) * sizeof(SampleObjMesh::Vertex);
|
235
|
|
- uint32_t indexBufferSize = SampleMesh->GetIndicesCount() * sizeof(uint32_t);
|
236
|
|
-
|
237
|
|
- DKObject<DKGpuBuffer> vertexBuffer = device->CreateBuffer(vertexBufferSize, DKGpuBuffer::StorageModeShared, DKCpuCacheModeReadWrite);
|
238
|
|
- memcpy(vertexBuffer->Contents(), SampleMesh->GetVerticesData(), vertexBufferSize);
|
239
|
|
- vertexBuffer->Flush();
|
240
|
|
-
|
241
|
|
- DKObject<DKGpuBuffer> indexBuffer = device->CreateBuffer(indexBufferSize, DKGpuBuffer::StorageModeShared, DKCpuCacheModeReadWrite);
|
242
|
|
- memcpy(indexBuffer->Contents(), SampleMesh->GetIndicesData(), indexBufferSize);
|
243
|
|
- indexBuffer->Flush();
|
244
|
|
-
|
245
|
|
- DKRenderPipelineDescriptor pipelineDescriptor;
|
246
|
|
- // setup shader
|
247
|
|
- pipelineDescriptor.vertexFunction = vertShaderFunction;
|
248
|
|
- pipelineDescriptor.fragmentFunction = fragShaderFunction;
|
249
|
|
- // setup color-attachment render-targets
|
250
|
|
- pipelineDescriptor.colorAttachments.Resize(1);
|
251
|
|
- pipelineDescriptor.colorAttachments.Value(0).pixelFormat = swapChain->ColorPixelFormat();
|
252
|
|
- pipelineDescriptor.colorAttachments.Value(0).blendState.enabled = false;
|
253
|
|
- pipelineDescriptor.colorAttachments.Value(0).blendState.sourceRGBBlendFactor = DKBlendFactor::SourceAlpha;
|
254
|
|
- pipelineDescriptor.colorAttachments.Value(0).blendState.destinationRGBBlendFactor = DKBlendFactor::OneMinusSourceAlpha;
|
255
|
|
- // setup depth-stencil
|
256
|
|
- pipelineDescriptor.depthStencilAttachmentPixelFormat = DKPixelFormat::D32Float;
|
257
|
|
- pipelineDescriptor.depthStencilDescriptor.depthWriteEnabled = true;
|
258
|
|
- pipelineDescriptor.depthStencilDescriptor.depthCompareFunction = DKCompareFunctionLessEqual;
|
259
|
|
- // setup vertex buffer and attributes
|
260
|
|
- pipelineDescriptor.vertexDescriptor.attributes = {
|
261
|
|
- { DKVertexFormat::Float3, offsetof(SampleObjMesh::Vertex, inPos), 0, 0 },
|
262
|
|
- { DKVertexFormat::Float3, offsetof(SampleObjMesh::Vertex, inColor), 0, 1 },
|
263
|
|
- { DKVertexFormat::Float2, offsetof(SampleObjMesh::Vertex, intexCoord), 0, 2 },
|
264
|
|
- };
|
265
|
|
- pipelineDescriptor.vertexDescriptor.layouts = {
|
266
|
|
- { DKVertexStepRate::Vertex, sizeof(SampleObjMesh::Vertex), 0 },
|
267
|
|
- };
|
268
|
|
- // setup topology and rasterization
|
269
|
|
- pipelineDescriptor.primitiveTopology = DKPrimitiveType::Triangle;
|
270
|
|
- pipelineDescriptor.frontFace = DKFrontFace::CCW;
|
271
|
|
- pipelineDescriptor.triangleFillMode = DKTriangleFillMode::Fill;
|
272
|
|
- pipelineDescriptor.depthClipMode = DKDepthClipMode::Clip;
|
273
|
|
- pipelineDescriptor.cullMode = DKCullMode::Back;
|
274
|
|
- pipelineDescriptor.rasterizationEnabled = true;
|
275
|
|
-
|
276
|
|
- DKPipelineReflection reflection;
|
277
|
|
- DKObject<DKRenderPipelineState> pipelineState = device->CreateRenderPipeline(pipelineDescriptor, &reflection);
|
278
|
|
- if (pipelineState)
|
279
|
|
- {
|
280
|
|
- PrintPipelineReflection(&reflection, DKLogCategory::Verbose);
|
281
|
|
- }
|
282
|
|
-
|
283
|
|
- DKShaderBindingSetLayout layout;
|
284
|
|
- if (1)
|
285
|
|
- {
|
286
|
|
- DKShaderBinding bindings[2] = {
|
287
|
|
- {
|
288
|
|
- 0,
|
289
|
|
- DKShader::DescriptorTypeUniformBuffer,
|
290
|
|
- 1,
|
291
|
|
- nullptr
|
292
|
|
- },
|
293
|
|
- {
|
294
|
|
- 1,
|
295
|
|
- DKShader::DescriptorTypeTextureSampler,
|
296
|
|
- 1,
|
297
|
|
- nullptr
|
298
|
|
- },
|
299
|
|
- };
|
300
|
|
- layout.bindings.Add(bindings, 2);
|
301
|
|
- }
|
302
|
|
- DKObject<DKShaderBindingSet> bindSet = device->CreateShaderBindingSet(layout);
|
303
|
|
-
|
304
|
|
- struct UBO
|
305
|
|
- {
|
306
|
|
- DKMatrix4 projectionMatrix;
|
307
|
|
- DKMatrix4 modelMatrix;
|
308
|
|
- DKMatrix4 viewMatrix;
|
309
|
|
- };
|
310
|
|
- DKObject<DKGpuBuffer> uboBuffer = device->CreateBuffer(sizeof(UBO), DKGpuBuffer::StorageModeShared, DKCpuCacheModeReadWrite);
|
311
|
|
- UBO* ubo = nullptr;
|
312
|
|
- if (bindSet)
|
313
|
|
- {
|
314
|
|
- if (uboBuffer)
|
315
|
|
- {
|
316
|
|
- ubo = reinterpret_cast<UBO*>(uboBuffer->Contents());
|
317
|
|
- ubo->projectionMatrix = DKMatrix4::identity;
|
318
|
|
- ubo->modelMatrix = DKMatrix4::identity;
|
319
|
|
- ubo->viewMatrix = DKMatrix4::identity;
|
320
|
|
- uboBuffer->Flush();
|
321
|
|
-
|
322
|
|
- //DKAffineTransform3 trans;
|
323
|
|
- //trans.Multiply(DKLinearTransform3().Scale(0.5).Rotate(DKVector3(0,1,0), DKGL_PI * 0.5));
|
324
|
|
- //ubo.modelMatrix.Multiply(trans.Matrix4());
|
325
|
|
-
|
326
|
|
- //memcpy(uboBuffer->Contents(), &ubo, sizeof(ubo));
|
327
|
|
- bindSet->SetBuffer(0, uboBuffer, 0, sizeof(UBO));
|
328
|
|
- }
|
329
|
|
-
|
330
|
|
- bindSet->SetTexture(1, texture);
|
331
|
|
- bindSet->SetSamplerState(1, sampler);
|
332
|
|
- }
|
333
|
|
-
|
334
|
|
- DKObject<DKTexture> depthBuffer = nullptr;
|
335
|
|
-
|
336
|
|
- DKCamera camera;
|
337
|
|
- DKVector3 cameraPosition = { 0, 5, 10 };
|
338
|
|
- DKVector3 cameraTartget = { 0, 0, 0 };
|
339
|
|
-
|
340
|
|
- DKAffineTransform3 tm(DKLinearTransform3().Scale(5).Rotate(DKVector3(-1,0,0), DKGL_PI * 0.5));
|
341
|
|
-
|
342
|
|
- DKTimer timer;
|
343
|
|
- timer.Reset();
|
344
|
|
-
|
345
|
|
- DKLog("Render thread begin");
|
346
|
|
- while (!runningRenderThread.CompareAndSet(0, 0))
|
347
|
|
- {
|
348
|
|
- DKRenderPassDescriptor rpd = swapChain->CurrentRenderPassDescriptor();
|
349
|
|
- double t = timer.Elapsed();
|
350
|
|
- double waveT = (cos(t) + 1.0) * 0.5;
|
351
|
|
- rpd.colorAttachments.Value(0).clearColor = DKColor(waveT, 0.0, 0.0, 0.0);
|
352
|
|
-
|
353
|
|
- int width = rpd.colorAttachments.Value(0).renderTarget->Width();
|
354
|
|
- int height = rpd.colorAttachments.Value(0).renderTarget->Height();
|
355
|
|
- if (depthBuffer)
|
356
|
|
- {
|
357
|
|
- if (depthBuffer->Width() != width ||
|
358
|
|
- depthBuffer->Height() != height )
|
359
|
|
- depthBuffer = nullptr;
|
360
|
|
- }
|
361
|
|
- if (depthBuffer == nullptr)
|
362
|
|
- {
|
363
|
|
- // create depth buffer
|
364
|
|
- DKTextureDescriptor texDesc = {};
|
365
|
|
- texDesc.textureType = DKTexture::Type2D;
|
366
|
|
- texDesc.pixelFormat = DKPixelFormat::D32Float;
|
367
|
|
- texDesc.width = width;
|
368
|
|
- texDesc.height = height;
|
369
|
|
- texDesc.depth = 1;
|
370
|
|
- texDesc.mipmapLevels = 1;
|
371
|
|
- texDesc.sampleCount = 1;
|
372
|
|
- texDesc.arrayLength = 1;
|
373
|
|
- texDesc.usage = DKTexture::UsageRenderTarget;
|
374
|
|
- depthBuffer = device->CreateTexture(texDesc);
|
375
|
|
- }
|
376
|
|
- rpd.depthStencilAttachment.renderTarget = depthBuffer;
|
377
|
|
- rpd.depthStencilAttachment.loadAction = DKRenderPassAttachmentDescriptor::LoadActionClear;
|
378
|
|
- rpd.depthStencilAttachment.storeAction = DKRenderPassAttachmentDescriptor::StoreActionDontCare;
|
379
|
|
-
|
380
|
|
- DKObject<DKCommandBuffer> buffer = queue->CreateCommandBuffer();
|
381
|
|
- DKObject<DKRenderCommandEncoder> encoder = buffer->CreateRenderCommandEncoder(rpd);
|
382
|
|
- if (encoder)
|
383
|
|
- {
|
384
|
|
- if (bindSet && ubo)
|
385
|
|
- {
|
386
|
|
- camera.SetView(cameraPosition, cameraTartget - cameraPosition, DKVector3(0, 1, 0));
|
387
|
|
- camera.SetPerspective(DKGL_DEGREE_TO_RADIAN(90), float(width)/float(height), 1, 1000);
|
388
|
|
-
|
389
|
|
- ubo->projectionMatrix = camera.ProjectionMatrix();
|
390
|
|
- ubo->viewMatrix = camera.ViewMatrix();
|
391
|
|
-
|
392
|
|
- DKQuaternion quat(DKVector3(0, 1, 0), t);
|
393
|
|
- DKAffineTransform3 trans = tm * DKAffineTransform3(quat);
|
394
|
|
- ubo->modelMatrix = trans.Matrix4();
|
395
|
|
- uboBuffer->Flush();
|
396
|
|
- bindSet->SetBuffer(0, uboBuffer, 0, sizeof(UBO));
|
397
|
|
- }
|
398
|
|
-
|
399
|
|
- encoder->SetRenderPipelineState(pipelineState);
|
400
|
|
- encoder->SetVertexBuffer(vertexBuffer, 0, 0);
|
401
|
|
- encoder->SetIndexBuffer(indexBuffer, 0, DKIndexType::UInt32);
|
402
|
|
- encoder->SetResources(0, bindSet);
|
403
|
|
- // draw scene!
|
404
|
|
- encoder->DrawIndexed(SampleMesh->GetIndicesCount(), 1, 0, 0, 0);
|
405
|
|
- encoder->EndEncoding();
|
406
|
|
- buffer->Commit();
|
407
|
|
- swapChain->Present();
|
408
|
|
- }
|
409
|
|
- else
|
410
|
|
- {
|
411
|
|
- }
|
412
|
|
- DKThread::Sleep(0.01);
|
413
|
|
- }
|
414
|
|
- DKLog("RenderThread terminating...");
|
415
|
|
- }
|
416
|
|
-
|
417
|
|
- void OnInitialize(void) override
|
418
|
|
- {
|
419
|
|
- SampleApp::OnInitialize();
|
420
|
|
- DKLogD("%s", DKGL_FUNCTION_NAME);
|
421
|
|
-
|
422
|
|
- // create window
|
423
|
|
- window = DKWindow::Create("DefaultWindow");
|
424
|
|
- window->SetOrigin({ 0, 0 });
|
425
|
|
- window->Resize({ 320, 240 });
|
426
|
|
- window->Activate();
|
427
|
|
-
|
428
|
|
- window->AddEventHandler(this, DKFunction([this](const DKWindow::WindowEvent& e)
|
429
|
|
- {
|
430
|
|
- if (e.type == DKWindow::WindowEvent::WindowClosed)
|
431
|
|
- DKApplication::Instance()->Terminate(0);
|
432
|
|
- }), NULL, NULL);
|
433
|
|
-
|
434
|
|
- SampleMesh = DKOBJECT_NEW SampleObjMesh();
|
435
|
|
-
|
436
|
|
- LoadMesh();
|
437
|
|
-
|
438
|
|
- runningRenderThread = 1;
|
439
|
|
- renderThread = DKThread::Create(DKFunction(this, &MeshDemo::RenderThread)->Invocation());
|
440
|
|
- }
|
441
|
|
- void OnTerminate(void) override
|
442
|
|
- {
|
443
|
|
- DKLogD("%s", DKGL_FUNCTION_NAME);
|
444
|
|
-
|
445
|
|
- runningRenderThread = 0;
|
446
|
|
- renderThread->WaitTerminate();
|
447
|
|
- renderThread = NULL;
|
448
|
|
- window = NULL;
|
449
|
|
-
|
450
|
|
- SampleApp::OnTerminate();
|
451
|
|
- }
|
452
|
|
-};
|
453
|
|
-
|
454
|
|
-
|
455
|
|
-#ifdef _WIN32
|
456
|
|
-int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
|
457
|
|
- _In_opt_ HINSTANCE hPrevInstance,
|
458
|
|
- _In_ LPWSTR lpCmdLine,
|
459
|
|
- _In_ int nCmdShow)
|
460
|
|
-#else
|
461
|
|
-int main(int argc, const char * argv[])
|
462
|
|
-#endif
|
463
|
|
-{
|
464
|
|
- MeshDemo app;
|
465
|
|
- DKPropertySet::SystemConfig().SetValue("AppDelegate", "AppDelegate");
|
466
|
|
- DKPropertySet::SystemConfig().SetValue("GraphicsAPI", "Vulkan");
|
467
|
|
- return app.Run();
|
468
|
|
-}
|