Skip to content

Commit

Permalink
Bundled shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
shg8 committed Feb 21, 2024
1 parent 571c0cc commit ef16ef8
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 25 deletions.
5 changes: 4 additions & 1 deletion 3dgs/GSScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "GSScene.h"

#include <random>
#include "shaders.h"

#include "../vulkan/Utils.h"
#include "../vulkan/DescriptorSet.h"
Expand Down Expand Up @@ -155,7 +156,9 @@ std::shared_ptr<Buffer> GSScene::createBuffer(const std::shared_ptr<VulkanContex
void GSScene::precomputeCov3D(const std::shared_ptr<VulkanContext>&context) {
cov3DBuffer = createBuffer(context, header.numVertices * sizeof(float) * 6);

auto pipeline = std::make_shared<ComputePipeline>(context, "precomp_cov3d");
auto pipeline = std::make_shared<ComputePipeline>(
context, std::make_shared<Shader>(context, "precomp_cov3d", SPV_PRECOMP_COV3D, SPV_PRECOMP_COV3D_len));

auto descriptorSet = std::make_shared<DescriptorSet>(context, FRAMES_IN_FLIGHT);
descriptorSet->bindBufferToDescriptorSet(0, vk::DescriptorType::eStorageBuffer, vk::ShaderStageFlagBits::eCompute,
vertexBuffer);
Expand Down
15 changes: 8 additions & 7 deletions 3dgs/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "../vulkan/Swapchain.h"

#include <memory>
#include <shaders.h>
#include <utility>

#include <glm/glm.hpp>
Expand Down Expand Up @@ -129,7 +130,7 @@ void Renderer::createPreprocessPipeline() {
vertexAttributeBuffer = Buffer::storage(context, scene->getNumVertices() * sizeof(VertexAttributeBuffer), false);
tileOverlapBuffer = Buffer::storage(context, scene->getNumVertices() * sizeof(uint32_t), false);

preprocessPipeline = std::make_shared<ComputePipeline>(context, "preprocess");
preprocessPipeline = std::make_shared<ComputePipeline>(context, std::make_shared<Shader>(context, "preprocess", SPV_PREPROCESS, SPV_PREPROCESS_len));
inputSet = std::make_shared<DescriptorSet>(context, FRAMES_IN_FLIGHT);
inputSet->bindBufferToDescriptorSet(0, vk::DescriptorType::eStorageBuffer, vk::ShaderStageFlagBits::eCompute,
scene->vertexBuffer);
Expand Down Expand Up @@ -174,7 +175,7 @@ void Renderer::createPrefixSumPipeline() {
prefixSumPongBuffer = Buffer::storage(context, scene->getNumVertices() * sizeof(uint32_t), false);
totalSumBufferHost = Buffer::staging(context, sizeof(uint32_t));

prefixSumPipeline = std::make_shared<ComputePipeline>(context, "prefix_sum");
prefixSumPipeline = std::make_shared<ComputePipeline>(context, std::make_shared<Shader>(context, "prefix_sum", SPV_PREFIX_SUM, SPV_PREFIX_SUM_len));
auto descriptorSet = std::make_shared<DescriptorSet>(context, FRAMES_IN_FLIGHT);
descriptorSet->bindBufferToDescriptorSet(0, vk::DescriptorType::eStorageBuffer, vk::ShaderStageFlagBits::eCompute,
prefixSumPingBuffer);
Expand Down Expand Up @@ -206,8 +207,8 @@ void Renderer::createRadixSortPipeline() {

sortHistBuffer = Buffer::storage(context, numWorkgroups * 256 * sizeof(uint32_t), false);

sortHistPipeline = std::make_shared<ComputePipeline>(context, "hist");
sortPipeline = std::make_shared<ComputePipeline>(context, "sort");
sortHistPipeline = std::make_shared<ComputePipeline>(context, std::make_shared<Shader>(context, "hist", SPV_HIST, SPV_HIST_len));
sortPipeline = std::make_shared<ComputePipeline>(context, std::make_shared<Shader>(context, "sort", SPV_SORT, SPV_SORT_len));

auto descriptorSet = std::make_shared<DescriptorSet>(context, FRAMES_IN_FLIGHT);
descriptorSet->bindBufferToDescriptorSet(0, vk::DescriptorType::eStorageBuffer, vk::ShaderStageFlagBits::eCompute,
Expand Down Expand Up @@ -248,7 +249,7 @@ void Renderer::createRadixSortPipeline() {

void Renderer::createPreprocessSortPipeline() {
spdlog::debug("Creating preprocess sort pipeline");
preprocessSortPipeline = std::make_shared<ComputePipeline>(context, "preprocess_sort");
preprocessSortPipeline = std::make_shared<ComputePipeline>(context, std::make_shared<Shader>(context, "preprocess_sort", SPV_PREPROCESS_SORT, SPV_PREPROCESS_SORT_len));
auto descriptorSet = std::make_shared<DescriptorSet>(context, FRAMES_IN_FLIGHT);
descriptorSet->bindBufferToDescriptorSet(0, vk::DescriptorType::eStorageBuffer, vk::ShaderStageFlagBits::eCompute,
vertexAttributeBuffer);
Expand All @@ -274,7 +275,7 @@ void Renderer::createTileBoundaryPipeline() {
auto tileY = (height + 16 - 1) / 16;
tileBoundaryBuffer = Buffer::storage(context, tileX * tileY * sizeof(uint32_t) * 2, false);

tileBoundaryPipeline = std::make_shared<ComputePipeline>(context, "tile_boundary");
tileBoundaryPipeline = std::make_shared<ComputePipeline>(context, std::make_shared<Shader>(context, "tile_boundary", SPV_TILE_BOUNDARY, SPV_TILE_BOUNDARY_len));
auto descriptorSet = std::make_shared<DescriptorSet>(context, FRAMES_IN_FLIGHT);
descriptorSet->bindBufferToDescriptorSet(0, vk::DescriptorType::eStorageBuffer, vk::ShaderStageFlagBits::eCompute,
sortKBufferEven);
Expand All @@ -291,7 +292,7 @@ void Renderer::createTileBoundaryPipeline() {

void Renderer::createRenderPipeline() {
spdlog::debug("Creating render pipeline");
renderPipeline = std::make_shared<ComputePipeline>(context, "render");
renderPipeline = std::make_shared<ComputePipeline>(context, std::make_shared<Shader>(context, "render", SPV_RENDER, SPV_RENDER_len));
auto inputSet = std::make_shared<DescriptorSet>(context, FRAMES_IN_FLIGHT);
inputSet->bindBufferToDescriptorSet(0, vk::DescriptorType::eStorageBuffer, vk::ShaderStageFlagBits::eCompute,
vertexAttributeBuffer);
Expand Down
24 changes: 22 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,41 @@ if (APPLE)
list(APPEND GLSLC_DEFINE "-DAPPLE")
endif ()

set(SHADER_HEADER "${PROJECT_BINARY_DIR}/shaders/shaders.h")
message(STATUS "Shader header file: ${SHADER_HEADER}")

foreach (GLSL ${GLSL_SOURCE_FILES})
get_filename_component(FILE_NAME ${GLSL} NAME)
get_filename_component(FILE_NAME ${GLSL} NAME_WE)
string(TOUPPER ${FILE_NAME} FILE_NAME_UPPER)
set(FILE_NAME_UPPER "SPV_${FILE_NAME_UPPER}")
set(SPIRV "${PROJECT_BINARY_DIR}/shaders/${FILE_NAME}.spv")
SET(TEMP_HEADER "${PROJECT_BINARY_DIR}/shaders/${FILE_NAME}.h")
add_custom_command(
OUTPUT ${SPIRV}
COMMAND ${CMAKE_COMMAND} -E make_directory "${PROJECT_BINARY_DIR}/shaders/"
COMMAND ${Vulkan_GLSLANG_VALIDATOR_EXECUTABLE} "--target-env" "vulkan1.2" -V ${GLSL} -o ${SPIRV} ${GLSLC_DEFINE}
DEPENDS ${GLSL})
list(APPEND SPIRV_BINARY_FILES ${SPIRV})

add_custom_command(
OUTPUT ${TEMP_HEADER}
COMMAND xxd -i -n ${FILE_NAME_UPPER} ${SPIRV} > ${TEMP_HEADER}
DEPENDS ${SPIRV})

list(APPEND TEMP_HEADERS ${TEMP_HEADER})
endforeach (GLSL)

add_custom_command(
OUTPUT ${SHADER_HEADER}
COMMAND cat ${TEMP_HEADERS} > ${SHADER_HEADER}
DEPENDS ${TEMP_HEADERS}
)

add_custom_target(
Shaders
DEPENDS ${SPIRV_BINARY_FILES}
DEPENDS ${SPIRV_BINARY_FILES} ${SHADER_HEADER}
)
include_directories(${PROJECT_BINARY_DIR}/shaders)

include_directories(third_party)

Expand Down
19 changes: 12 additions & 7 deletions vulkan/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
#include "Utils.h"

void Shader::load() {
auto filename = "shaders/" + name + ".comp.spv";
auto shader_code = Utils::readFile(filename);
if (shader_code.empty()) {
throw std::runtime_error("Failed to load shader: " + filename);
}
vk::ShaderModuleCreateInfo create_info;
create_info.codeSize = shader_code.size();
create_info.pCode = reinterpret_cast<const uint32_t *>(shader_code.data());
if (data == nullptr) {
auto fn = "shaders/" + filename + ".spv";
auto shader_code = Utils::readFile(fn);
if (shader_code.empty()) {
throw std::runtime_error("Failed to load shader: " + fn);
}
create_info.codeSize = shader_code.size();
create_info.pCode = reinterpret_cast<const uint32_t *>(shader_code.data());
} else {
create_info.codeSize = size;
create_info.pCode = reinterpret_cast<const uint32_t *>(data);
}
shader = context->device->createShaderModuleUnique(create_info);
}
22 changes: 19 additions & 3 deletions vulkan/Shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,33 @@

class Shader {
public:
Shader(const std::shared_ptr<VulkanContext>& _context, std::string name)
Shader(const std::shared_ptr<VulkanContext>& _context, std::string filename)
: context(_context),
name(std::move(name)) {
filename(std::move(filename)) {
}

Shader(const std::shared_ptr<VulkanContext>& _context, unsigned char *data, size_t size)
: context(_context),
filename(""),
data(data),
size(size) {
}

Shader(const std::shared_ptr<VulkanContext>& context, const std::string& filename, unsigned char* data, size_t size)
: filename(filename),
context(context),
data(data),
size(size) {
}

void load();

vk::UniqueShaderModule shader;
private:
const std::string name;
const std::string filename;
std::shared_ptr<VulkanContext> context;
unsigned char* data;
size_t size;
};


Expand Down
6 changes: 3 additions & 3 deletions vulkan/pipelines/ComputePipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

#include "ComputePipeline.h"

ComputePipeline::ComputePipeline(const std::shared_ptr<VulkanContext> &context, const std::string &shaderFile) : Pipeline(context), shader(context, shaderFile) {
shader.load();
ComputePipeline::ComputePipeline(const std::shared_ptr<VulkanContext>& context, std::shared_ptr<Shader> shader): Pipeline(context), shader(std::move(shader)) {
shader->load();
}

void ComputePipeline::build() {
buildPipelineLayout();

vk::PipelineShaderStageCreateInfo pipelineShaderStageCreateInfo({}, vk::ShaderStageFlagBits::eCompute, shader.shader.get(), "main");
vk::PipelineShaderStageCreateInfo pipelineShaderStageCreateInfo({}, vk::ShaderStageFlagBits::eCompute, shader->shader.get(), "main");
vk::ComputePipelineCreateInfo computePipelineCreateInfo({}, pipelineShaderStageCreateInfo, pipelineLayout.get());
pipeline = context->device->createComputePipelineUnique(nullptr, computePipelineCreateInfo).value;
}
5 changes: 3 additions & 2 deletions vulkan/pipelines/ComputePipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@


#include "Pipeline.h"
#include <memory>

class ComputePipeline : public Pipeline {
public:
explicit ComputePipeline(const std::shared_ptr<VulkanContext> &context, const std::string& shader);
explicit ComputePipeline(const std::shared_ptr<VulkanContext> &context, std::shared_ptr<Shader> shader);;

void build() override;
private:
Shader shader;
std::shared_ptr<Shader> shader;
};


Expand Down

0 comments on commit ef16ef8

Please sign in to comment.