瀏覽代碼

no message

Hongtae Kim 5 年之前
父節點
當前提交
b9b1f220cf
共有 1 個檔案被更改,包括 24 行新增3 行删除
  1. 24
    3
      Samples/Texture/Texture.cpp

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

10
 	DKAtomicNumber32 runningRenderThread;
10
 	DKAtomicNumber32 runningRenderThread;
11
 
11
 
12
 public:
12
 public:
13
-    DKObject<DKTexture> LoadTexture2D(DKGraphicsDevice* device, DKData* data)
13
+    DKObject<DKTexture> LoadTexture2D(DKCommandQueue* queue, DKData* data)
14
     {
14
     {
15
         DKObject<DKImage> image = DKImage::Create(data);
15
         DKObject<DKImage> image = DKImage::Create(data);
16
         if (image)
16
         if (image)
17
         {
17
         {
18
+            DKGraphicsDevice* device = queue->Device();
18
             DKTextureDescriptor texDesc = {};
19
             DKTextureDescriptor texDesc = {};
19
             texDesc.textureType = DKTexture::Type2D;
20
             texDesc.textureType = DKTexture::Type2D;
20
             texDesc.pixelFormat = DKPixelFormat::RGBA8Unorm;
21
             texDesc.pixelFormat = DKPixelFormat::RGBA8Unorm;
28
             DKObject<DKTexture> tex = device->CreateTexture(texDesc);
29
             DKObject<DKTexture> tex = device->CreateTexture(texDesc);
29
             if (tex)
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
                 DKLog("Texture created!");
52
                 DKLog("Texture created!");
32
             }
53
             }
33
         }
54
         }
41
 		DKShader fragShader(fragData);
62
 		DKShader fragShader(fragData);
42
 
63
 
43
 		DKObject<DKGraphicsDevice> device = DKGraphicsDevice::SharedInstance();
64
 		DKObject<DKGraphicsDevice> device = DKGraphicsDevice::SharedInstance();
65
+        DKObject<DKCommandQueue> queue = device->CreateCommandQueue(DKCommandQueue::Graphics);
44
 
66
 
45
         // create texture
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
 		DKObject<DKShaderModule> vertShaderModule = device->CreateShaderModule(&vertShader);
70
 		DKObject<DKShaderModule> vertShaderModule = device->CreateShaderModule(&vertShader);
49
 		DKObject<DKShaderModule> fragShaderModule = device->CreateShaderModule(&fragShader);
71
 		DKObject<DKShaderModule> fragShaderModule = device->CreateShaderModule(&fragShader);
51
 		DKObject<DKShaderFunction> vertShaderFunction = vertShaderModule->CreateFunction(vertShaderModule->FunctionNames().Value(0));
73
 		DKObject<DKShaderFunction> vertShaderFunction = vertShaderModule->CreateFunction(vertShaderModule->FunctionNames().Value(0));
52
 		DKObject<DKShaderFunction> fragShaderFunction = fragShaderModule->CreateFunction(fragShaderModule->FunctionNames().Value(0));
74
 		DKObject<DKShaderFunction> fragShaderFunction = fragShaderModule->CreateFunction(fragShaderModule->FunctionNames().Value(0));
53
 
75
 
54
-		DKObject<DKCommandQueue> queue = device->CreateCommandQueue(DKCommandQueue::Graphics);
55
 		DKObject<DKSwapChain> swapChain = queue->CreateSwapChain(window);
76
 		DKObject<DKSwapChain> swapChain = queue->CreateSwapChain(window);
56
 
77
 
57
 		DKLog("VertexFunction.VertexAttributes: %d", vertShaderFunction->StageInputAttributes().Count());
78
 		DKLog("VertexFunction.VertexAttributes: %d", vertShaderFunction->StageInputAttributes().Count());