diff --git a/engine/src/vulkan/buffer.hpp b/engine/src/vulkan/buffer.hpp index 2915824..52697c5 100644 --- a/engine/src/vulkan/buffer.hpp +++ b/engine/src/vulkan/buffer.hpp @@ -48,6 +48,10 @@ class Buffer { } vk::DeviceSize getBufferSize() const { return bufferSize; } + vk::DeviceAddress getDeviceAddress() { + return this->device.get()->getBufferAddress(this->buffer); + } + private: static vk::DeviceSize getAlignment(vk::DeviceSize instanceSize, vk::DeviceSize minOffsetAlignment); diff --git a/sandbox/main.cpp b/sandbox/main.cpp index 5672ff7..573dbb9 100644 --- a/sandbox/main.cpp +++ b/sandbox/main.cpp @@ -1,8 +1,10 @@ #define GLFW_INCLUDE_VULKAN #include +#include #include #include +#include #include #include #include @@ -18,6 +20,9 @@ #include "vulkan/shader.hpp" #include "window.hpp" +// local includes +#include "material.hpp" + constexpr uint32_t WIDTH = 1280; constexpr uint32_t HEIGHT = 720; constexpr const char* NAME = "sandbox"; @@ -29,9 +34,7 @@ void immediate_submit(sophia::Device& device, device.endSingleTimeCommands(command); } -#include "material.hpp" - -int main() { +void triangle_test() { sophia::Window window{WIDTH, HEIGHT, NAME}; sophia::Device device{&window}; sophia::RenderContext renderContext{window, device}; @@ -89,8 +92,7 @@ int main() { triangleBuffer.writeToBuffer(verticies); - vk::DeviceAddress address = - device.get()->getBufferAddress({triangleBuffer.getBuffer()}); + vk::DeviceAddress address = triangleBuffer.getDeviceAddress(); while (!window.shouldClose()) { glfwPollEvents(); @@ -151,6 +153,69 @@ int main() { } device.waitIdle(); +} +int main() { + { + using namespace sophia; + + auto shader = Shader::Builder() + .fromGLSL(SANDBOX_SHADER_DIR "/shader.comp") + .setStage(ShaderStage::VERTEX) + .build(); + + std::vector spirv = shader.get()->getSpirv(); + size_t spirv_size = spirv.size() * sizeof(u32); + void* spirv_code = spirv.data(); + + SpvReflectShaderModule module; + SpvReflectResult result = + spvReflectCreateShaderModule(spirv_size, spirv_code, &module); + assert(result == SPV_REFLECT_RESULT_SUCCESS); + + { + log::print(); + log::info("Reflecting Input Variables"); + + uint32_t count = 0; + result = spvReflectEnumerateInputVariables(&module, &count, NULL); + assert(result == SPV_REFLECT_RESULT_SUCCESS); + SpvReflectInterfaceVariable** vars = + (SpvReflectInterfaceVariable**)malloc( + count * sizeof(SpvReflectInterfaceVariable*)); + result = spvReflectEnumerateInputVariables(&module, &count, vars); + assert(result == SPV_REFLECT_RESULT_SUCCESS); + + for (size_t i = 0; i < count; i++) { + SpvReflectInterfaceVariable* var = vars[i]; + assert(var != nullptr); + + std::cout << "{" << std::endl; + log::print("\tname: {},", var->name); + std::cout << "}" << std::endl; + std::cout << std::endl; + } + } + + { + log::print(); + log::info("Reflecting Output Variables"); + + u32 count = 0; + result = spvReflectEnumerateOutputVariables(&module, &count, NULL); + assert(result == SPV_REFLECT_RESULT_SUCCESS); + + SpvReflectInterfaceVariable** vars = + (SpvReflectInterfaceVariable**)malloc( + count * sizeof(SpvReflectInterfaceVariable*)); + + result = spvReflectEnumerateOutputVariables(&module, &count, vars); + assert(result == SPV_REFLECT_RESULT_SUCCESS); + } + + spvReflectDestroyShaderModule(&module); + } + + // triangle_test(); return EXIT_SUCCESS; } diff --git a/sandbox/material.hpp b/sandbox/material.hpp index 1fd51a5..c035a20 100644 --- a/sandbox/material.hpp +++ b/sandbox/material.hpp @@ -40,8 +40,14 @@ class Material { std::unique_ptr pipeline; }; -struct MaterialInstance { +class MaterialInstance { + public: + void setBaseColor(); + + private: Material* material; + + vec3 base_color = {}; }; // !NOTE: eventually add ecs support for ecs diff --git a/sandbox/shaders/shader.comp b/sandbox/shaders/shader.comp new file mode 100644 index 0000000..1873496 --- /dev/null +++ b/sandbox/shaders/shader.comp @@ -0,0 +1,45 @@ +#version 450 +#extension GL_EXT_buffer_reference : require + +struct TriangleVertex { + vec4 vertex; + vec4 color; +}; + +layout(buffer_reference, std430) readonly buffer TriangleVertexBuffer { + TriangleVertex vertices[]; +}; + +layout( push_constant ) uniform constants { + TriangleVertexBuffer triangleBuffer; + vec2 padding; + vec4 data1; + vec4 data2; + vec4 data3; +} PushConstants; + +layout (location = 0) out vec3 outColor; + +void main() +{ + //const array of positions for the triangle + const vec3 positions[3] = vec3[3]( + vec3(-1.f,1.f, 0.0f), + vec3(1.f,1.f, 0.0f), + vec3(0.f,-1.f, 0.0f) + ); + + //const array of colors for the triangle + const vec3 colors[3] = vec3[3]( + vec3(1.0f, 0.0f, 0.0f), //red + vec3(0.0f, 1.0f, 0.0f), //green + vec3(00.f, 0.0f, 1.0f) //blue + ); + + // Load vertex data from device address + TriangleVertex v = PushConstants.triangleBuffer.vertices[gl_VertexIndex]; + + // output the position of each vertex + gl_Position = vec4(v.vertex.xyz, 1.0); + outColor = vec3(v.color.xyz); +}