Add sample RayTracing. (#361)
This commit is contained in:
committed by
Markus Tavenrath
parent
d5d4f3457b
commit
ecfaf9a0df
@@ -33,6 +33,14 @@ namespace vk
|
||||
case vk::ShaderStageFlagBits::eGeometry: return EShLangGeometry;
|
||||
case vk::ShaderStageFlagBits::eFragment: return EShLangFragment;
|
||||
case vk::ShaderStageFlagBits::eCompute: return EShLangCompute;
|
||||
case vk::ShaderStageFlagBits::eRaygenNV: return EShLangRayGenNV;
|
||||
case vk::ShaderStageFlagBits::eAnyHitNV: return EShLangAnyHitNV;
|
||||
case vk::ShaderStageFlagBits::eClosestHitNV: return EShLangClosestHitNV;
|
||||
case vk::ShaderStageFlagBits::eMissNV: return EShLangMissNV;
|
||||
case vk::ShaderStageFlagBits::eIntersectionNV: return EShLangIntersectNV;
|
||||
case vk::ShaderStageFlagBits::eCallableNV: return EShLangCallableNV;
|
||||
case vk::ShaderStageFlagBits::eTaskNV: return EShLangTaskNV;
|
||||
case vk::ShaderStageFlagBits::eMeshNV: return EShLangMeshNV;
|
||||
default:
|
||||
assert(false && "Unknown shader stage");
|
||||
return EShLangVertex;
|
||||
|
||||
@@ -537,14 +537,15 @@ namespace vk
|
||||
}
|
||||
|
||||
void updateDescriptorSets(vk::UniqueDevice const& device, vk::UniqueDescriptorSet const& descriptorSet,
|
||||
std::vector<std::tuple<vk::DescriptorType, vk::UniqueBuffer const&, vk::UniqueBufferView const&>> const& bufferData, vk::su::TextureData const& textureData)
|
||||
std::vector<std::tuple<vk::DescriptorType, vk::UniqueBuffer const&, vk::UniqueBufferView const&>> const& bufferData, vk::su::TextureData const& textureData,
|
||||
uint32_t bindingOffset)
|
||||
{
|
||||
std::vector<vk::DescriptorBufferInfo> bufferInfos;
|
||||
bufferInfos.reserve(bufferData.size());
|
||||
|
||||
std::vector<vk::WriteDescriptorSet> writeDescriptorSets;
|
||||
writeDescriptorSets.reserve(bufferData.size() + 1);
|
||||
uint32_t dstBinding = 0;
|
||||
uint32_t dstBinding = bindingOffset;
|
||||
for (auto const& bd : bufferData)
|
||||
{
|
||||
bufferInfos.push_back(vk::DescriptorBufferInfo(*std::get<1>(bd), 0, VK_WHOLE_SIZE));
|
||||
@@ -559,14 +560,14 @@ namespace vk
|
||||
|
||||
void updateDescriptorSets(vk::UniqueDevice const& device, vk::UniqueDescriptorSet const& descriptorSet,
|
||||
std::vector<std::tuple<vk::DescriptorType, vk::UniqueBuffer const&, vk::UniqueBufferView const&>> const& bufferData,
|
||||
std::vector<vk::su::TextureData> const& textureData)
|
||||
std::vector<vk::su::TextureData> const& textureData, uint32_t bindingOffset)
|
||||
{
|
||||
std::vector<vk::DescriptorBufferInfo> bufferInfos;
|
||||
bufferInfos.reserve(bufferData.size());
|
||||
|
||||
std::vector<vk::WriteDescriptorSet> writeDescriptorSets;
|
||||
writeDescriptorSets.reserve(bufferData.size() + textureData.empty() ? 0 : 1);
|
||||
uint32_t dstBinding = 0;
|
||||
uint32_t dstBinding = bindingOffset;
|
||||
for (auto const& bd : bufferData)
|
||||
{
|
||||
bufferInfos.push_back(vk::DescriptorBufferInfo(*std::get<1>(bd), 0, VK_WHOLE_SIZE));
|
||||
@@ -684,18 +685,23 @@ namespace vk
|
||||
}
|
||||
}
|
||||
|
||||
CheckerboardImageGenerator::CheckerboardImageGenerator(std::array<uint8_t, 3> const& rgb0, std::array<uint8_t, 3> const& rgb1)
|
||||
: m_rgb0(rgb0)
|
||||
, m_rgb1(rgb1)
|
||||
{}
|
||||
|
||||
void CheckerboardImageGenerator::operator()(void* data, vk::Extent2D &extent) const
|
||||
{
|
||||
// Checkerboard of 16x16 pixel squares
|
||||
unsigned char *pImageMemory = static_cast<unsigned char*>(data);
|
||||
uint8_t *pImageMemory = static_cast<uint8_t *>(data);
|
||||
for (uint32_t row = 0; row < extent.height; row++)
|
||||
{
|
||||
for (uint32_t col = 0; col < extent.width; col++)
|
||||
{
|
||||
unsigned char rgb = (((row & 0x10) == 0) ^ ((col & 0x10) == 0)) * 255;
|
||||
pImageMemory[0] = rgb;
|
||||
pImageMemory[1] = rgb;
|
||||
pImageMemory[2] = rgb;
|
||||
std::array<uint8_t, 3> const& rgb = (((row & 0x10) == 0) ^ ((col & 0x10) == 0)) ? m_rgb1 : m_rgb0;
|
||||
pImageMemory[0] = rgb[0];
|
||||
pImageMemory[1] = rgb[1];
|
||||
pImageMemory[2] = rgb[2];
|
||||
pImageMemory[3] = 255;
|
||||
pImageMemory += 4;
|
||||
}
|
||||
|
||||
@@ -42,34 +42,34 @@ namespace vk
|
||||
}
|
||||
|
||||
template <typename DataType>
|
||||
void upload(vk::UniqueDevice const& device, std::vector<DataType> const& data) const
|
||||
void upload(vk::UniqueDevice const& device, std::vector<DataType> const& data, size_t stride = 0) const
|
||||
{
|
||||
assert((m_propertyFlags & vk::MemoryPropertyFlagBits::eHostCoherent) && (m_propertyFlags & vk::MemoryPropertyFlagBits::eHostVisible));
|
||||
assert(m_propertyFlags & vk::MemoryPropertyFlagBits::eHostVisible);
|
||||
|
||||
size_t dataSize = data.size() * sizeof(DataType);
|
||||
assert(dataSize <= m_size);
|
||||
size_t elementSize = stride ? stride : sizeof(DataType);
|
||||
assert(sizeof(DataType) <= elementSize);
|
||||
|
||||
void* dataPtr = device->mapMemory(*this->deviceMemory, 0, dataSize);
|
||||
memcpy(dataPtr, data.data(), dataSize);
|
||||
device->unmapMemory(*this->deviceMemory);
|
||||
copyToDevice(device, deviceMemory, data.data(), data.size(), elementSize);
|
||||
}
|
||||
|
||||
template <typename DataType>
|
||||
void upload(vk::PhysicalDevice const& physicalDevice, vk::UniqueDevice const& device, vk::UniqueCommandPool const& commandPool, vk::Queue queue, std::vector<DataType> const& data) const
|
||||
void upload(vk::PhysicalDevice const& physicalDevice, vk::UniqueDevice const& device, vk::UniqueCommandPool const& commandPool, vk::Queue queue, std::vector<DataType> const& data,
|
||||
size_t stride) const
|
||||
{
|
||||
assert(m_usage & vk::BufferUsageFlagBits::eTransferDst);
|
||||
assert(m_propertyFlags & vk::MemoryPropertyFlagBits::eDeviceLocal);
|
||||
|
||||
size_t dataSize = data.size() * sizeof(DataType);
|
||||
size_t elementSize = stride ? stride : sizeof(DataType);
|
||||
assert(sizeof(DataType) <= elementSize);
|
||||
|
||||
size_t dataSize = data.size() * elementSize;
|
||||
assert(dataSize <= m_size);
|
||||
|
||||
vk::su::BufferData stagingBuffer(physicalDevice, device, dataSize, vk::BufferUsageFlagBits::eTransferSrc);
|
||||
void* dataPtr = device->mapMemory(*stagingBuffer.deviceMemory, 0, dataSize);
|
||||
memcpy(dataPtr, data.data(), dataSize);
|
||||
device->unmapMemory(*stagingBuffer.deviceMemory);
|
||||
copyToDevice(device, stagingBuffer.deviceMemory, data.data(), data.size(), elementSize);
|
||||
|
||||
vk::UniqueCommandBuffer commandBuffer = std::move(device->allocateCommandBuffersUnique(vk::CommandBufferAllocateInfo(*commandPool, vk::CommandBufferLevel::ePrimary, 1)).front());
|
||||
vk::su::oneTimeSubmit(commandBuffer, queue, [&]() { commandBuffer->copyBuffer(*stagingBuffer.buffer, *this->buffer, vk::BufferCopy(0, 0, dataSize)); });
|
||||
vk::su::oneTimeSubmit(device, commandPool, queue,
|
||||
[&](vk::UniqueCommandBuffer const& commandBuffer) { commandBuffer->copyBuffer(*stagingBuffer.buffer, *this->buffer, vk::BufferCopy(0, 0, dataSize)); });
|
||||
}
|
||||
|
||||
vk::UniqueBuffer buffer;
|
||||
@@ -120,8 +120,14 @@ namespace vk
|
||||
|
||||
class CheckerboardImageGenerator
|
||||
{
|
||||
public:
|
||||
public:
|
||||
CheckerboardImageGenerator(std::array<uint8_t, 3> const& rgb0 = {0, 0, 0}, std::array<uint8_t, 3> const& rgb1 = {255, 255, 255});
|
||||
|
||||
void operator()(void* data, vk::Extent2D &extent) const;
|
||||
|
||||
private:
|
||||
std::array<uint8_t, 3> const& m_rgb0;
|
||||
std::array<uint8_t, 3> const& m_rgb1;
|
||||
};
|
||||
|
||||
class MonochromeImageGenerator
|
||||
@@ -207,7 +213,7 @@ namespace vk
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void copyToDevice(vk::UniqueDevice &device, vk::UniqueDeviceMemory &memory, T const* pData, size_t count, size_t stride = sizeof(T))
|
||||
void copyToDevice(vk::UniqueDevice const& device, vk::UniqueDeviceMemory const& memory, T const* pData, size_t count, size_t stride = sizeof(T))
|
||||
{
|
||||
assert(sizeof(T) <= stride);
|
||||
uint8_t* deviceData = static_cast<uint8_t*>(device->mapMemory(memory.get(), 0, count * stride));
|
||||
@@ -227,7 +233,7 @@ namespace vk
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void copyToDevice(vk::UniqueDevice &device, vk::UniqueDeviceMemory &memory, T const& data)
|
||||
void copyToDevice(vk::UniqueDevice const& device, vk::UniqueDeviceMemory const& memory, T const& data)
|
||||
{
|
||||
copyToDevice<T>(device, memory, &data, 1);
|
||||
}
|
||||
@@ -238,16 +244,23 @@ namespace vk
|
||||
return v < lo ? lo : hi < v ? hi : v;
|
||||
}
|
||||
|
||||
template <typename Func, typename... Args>
|
||||
void oneTimeSubmit(vk::UniqueCommandBuffer const& commandBuffer, vk::Queue const& queue, Func const& func, Args... args)
|
||||
template <typename Func>
|
||||
void oneTimeSubmit(vk::UniqueCommandBuffer const& commandBuffer, vk::Queue const& queue, Func const& func)
|
||||
{
|
||||
commandBuffer->begin(vk::CommandBufferBeginInfo(vk::CommandBufferUsageFlagBits::eOneTimeSubmit));
|
||||
func(args...);
|
||||
func(commandBuffer);
|
||||
commandBuffer->end();
|
||||
queue.submit(vk::SubmitInfo(0, nullptr, nullptr, 1, &(*commandBuffer)), nullptr);
|
||||
queue.waitIdle();
|
||||
}
|
||||
|
||||
template <typename Func>
|
||||
void oneTimeSubmit(vk::UniqueDevice const& device, vk::UniqueCommandPool const& commandPool, vk::Queue const& queue, Func const& func)
|
||||
{
|
||||
vk::UniqueCommandBuffer commandBuffer = std::move(device->allocateCommandBuffersUnique(vk::CommandBufferAllocateInfo(*commandPool, vk::CommandBufferLevel::ePrimary, 1)).front());
|
||||
oneTimeSubmit(commandBuffer, queue, func);
|
||||
}
|
||||
|
||||
vk::UniqueDeviceMemory allocateMemory(vk::UniqueDevice const& device, vk::PhysicalDeviceMemoryProperties const& memoryProperties, vk::MemoryRequirements const& memoryRequirements,
|
||||
vk::MemoryPropertyFlags memoryPropertyFlags);
|
||||
vk::UniqueCommandPool createCommandPool(vk::UniqueDevice &device, uint32_t queueFamilyIndex);
|
||||
@@ -277,10 +290,11 @@ namespace vk
|
||||
void setImageLayout(vk::UniqueCommandBuffer const& commandBuffer, vk::Image image, vk::Format format, vk::ImageLayout oldImageLayout, vk::ImageLayout newImageLayout);
|
||||
void submitAndWait(vk::UniqueDevice &device, vk::Queue queue, vk::UniqueCommandBuffer &commandBuffer);
|
||||
void updateDescriptorSets(vk::UniqueDevice const& device, vk::UniqueDescriptorSet const& descriptorSet,
|
||||
std::vector<std::tuple<vk::DescriptorType, vk::UniqueBuffer const&, vk::UniqueBufferView const&>> const& bufferData, vk::su::TextureData const& textureData);
|
||||
std::vector<std::tuple<vk::DescriptorType, vk::UniqueBuffer const&, vk::UniqueBufferView const&>> const& bufferData, vk::su::TextureData const& textureData,
|
||||
uint32_t bindingOffset = 0);
|
||||
void updateDescriptorSets(vk::UniqueDevice const& device, vk::UniqueDescriptorSet const& descriptorSet,
|
||||
std::vector<std::tuple<vk::DescriptorType, vk::UniqueBuffer const&, vk::UniqueBufferView const&>> const& bufferData,
|
||||
std::vector<vk::su::TextureData> const& textureData);
|
||||
std::vector<vk::su::TextureData> const& textureData, uint32_t bindingOffset = 0);
|
||||
|
||||
#if defined(VK_USE_PLATFORM_WIN32_KHR)
|
||||
HWND initializeWindow(std::string const& className, std::string const& windowName, LONG width, LONG height);
|
||||
|
||||
Reference in New Issue
Block a user