浏览代码

no message

Hongtae Kim 5 年前
父节点
当前提交
b9b1f220cf
共有 1 个文件被更改,包括 24 次插入3 次删除
  1. 24
    3
      Samples/Texture/Texture.cpp

+ 24
- 3
Samples/Texture/Texture.cpp 查看文件

@@ -10,11 +10,12 @@ class TextureDemo : public SampleApp
10 10
 	DKAtomicNumber32 runningRenderThread;
11 11
 
12 12
 public:
13
-    DKObject<DKTexture> LoadTexture2D(DKGraphicsDevice* device, DKData* data)
13
+    DKObject<DKTexture> LoadTexture2D(DKCommandQueue* queue, DKData* data)
14 14
     {
15 15
         DKObject<DKImage> image = DKImage::Create(data);
16 16
         if (image)
17 17
         {
18
+            DKGraphicsDevice* device = queue->Device();
18 19
             DKTextureDescriptor texDesc = {};
19 20
             texDesc.textureType = DKTexture::Type2D;
20 21
             texDesc.pixelFormat = DKPixelFormat::RGBA8Unorm;
@@ -28,6 +29,26 @@ public:
28 29
             DKObject<DKTexture> tex = device->CreateTexture(texDesc);
29 30
             if (tex)
30 31
             {
32
+                size_t bytesPerPixel = image->BytesPerPixel();
33
+                uint32_t width = image->Width();
34
+                uint32_t height = image->Height();
35
+
36
+                size_t bufferLength = bytesPerPixel * width * height;
37
+                DKObject<DKGpuBuffer> stagingBuffer = device->CreateBuffer(bufferLength, DKGpuBuffer::StorageModeShared, DKCpuCacheModeReadWrite);
38
+                
39
+                memcpy(stagingBuffer->Contents(), image->Contents(), bufferLength);
40
+                stagingBuffer->Flush();
41
+
42
+                DKObject<DKCommandBuffer> cb = queue->CreateCommandBuffer();
43
+                DKObject<DKCopyCommandEncoder> encoder = cb->CreateCopyCommandEncoder();
44
+                encoder->CopyFromBufferToTexture(stagingBuffer,
45
+                                                 { 0, uint32_t(bytesPerPixel * width), height },
46
+                                                 tex,
47
+                                                 { 0,0, 0,0,0 },
48
+                                                 { width,height,1 });
49
+                encoder->EndEncoding();
50
+                cb->Commit();
51
+
31 52
                 DKLog("Texture created!");
32 53
             }
33 54
         }
@@ -41,9 +62,10 @@ public:
41 62
 		DKShader fragShader(fragData);
42 63
 
43 64
 		DKObject<DKGraphicsDevice> device = DKGraphicsDevice::SharedInstance();
65
+        DKObject<DKCommandQueue> queue = device->CreateCommandQueue(DKCommandQueue::Graphics);
44 66
 
45 67
         // create texture
46
-        DKObject<DKTexture> texture = LoadTexture2D(device, resourcePool.LoadResourceData("textures/deathstar3.png"));
68
+        DKObject<DKTexture> texture = LoadTexture2D(queue, resourcePool.LoadResourceData("textures/deathstar3.png"));
47 69
 
48 70
 		DKObject<DKShaderModule> vertShaderModule = device->CreateShaderModule(&vertShader);
49 71
 		DKObject<DKShaderModule> fragShaderModule = device->CreateShaderModule(&fragShader);
@@ -51,7 +73,6 @@ public:
51 73
 		DKObject<DKShaderFunction> vertShaderFunction = vertShaderModule->CreateFunction(vertShaderModule->FunctionNames().Value(0));
52 74
 		DKObject<DKShaderFunction> fragShaderFunction = fragShaderModule->CreateFunction(fragShaderModule->FunctionNames().Value(0));
53 75
 
54
-		DKObject<DKCommandQueue> queue = device->CreateCommandQueue(DKCommandQueue::Graphics);
55 76
 		DKObject<DKSwapChain> swapChain = queue->CreateSwapChain(window);
56 77
 
57 78
 		DKLog("VertexFunction.VertexAttributes: %d", vertShaderFunction->StageInputAttributes().Count());