|
@@ -1,4 +1,4 @@
|
1
|
|
-
|
|
1
|
+#include <cstddef>
|
2
|
2
|
#include "app.h"
|
3
|
3
|
#include "util.h"
|
4
|
4
|
|
|
@@ -10,14 +10,41 @@ class TextureDemo : public SampleApp
|
10
|
10
|
DKAtomicNumber32 runningRenderThread;
|
11
|
11
|
|
12
|
12
|
public:
|
|
13
|
+ DKObject<DKTexture> LoadTexture2D(DKGraphicsDevice* device, DKData* data)
|
|
14
|
+ {
|
|
15
|
+ DKObject<DKImage> image = DKImage::Create(data);
|
|
16
|
+ if (image)
|
|
17
|
+ {
|
|
18
|
+ DKTextureDescriptor texDesc = {};
|
|
19
|
+ texDesc.textureType = DKTexture::Type2D;
|
|
20
|
+ texDesc.pixelFormat = DKPixelFormat::RGBA8Unorm;
|
|
21
|
+ texDesc.width = image->Width();
|
|
22
|
+ texDesc.height = image->Height();
|
|
23
|
+ texDesc.depth = 1;
|
|
24
|
+ texDesc.mipmapLevels = 1;
|
|
25
|
+ texDesc.sampleCount = 1;
|
|
26
|
+ texDesc.arrayLength = 1;
|
|
27
|
+ texDesc.usage = DKTexture::UsageCopyDestination | DKTexture::UsageSampled;
|
|
28
|
+ DKObject<DKTexture> tex = device->CreateTexture(texDesc);
|
|
29
|
+ if (tex)
|
|
30
|
+ {
|
|
31
|
+ DKLog("Texture created!");
|
|
32
|
+ }
|
|
33
|
+ }
|
|
34
|
+ return nullptr;
|
|
35
|
+ }
|
13
|
36
|
void RenderThread(void)
|
14
|
37
|
{
|
15
|
|
- DKObject<DKData> vertData = resourcePool.LoadResourceData("triangle.vert.spv");
|
16
|
|
- DKObject<DKData> fragData = resourcePool.LoadResourceData("triangle.frag.spv");
|
|
38
|
+ DKObject<DKData> vertData = resourcePool.LoadResourceData("shaders/texture.vert.spv");
|
|
39
|
+ DKObject<DKData> fragData = resourcePool.LoadResourceData("shaders/texture.frag.spv");
|
17
|
40
|
DKShader vertShader(vertData);
|
18
|
41
|
DKShader fragShader(fragData);
|
19
|
42
|
|
20
|
43
|
DKObject<DKGraphicsDevice> device = DKGraphicsDevice::SharedInstance();
|
|
44
|
+
|
|
45
|
+ // create texture
|
|
46
|
+ DKObject<DKTexture> texture = LoadTexture2D(device, resourcePool.LoadResourceData("textures/deathstar3.png"));
|
|
47
|
+
|
21
|
48
|
DKObject<DKShaderModule> vertShaderModule = device->CreateShaderModule(&vertShader);
|
22
|
49
|
DKObject<DKShaderModule> fragShaderModule = device->CreateShaderModule(&fragShader);
|
23
|
50
|
|
|
@@ -36,17 +63,19 @@ public:
|
36
|
63
|
|
37
|
64
|
struct Vertex
|
38
|
65
|
{
|
39
|
|
- DKVector3 position;
|
40
|
|
- DKVector3 color;
|
|
66
|
+ DKVector3 inPos;
|
|
67
|
+ DKVector2 inUV;
|
|
68
|
+ DKVector3 inNormal;
|
41
|
69
|
};
|
42
|
70
|
DKArray<Vertex> vertexData =
|
43
|
71
|
{
|
44
|
|
- { { 0.0f, -0.5f, 0.0f },{ 1.0f, 1.0f, 1.0f } },
|
45
|
|
- { { 0.5f, 0.5f, 0.0f },{ 0.0f, 1.0f, 0.0f } },
|
46
|
|
- { { -0.5f, 0.5f, 0.0f },{ 0.0f, 0.0f, 1.0f } }
|
|
72
|
+ { { 1.0f, 1.0f, 0.0f }, { 1.0f, 1.0f }, { 0.0f, 0.0f, 1.0f } },
|
|
73
|
+ { { -1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f }, { 0.0f, 0.0f, 1.0f } },
|
|
74
|
+ { { -1.0f, -1.0f, 0.0f }, { 0.0f, 0.0f }, { 0.0f, 0.0f, 1.0f } },
|
|
75
|
+ { { 1.0f, -1.0f, 0.0f }, { 1.0f, 0.0f }, { 0.0f, 0.0f, 1.0f } }
|
47
|
76
|
};
|
48
|
77
|
uint32_t vertexBufferSize = static_cast<uint32_t>(vertexData.Count()) * sizeof(Vertex);
|
49
|
|
- DKArray<uint32_t> indexData = { 0, 1, 2 };
|
|
78
|
+ DKArray<uint32_t> indexData = { 0, 1, 2, 2, 3, 0 };
|
50
|
79
|
uint32_t indexBufferSize = indexData.Count() * sizeof(uint32_t);
|
51
|
80
|
|
52
|
81
|
DKObject<DKGpuBuffer> vertexBuffer = device->CreateBuffer(vertexBufferSize, DKGpuBuffer::StorageModeShared, DKCpuCacheModeDefault);
|
|
@@ -65,7 +94,8 @@ public:
|
65
|
94
|
pipelineDescriptor.depthStencilAttachmentPixelFormat = DKPixelFormat::Invalid; // no depth buffer
|
66
|
95
|
pipelineDescriptor.vertexDescriptor.attributes = {
|
67
|
96
|
{ DKVertexFormat::Float3, 0, 0, 0 },
|
68
|
|
- { DKVertexFormat::Float3, sizeof(DKVector3), 0, 1 },
|
|
97
|
+ { DKVertexFormat::Float2, offsetof(Vertex, inUV), 0, 1 },
|
|
98
|
+ { DKVertexFormat::Float3, offsetof(Vertex, inNormal), 0, 2 },
|
69
|
99
|
};
|
70
|
100
|
pipelineDescriptor.vertexDescriptor.layouts = {
|
71
|
101
|
{ DKVertexStepRate::Vertex, sizeof(Vertex), 0 },
|