DKGL2 sample codes

Mesh.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #include <cstddef>
  2. #include "app.h"
  3. #include "util.h"
  4. #define TINYOBJLOADER_IMPLMENTATION
  5. #include "tiny_obj_loader.h"
  6. #include <unordered_map>
  7. class SampleObjMesh
  8. {
  9. public:
  10. struct Vertex
  11. {
  12. DKVector3 inPos;
  13. DKVector3 inColor;
  14. DKVector2 intexCoord;
  15. int Compare(const Vertex& Other) const
  16. {
  17. if ( this == &Other
  18. || inPos == Other.inPos && inColor == Other.inColor && intexCoord == Other.intexCoord)
  19. {
  20. return 0;
  21. }
  22. else
  23. {
  24. return static_cast<int>(this - &Other);
  25. }
  26. }
  27. bool operator > (const Vertex& Other) const
  28. {
  29. return Compare(Other) > 0;
  30. }
  31. bool operator < (const Vertex& Other) const
  32. {
  33. return Compare(Other) < 0;
  34. }
  35. };
  36. SampleObjMesh()
  37. {
  38. vertices.Reserve(100);
  39. indices.Reserve(100);
  40. }
  41. void LoadFromObjFile(const char* InPath)
  42. {
  43. tinyobj::attrib_t attrib;
  44. std::vector<tinyobj::shape_t> shapes;
  45. std::vector<tinyobj::material_t> materials;
  46. std::string err;
  47. if (!tinyobj::LoadObj(&attrib, &shapes, &materials, &err, InPath)) {
  48. throw std::runtime_error(err);
  49. }
  50. DKMap<Vertex, uint32_t> uniqueVertices;
  51. DKLog("Save to Container");
  52. for (const auto& shape : shapes)
  53. {
  54. for (const auto& index : shape.mesh.indices)
  55. {
  56. Vertex vertex = {};
  57. vertex.inPos = {
  58. attrib.vertices[3 * index.vertex_index + 0],
  59. attrib.vertices[3 * index.vertex_index + 1],
  60. attrib.vertices[3 * index.vertex_index + 2]
  61. };
  62. if (attrib.texcoords.size())
  63. {
  64. vertex.intexCoord = {
  65. attrib.texcoords[2 * index.texcoord_index + 0],
  66. 1.0f - attrib.texcoords[2 * index.texcoord_index + 1]
  67. };
  68. }
  69. vertex.inColor = { 1.0f, 1.0f, 1.0f };
  70. if (uniqueVertices.Find(vertex) == nullptr)
  71. {
  72. uniqueVertices.Insert(vertex, static_cast<uint32_t>(vertices.Count()));
  73. vertices.Add(vertex);
  74. }
  75. indices.Add(uniqueVertices.Value(vertex));
  76. aabb.Expand(vertex.inPos);
  77. }
  78. }
  79. }
  80. uint32_t GetVerticesCount() const {
  81. return static_cast<uint32_t>(vertices.Count()); };
  82. uint32_t GetIndicesCount() const {
  83. return static_cast<uint32_t>(indices.Count()); };
  84. const Vertex* GetVerticesData() const {
  85. return vertices; }
  86. const uint32_t* GetIndicesData() const {
  87. return indices; }
  88. DKAabb aabb;
  89. private:
  90. DKArray<Vertex> vertices;
  91. DKArray<uint32_t> indices;
  92. DKSpinLock MeshLock;
  93. };
  94. ///// Template Spealization for DKString. (for DKMap, DKSet)
  95. //template <> struct DKMapKeyComparator<SampleObjMesh::Vertex>
  96. //{
  97. // int operator () (const DKStringW& lhs, const DKStringW& rhs) const
  98. // {
  99. // return lhs.Compare(rhs);
  100. // }
  101. //};
  102. class MeshDemo : public SampleApp
  103. {
  104. DKObject<DKWindow> window;
  105. DKObject<DKThread> renderThread;
  106. DKAtomicNumber32 runningRenderThread;
  107. DKObject<SampleObjMesh> SampleMesh;
  108. public:
  109. void LoadMesh()
  110. {
  111. DKLog("Loading Mesh");
  112. DKString path = resourcePool.ResourceFilePath("meshes/car.obj");
  113. SampleMesh->LoadFromObjFile(DKStringU8(path));
  114. }
  115. DKObject<DKTexture> LoadTexture2D(DKCommandQueue* queue, DKData* data)
  116. {
  117. DKObject<DKImage> image = DKImage::Create(data);
  118. if (image)
  119. {
  120. DKGraphicsDevice* device = queue->Device();
  121. DKTextureDescriptor texDesc = {};
  122. texDesc.textureType = DKTexture::Type2D;
  123. texDesc.pixelFormat = DKPixelFormat::RGBA8Unorm;
  124. texDesc.width = image->Width();
  125. texDesc.height = image->Height();
  126. texDesc.depth = 1;
  127. texDesc.mipmapLevels = 1;
  128. texDesc.sampleCount = 1;
  129. texDesc.arrayLength = 1;
  130. texDesc.usage = DKTexture::UsageCopyDestination | DKTexture::UsageSampled;
  131. DKObject<DKTexture> tex = device->CreateTexture(texDesc);
  132. if (tex)
  133. {
  134. size_t bytesPerPixel = image->BytesPerPixel();
  135. uint32_t width = image->Width();
  136. uint32_t height = image->Height();
  137. size_t bufferLength = bytesPerPixel * width * height;
  138. DKObject<DKGpuBuffer> stagingBuffer = device->CreateBuffer(bufferLength, DKGpuBuffer::StorageModeShared, DKCpuCacheModeReadWrite);
  139. memcpy(stagingBuffer->Contents(), image->Contents(), bufferLength);
  140. stagingBuffer->Flush();
  141. DKObject<DKCommandBuffer> cb = queue->CreateCommandBuffer();
  142. DKObject<DKCopyCommandEncoder> encoder = cb->CreateCopyCommandEncoder();
  143. encoder->CopyFromBufferToTexture(stagingBuffer,
  144. { 0, width, height },
  145. tex,
  146. { 0,0, 0,0,0 },
  147. { width,height,1 });
  148. encoder->EndEncoding();
  149. cb->Commit();
  150. DKLog("Texture created!");
  151. return tex;
  152. }
  153. }
  154. return nullptr;
  155. }
  156. void RenderThread(void)
  157. {
  158. DKObject<DKData> vertData = resourcePool.LoadResourceData("shaders/mesh.vert.spv");
  159. DKObject<DKData> fragData = resourcePool.LoadResourceData("shaders/mesh.frag.spv");
  160. DKShader vertShader(vertData);
  161. DKShader fragShader(fragData);
  162. DKObject<DKGraphicsDevice> device = DKGraphicsDevice::SharedInstance();
  163. DKObject<DKCommandQueue> queue = device->CreateCommandQueue(DKCommandQueue::Graphics);
  164. // create texture
  165. DKObject<DKTexture> texture = LoadTexture2D(queue, resourcePool.LoadResourceData("textures/koo.jpg"));
  166. // create sampler
  167. DKSamplerDescriptor samplerDesc = {};
  168. samplerDesc.magFilter = DKSamplerDescriptor::MinMagFilterLinear;
  169. samplerDesc.minFilter = DKSamplerDescriptor::MinMagFilterLinear;
  170. samplerDesc.addressModeU = DKSamplerDescriptor::AddressModeClampToEdge;
  171. samplerDesc.addressModeV = DKSamplerDescriptor::AddressModeClampToEdge;
  172. samplerDesc.addressModeW = DKSamplerDescriptor::AddressModeClampToEdge;
  173. samplerDesc.maxAnisotropy = 16;
  174. DKObject<DKSamplerState> sampler = device->CreateSamplerState(samplerDesc);
  175. // create shaders
  176. DKObject<DKShaderModule> vertShaderModule = device->CreateShaderModule(&vertShader);
  177. DKObject<DKShaderModule> fragShaderModule = device->CreateShaderModule(&fragShader);
  178. DKObject<DKShaderFunction> vertShaderFunction = vertShaderModule->CreateFunction(vertShaderModule->FunctionNames().Value(0));
  179. DKObject<DKShaderFunction> fragShaderFunction = fragShaderModule->CreateFunction(fragShaderModule->FunctionNames().Value(0));
  180. DKObject<DKSwapChain> swapChain = queue->CreateSwapChain(window);
  181. DKLog("VertexFunction.VertexAttributes: %d", vertShaderFunction->StageInputAttributes().Count());
  182. for (int i = 0; i < vertShaderFunction->StageInputAttributes().Count(); ++i)
  183. {
  184. const DKShaderAttribute& attr = vertShaderFunction->StageInputAttributes().Value(i);
  185. DKLog(" --> VertexAttribute[%d]: \"%ls\" (location:%u)", i, (const wchar_t*)attr.name, attr.location);
  186. }
  187. uint32_t vertexBufferSize = static_cast<uint32_t>(SampleMesh->GetVerticesCount()) * sizeof(SampleObjMesh::Vertex);
  188. uint32_t indexBufferSize = SampleMesh->GetIndicesCount() * sizeof(uint32_t);
  189. DKObject<DKGpuBuffer> vertexBuffer = device->CreateBuffer(vertexBufferSize, DKGpuBuffer::StorageModeShared, DKCpuCacheModeReadWrite);
  190. memcpy(vertexBuffer->Contents(), SampleMesh->GetVerticesData(), vertexBufferSize);
  191. vertexBuffer->Flush();
  192. DKObject<DKGpuBuffer> indexBuffer = device->CreateBuffer(indexBufferSize, DKGpuBuffer::StorageModeShared, DKCpuCacheModeReadWrite);
  193. memcpy(indexBuffer->Contents(), SampleMesh->GetIndicesData(), indexBufferSize);
  194. indexBuffer->Flush();
  195. DKRenderPipelineDescriptor pipelineDescriptor;
  196. pipelineDescriptor.vertexFunction = vertShaderFunction;
  197. pipelineDescriptor.fragmentFunction = fragShaderFunction;
  198. pipelineDescriptor.colorAttachments.Resize(1);
  199. pipelineDescriptor.colorAttachments.Value(0).pixelFormat = swapChain->ColorPixelFormat();
  200. pipelineDescriptor.colorAttachments.Value(0).blendingEnabled = false;
  201. pipelineDescriptor.colorAttachments.Value(0).sourceRGBBlendFactor = DKBlendFactor::SourceAlpha;
  202. pipelineDescriptor.colorAttachments.Value(0).destinationRGBBlendFactor = DKBlendFactor::OneMinusSourceAlpha;
  203. pipelineDescriptor.depthStencilAttachmentPixelFormat = DKPixelFormat::Invalid; // no depth buffer
  204. pipelineDescriptor.vertexDescriptor.attributes = {
  205. { DKVertexFormat::Float3, offsetof(SampleObjMesh::Vertex, inPos), 0, 0 },
  206. { DKVertexFormat::Float3, offsetof(SampleObjMesh::Vertex, inColor), 0, 1 },
  207. { DKVertexFormat::Float2, offsetof(SampleObjMesh::Vertex, intexCoord), 0, 2 },
  208. };
  209. pipelineDescriptor.vertexDescriptor.layouts = {
  210. { DKVertexStepRate::Vertex, sizeof(SampleObjMesh::Vertex), 0 },
  211. };
  212. pipelineDescriptor.primitiveTopology = DKPrimitiveType::Triangle;
  213. pipelineDescriptor.frontFace = DKFrontFace::CCW;
  214. pipelineDescriptor.triangleFillMode = DKTriangleFillMode::Fill;
  215. pipelineDescriptor.depthClipMode = DKDepthClipMode::Clip;
  216. pipelineDescriptor.cullMode = DKCullMode::None;
  217. pipelineDescriptor.rasterizationEnabled = true;
  218. DKPipelineReflection reflection;
  219. DKObject<DKRenderPipelineState> pipelineState = device->CreateRenderPipeline(pipelineDescriptor, &reflection);
  220. if (pipelineState)
  221. {
  222. PrintPipelineReflection(&reflection, DKLogCategory::Verbose);
  223. }
  224. DKShaderBindingSetLayout layout;
  225. if (1)
  226. {
  227. DKShaderBinding bindings[2] = {
  228. {
  229. 0,
  230. DKShader::DescriptorTypeUniformBuffer,
  231. 1,
  232. nullptr
  233. },
  234. {
  235. 1,
  236. DKShader::DescriptorTypeTextureSampler,
  237. 1,
  238. nullptr
  239. },
  240. };
  241. layout.bindings.Add(bindings, 2);
  242. }
  243. DKObject<DKShaderBindingSet> bindSet = device->CreateShaderBindingSet(layout);
  244. if (bindSet)
  245. {
  246. struct
  247. {
  248. DKMatrix4 projectionMatrix;
  249. DKMatrix4 modelMatrix;
  250. DKMatrix4 viewMatrix;
  251. } ubo;
  252. DKObject<DKGpuBuffer> uboBuffer = device->CreateBuffer(sizeof(ubo), DKGpuBuffer::StorageModeShared, DKCpuCacheModeReadWrite);
  253. if (uboBuffer)
  254. {
  255. ubo.projectionMatrix = DKMatrix4::identity;
  256. ubo.modelMatrix = DKMatrix4::identity;
  257. ubo.viewMatrix = DKMatrix4::identity;
  258. DKAffineTransform3 trans;
  259. trans.Multiply(DKLinearTransform3().Scale(0.25).Rotate(DKVector3(1,1,1), DKGL_PI * 0.1));
  260. ubo.modelMatrix.Multiply(trans.Matrix4());
  261. memcpy(uboBuffer->Contents(), &ubo, sizeof(ubo));
  262. bindSet->SetBuffer(0, uboBuffer, 0, sizeof(ubo));
  263. uboBuffer->Flush();
  264. }
  265. bindSet->SetTexture(1, texture);
  266. bindSet->SetSamplerState(1, sampler);
  267. }
  268. DKTimer timer;
  269. timer.Reset();
  270. DKLog("Render thread begin");
  271. while (!runningRenderThread.CompareAndSet(0, 0))
  272. {
  273. DKRenderPassDescriptor rpd = swapChain->CurrentRenderPassDescriptor();
  274. double t = timer.Elapsed();
  275. t = (cos(t) + 1.0) * 0.5;
  276. rpd.colorAttachments.Value(0).clearColor = DKColor(t, 0.0, 0.0, 0.0);
  277. DKObject<DKCommandBuffer> buffer = queue->CreateCommandBuffer();
  278. DKObject<DKRenderCommandEncoder> encoder = buffer->CreateRenderCommandEncoder(rpd);
  279. if (encoder)
  280. {
  281. encoder->SetRenderPipelineState(pipelineState);
  282. encoder->SetVertexBuffer(vertexBuffer, 0, 0);
  283. encoder->SetIndexBuffer(indexBuffer, 0, DKIndexType::UInt32);
  284. encoder->SetResources(0, bindSet);
  285. // draw scene!
  286. encoder->DrawIndexed(SampleMesh->GetIndicesCount(), 1, 0, 0, 0);
  287. encoder->EndEncoding();
  288. buffer->Commit();
  289. swapChain->Present();
  290. }
  291. else
  292. {
  293. }
  294. DKThread::Sleep(0.01);
  295. }
  296. DKLog("RenderThread terminating...");
  297. }
  298. void OnInitialize(void) override
  299. {
  300. SampleApp::OnInitialize();
  301. DKLogD("%s", DKGL_FUNCTION_NAME);
  302. // create window
  303. window = DKWindow::Create("DefaultWindow");
  304. window->SetOrigin({ 0, 0 });
  305. window->Resize({ 320, 240 });
  306. window->Activate();
  307. window->AddEventHandler(this, DKFunction([this](const DKWindow::WindowEvent& e)
  308. {
  309. if (e.type == DKWindow::WindowEvent::WindowClosed)
  310. DKApplication::Instance()->Terminate(0);
  311. }), NULL, NULL);
  312. SampleMesh = DKOBJECT_NEW SampleObjMesh();
  313. LoadMesh();
  314. runningRenderThread = 1;
  315. renderThread = DKThread::Create(DKFunction(this, &MeshDemo::RenderThread)->Invocation());
  316. }
  317. void OnTerminate(void) override
  318. {
  319. DKLogD("%s", DKGL_FUNCTION_NAME);
  320. runningRenderThread = 0;
  321. renderThread->WaitTerminate();
  322. renderThread = NULL;
  323. window = NULL;
  324. SampleApp::OnTerminate();
  325. }
  326. };
  327. #ifdef _WIN32
  328. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  329. _In_opt_ HINSTANCE hPrevInstance,
  330. _In_ LPWSTR lpCmdLine,
  331. _In_ int nCmdShow)
  332. #else
  333. int main(int argc, const char * argv[])
  334. #endif
  335. {
  336. MeshDemo app;
  337. DKPropertySet::SystemConfig().SetValue("AppDelegate", "AppDelegate");
  338. DKPropertySet::SystemConfig().SetValue("GraphicsAPI", "Vulkan");
  339. return app.Run();
  340. }