Skip to content

Commit a7574e5

Browse files
committed
Refactoring of Buffer
1 parent 33e4f99 commit a7574e5

12 files changed

Lines changed: 314 additions & 266 deletions

.clang-tidy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ readability-*,
4949
-readability-identifier-length,
5050
-readability-magic-numbers,
5151
-readability-math-missing-parentheses,
52+
-readability-redundant-access-specifiers,
5253
-readability-redundant-member-init,
5354
-readability-uppercase-literal-suffix,
5455
-readability-use-std-min-max'

example/helloTriangle.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,14 @@ int main(void)
309309
{
310310
return 4;
311311
}
312+
const LunaPhysicalDevicePreferenceDefinition physicalDevicePreferenceDefinition = {
313+
.preferredDeviceType = VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU,
314+
};
312315
const LunaDeviceCreationInfo deviceCreationInfo = {
313316
.extensionCount = 1,
314317
.extensionNames = (const char *const[]){VK_KHR_SWAPCHAIN_EXTENSION_NAME},
315318
.surface = surface,
319+
.physicalDevicePreferenceDefinition = &physicalDevicePreferenceDefinition,
316320
};
317321
CHECK_RESULT(lunaAddNewDevice(&deviceCreationInfo));
318322

src/Buffer.cpp

Lines changed: 130 additions & 132 deletions
Large diffs are not rendered by default.

src/GraphicsPipeline.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,6 @@ void GraphicsPipeline::destroy()
116116
}
117117
VkResult GraphicsPipeline::bind(const LunaGraphicsPipelineBindInfo &bindInfo) const
118118
{
119-
if (pipeline_ == boundPipeline)
120-
{
121-
return VK_SUCCESS;
122-
}
123119
CommandBuffer &commandBuffer = device.commandPools().graphics.commandBuffer();
124120
CHECK_RESULT_RETURN(commandBuffer.ensureIsRecording(device));
125121
for (uint32_t i = 0; i < bindInfo.dynamicStateCount; i++)
@@ -143,7 +139,10 @@ VkResult GraphicsPipeline::bind(const LunaGraphicsPipelineBindInfo &bindInfo) co
143139
throw std::runtime_error("Unhandled dynamic state type!");
144140
}
145141
}
146-
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_);
142+
if (pipeline_ != boundPipeline)
143+
{
144+
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_);
145+
}
147146
if (bindInfo.descriptorSetBindInfo.descriptorSetCount > 0)
148147
{
149148
std::vector<VkDescriptorSet> descriptorSetsVector;

src/Image.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ VkResult Image::write(const LunaImageWriteInfo &writeInfo) const
202202
CommandBuffer &commandBuffer = device.commandPools().graphics.commandBuffer(1);
203203
CHECK_RESULT_RETURN(commandBuffer.ensureIsRecording(luna::device, true));
204204

205-
const auto *stagingBufferRegionIndex = static_cast<const buffer::BufferRegionIndex *>(stagingBuffer);
205+
const BufferRegionIndex *stagingBufferRegionIndex = stagingBuffer;
206206
if (stagingBufferRegionIndex == nullptr || stagingBufferRegionIndex->size() < writeInfo.bytes)
207207
{
208208
if (stagingBufferRegionIndex != nullptr)
@@ -213,9 +213,9 @@ VkResult Image::write(const LunaImageWriteInfo &writeInfo) const
213213
.size = writeInfo.bytes,
214214
.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
215215
};
216-
LunaBuffer *stagingBufferHandle = &stagingBuffer; // NOLINT(*-const-correctness)
217-
CHECK_RESULT_RETURN(luna::buffer::BufferRegion::createBufferRegion(bufferCreationInfo, &stagingBufferHandle));
218-
stagingBufferRegionIndex = static_cast<const buffer::BufferRegionIndex *>(stagingBuffer);
216+
LunaBuffer stagingBufferHandle = stagingBuffer;
217+
CHECK_RESULT_RETURN(lunaCreateBuffer(&bufferCreationInfo, &stagingBufferHandle));
218+
stagingBufferRegionIndex = stagingBuffer;
219219
}
220220

221221
stagingBufferRegionIndex->bufferRegion()->copyToBuffer(static_cast<const uint8_t *>(writeInfo.pixels),

src/Instance.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <vulkan/vulkan_core.h>
1717
#include "Buffer.hpp"
1818
#include "DescriptorSetLayout.hpp"
19+
#include "Device.hpp"
1920
#include "GraphicsPipeline.hpp"
2021
#include "Image.hpp"
2122
#include "Instance.hpp"
@@ -248,7 +249,7 @@ VkFormat depthImageFormat{};
248249
uint32_t apiVersion{};
249250
VkInstance instance{};
250251
Device device{};
251-
LunaBuffer stagingBuffer{};
252+
const BufferRegionIndex *stagingBuffer{};
252253
VkPipeline boundPipeline{};
253254
LunaBuffer boundVertexBuffer{};
254255
LunaBuffer boundIndexBuffer{};
@@ -260,7 +261,7 @@ std::list<VkDescriptorSet> descriptorSets{};
260261
std::list<DescriptorSetIndex> descriptorSetIndices{};
261262
std::list<GraphicsPipeline> graphicsPipelines{};
262263
std::list<Buffer> buffers{};
263-
std::list<buffer::BufferRegionIndex> bufferRegionIndices{};
264+
std::list<BufferRegionIndex> bufferRegionIndices{};
264265
std::list<VkSampler> samplers{};
265266
std::list<Image> images{};
266267
} // namespace luna

src/Luna.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -249,46 +249,44 @@ void lunaWriteDescriptorSets(const uint32_t writeCount, const LunaWriteDescripto
249249
writes.reserve(writeCount);
250250
for (uint32_t i = 0; i < writeCount; i++)
251251
{
252-
const auto &[descriptorSet,
253-
bindingName,
254-
descriptorArrayElement,
255-
descriptorCount,
256-
imageInfo,
257-
bufferInfo] = descriptorWrites[i];
252+
const LunaWriteDescriptorSet &descriptorWrite = descriptorWrites[i];
253+
const LunaDescriptorSet descriptorSet = descriptorWrite.descriptorSet;
258254
const DescriptorSetIndex *descriptorSetIndex = static_cast<const DescriptorSetIndex *>(descriptorSet);
259-
const DescriptorSetLayout::Binding &binding = descriptorSetIndex->layout->binding(bindingName);
260-
if (imageInfo != nullptr)
255+
const DescriptorSetLayout::Binding &binding = descriptorSetIndex->layout->binding(descriptorWrite.bindingName);
256+
if (descriptorWrite.imageInfo != nullptr)
261257
{
262-
const Image *image = static_cast<const Image *>(imageInfo->image);
258+
const Image *image = static_cast<const Image *>(descriptorWrite.imageInfo->image);
263259
descriptorImageInfo = {
264-
.sampler = image->sampler(imageInfo->sampler),
260+
.sampler = image->sampler(descriptorWrite.imageInfo->sampler),
265261
.imageView = image->imageView(),
266-
.imageLayout = imageInfo->imageLayout,
262+
.imageLayout = descriptorWrite.imageInfo->imageLayout,
267263
};
268264
writes.emplace_back(VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
269265
nullptr,
270266
*descriptorSetIndex->set,
271267
binding.index,
272-
descriptorArrayElement,
273-
descriptorCount,
268+
descriptorWrite.descriptorArrayElement,
269+
descriptorWrite.descriptorCount,
274270
binding.type,
275271
&descriptorImageInfo,
276272
nullptr,
277273
nullptr);
278-
} else if (bufferInfo != nullptr)
274+
} else if (descriptorWrite.bufferInfo != nullptr)
279275
{
280-
const auto *bufferRegionIndex = static_cast<const buffer::BufferRegionIndex *>(bufferInfo->buffer);
276+
const LunaBuffer buffer = descriptorWrite.bufferInfo->buffer;
277+
const BufferRegionIndex *bufferRegionIndex = static_cast<const BufferRegionIndex *>(buffer);
281278
const VkDescriptorBufferInfo descriptorBufferInfo = {
282279
.buffer = *bufferRegionIndex->buffer(),
283-
.offset = bufferInfo->offset + bufferRegionIndex->offset(),
284-
.range = bufferInfo->range == 0 ? bufferRegionIndex->size() : bufferInfo->range,
280+
.offset = descriptorWrite.bufferInfo->offset + bufferRegionIndex->offset(),
281+
.range = descriptorWrite.bufferInfo->range == 0 ? bufferRegionIndex->size()
282+
: descriptorWrite.bufferInfo->range,
285283
};
286284
writes.emplace_back(VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
287285
nullptr,
288286
*descriptorSetIndex->set,
289287
binding.index,
290-
descriptorArrayElement,
291-
descriptorCount,
288+
descriptorWrite.descriptorArrayElement,
289+
descriptorWrite.descriptorCount,
292290
binding.type,
293291
nullptr,
294292
&descriptorBufferInfo,

src/RenderPass.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,6 @@ VkResult lunaBeginRenderPass(const LunaRenderPass renderPass, const LunaRenderPa
577577
commandBuffer.semaphore(),
578578
VK_NULL_HANDLE,
579579
&swapchain.imageIndex);
580-
581580
switch (acquireImageResult)
582581
{
583582
case VK_SUCCESS:

0 commit comments

Comments
 (0)