OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrVkUniformBuffer.h" | 8 #include "GrVkUniformBuffer.h" |
9 #include "GrVkGpu.h" | 9 #include "GrVkGpu.h" |
10 | 10 |
| 11 #define VK_CALL(GPU, X) GR_VK_CALL(GPU->vkInterface(), X) |
11 | 12 |
12 GrVkUniformBuffer* GrVkUniformBuffer::Create(GrVkGpu* gpu, size_t size, bool dyn
amic) { | 13 GrVkUniformBuffer* GrVkUniformBuffer::Create(GrVkGpu* gpu, size_t size) { |
13 if (0 == size) { | 14 if (0 == size) { |
14 return nullptr; | 15 return nullptr; |
15 } | 16 } |
16 GrVkBuffer::Desc desc; | 17 const GrVkResource* resource = nullptr; |
17 desc.fDynamic = dynamic; | 18 if (size <= GrVkUniformBuffer::kStandardSize) { |
18 desc.fType = GrVkBuffer::kUniform_Type; | 19 resource = gpu->resourceProvider().findOrCreateStandardUniformBufferReso
urce(); |
19 desc.fSizeInBytes = size; | 20 } else { |
20 | 21 resource = CreateResource(gpu, size); |
21 const GrVkBuffer::Resource* bufferResource = GrVkBuffer::Create(gpu, desc); | 22 } |
22 if (!bufferResource) { | 23 if (!resource) { |
23 return nullptr; | 24 return nullptr; |
24 } | 25 } |
25 | 26 |
26 GrVkUniformBuffer* buffer = new GrVkUniformBuffer(desc, bufferResource); | 27 GrVkBuffer::Desc desc; |
| 28 desc.fDynamic = true; |
| 29 desc.fType = GrVkBuffer::kUniform_Type; |
| 30 desc.fSizeInBytes = size; |
| 31 GrVkUniformBuffer* buffer = new GrVkUniformBuffer(gpu, desc, |
| 32 (const GrVkUniformBuffer::
Resource*) resource); |
27 if (!buffer) { | 33 if (!buffer) { |
28 bufferResource->unref(gpu); | 34 // this will destroy anything we got from the resource provider, |
| 35 // but this avoids a conditional |
| 36 resource->unref(gpu); |
29 } | 37 } |
30 return buffer; | 38 return buffer; |
31 } | 39 } |
| 40 |
| 41 // We implement our own creation function for special buffer resource type |
| 42 const GrVkResource* GrVkUniformBuffer::CreateResource(GrVkGpu* gpu, size_t size)
{ |
| 43 if (0 == size) { |
| 44 return nullptr; |
| 45 } |
| 46 |
| 47 VkBuffer buffer; |
| 48 GrVkAlloc alloc; |
| 49 |
| 50 // create the buffer object |
| 51 VkBufferCreateInfo bufInfo; |
| 52 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo)); |
| 53 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
| 54 bufInfo.flags = 0; |
| 55 bufInfo.size = size; |
| 56 bufInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; |
| 57 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 58 bufInfo.queueFamilyIndexCount = 0; |
| 59 bufInfo.pQueueFamilyIndices = nullptr; |
| 60 |
| 61 VkResult err; |
| 62 err = VK_CALL(gpu, CreateBuffer(gpu->device(), &bufInfo, nullptr, &buffer)); |
| 63 if (err) { |
| 64 return nullptr; |
| 65 } |
| 66 |
| 67 if (!GrVkMemory::AllocAndBindBufferMemory(gpu, |
| 68 buffer, |
| 69 kUniform_Type, |
| 70 true, // dynamic |
| 71 &alloc)) { |
| 72 return nullptr; |
| 73 } |
| 74 |
| 75 const GrVkResource* resource = new GrVkUniformBuffer::Resource(buffer, alloc
); |
| 76 if (!resource) { |
| 77 VK_CALL(gpu, DestroyBuffer(gpu->device(), buffer, nullptr)); |
| 78 GrVkMemory::FreeBufferMemory(gpu, kUniform_Type, alloc); |
| 79 return nullptr; |
| 80 } |
| 81 |
| 82 return resource; |
| 83 } |
| 84 |
| 85 void GrVkUniformBuffer::Resource::onRecycle(GrVkGpu* gpu) const { |
| 86 if (fAlloc.fSize <= GrVkUniformBuffer::kStandardSize) { |
| 87 gpu->resourceProvider().recycleStandardUniformBufferResource(this); |
| 88 } else { |
| 89 this->unref(gpu); |
| 90 } |
| 91 } |
OLD | NEW |