diff --git a/libs/wgpu/include/psemek/wgpu/pipeline_layout.hpp b/libs/wgpu/include/psemek/wgpu/pipeline_layout.hpp new file mode 100644 index 00000000..7f311b3a --- /dev/null +++ b/libs/wgpu/include/psemek/wgpu/pipeline_layout.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include + +namespace psemek::wgpu +{ + + struct pipeline_layout + : detail::object + { + using detail::object::object; + + static void reference(void * ptr); + static void release(void * ptr); + + private: + explicit pipeline_layout(void * ptr) + : detail::object(ptr) + {} + + friend struct device; + }; + +} diff --git a/libs/wgpu/include/psemek/wgpu/shader_module.hpp b/libs/wgpu/include/psemek/wgpu/shader_module.hpp new file mode 100644 index 00000000..dc766950 --- /dev/null +++ b/libs/wgpu/include/psemek/wgpu/shader_module.hpp @@ -0,0 +1,120 @@ +#pragma once + +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace psemek::wgpu +{ + + enum class shader_stage { + none = 0x00000000, + vertex = 0x00000001, + fragment = 0x00000002, + compute = 0x00000004, + }; + + struct shader_module + : detail::object + { + using detail::object::object; + + enum class compilation_info_request_status : std::uint32_t + { + success = 0x00000000, + error = 0x00000001, + device_lost = 0x00000002, + unknown = 0x00000003, + }; + + enum class compilation_message_type : std::uint32_t + { + error = 0x00000000, + warning = 0x00000001, + info = 0x00000002, + }; + + struct compilation_message + { + std::vector chain = {}; + std::string message = {}; + compilation_message_type type; + std::uint64_t line_num; + std::uint64_t line_pos; + std::uint64_t offset; + std::uint64_t length; + std::uint64_t utf16_line_pos; + std::uint64_t utf16_offset; + std::uint64_t utf16_length; + }; + + struct compilation_info + { + std::vector chain = {}; + std::vector messages = {}; + }; + + struct compilation_hint + { + std::vector chain = {}; + std::string entryPoint; + pipeline_layout layout; + }; + + struct define + { + char const * name; + char const * value; + }; + + struct descriptor + { + std::vector chain = {}; + std::string label = {}; + std::vector hints = {}; + }; + + struct spirv_descriptor + { + util::span code; + }; + + struct wgsl_descriptor + { + char const * code; + }; + + struct glsl_descriptor + { + shader_stage stage; + char const * code; + std::vector defines = {}; + }; + + using compilation_info_callback = std::function; + + void get_compilation_info(compilation_info_callback const & callback); + void set_label(std::string const & label); + + static void reference(void * ptr); + static void release(void * ptr); + + private: + explicit shader_module(void * ptr) + : detail::object(ptr) + {} + + friend struct device; + }; + + detail::chained_struct_ptr to_chained_struct(shader_module::spirv_descriptor const & value); + detail::chained_struct_ptr to_chained_struct(shader_module::wgsl_descriptor const & value); + detail::chained_struct_ptr to_chained_struct(shader_module::glsl_descriptor const & value); + +} diff --git a/libs/wgpu/objects-todo b/libs/wgpu/objects-todo index 42ee9a61..d630809c 100644 --- a/libs/wgpu/objects-todo +++ b/libs/wgpu/objects-todo @@ -8,7 +8,7 @@ WGPUComputePipeline - WGPUDevice + WGPUInstance - WGPUPipelineLayout +- WGPUPipelineLayout + WGPUQuerySet + WGPUQueue WGPURenderBundle @@ -16,7 +16,7 @@ - WGPURenderPassEncoder WGPURenderPipeline WGPUSampler - WGPUShaderModule ++ WGPUShaderModule + WGPUSurface + WGPUTexture + WGPUTextureView diff --git a/libs/wgpu/source/pipeline_layout.cpp b/libs/wgpu/source/pipeline_layout.cpp new file mode 100644 index 00000000..e0014927 --- /dev/null +++ b/libs/wgpu/source/pipeline_layout.cpp @@ -0,0 +1,17 @@ +#include +#include + +namespace psemek::wgpu +{ + + void pipeline_layout::reference(void * ptr) + { + wgpuPipelineLayoutReference((WGPUPipelineLayout)ptr); + } + + void pipeline_layout::release(void * ptr) + { + wgpuPipelineLayoutRelease((WGPUPipelineLayout)ptr); + } + +} diff --git a/libs/wgpu/source/shader_module.cpp b/libs/wgpu/source/shader_module.cpp new file mode 100644 index 00000000..54192138 --- /dev/null +++ b/libs/wgpu/source/shader_module.cpp @@ -0,0 +1,89 @@ +#include +#include +#include + +#include + +namespace psemek::wgpu +{ + + void shader_module::get_compilation_info(compilation_info_callback const & callback) + { + auto userdata = new compilation_info_callback(callback); + + auto real_callback = [](WGPUCompilationInfoRequestStatus status, WGPUCompilationInfo const * info, void * userdata) + { + std::unique_ptr callback((compilation_info_callback *)userdata); + + // TODO support chained struct + compilation_info cinfo; + if (info) + { + for (std::size_t i = 0; i < info->messageCount; ++i) + { + auto & message = cinfo.messages.emplace_back(); + auto & message_in = info->messages[i]; + + message.message = message_in.message; + message.type = (compilation_message_type)message_in.type; + message.line_num = message_in.lineNum; + message.line_pos = message_in.linePos; + message.offset = message_in.offset; + message.length = message_in.length; + message.utf16_line_pos = message_in.utf16LinePos; + message.utf16_offset = message_in.utf16Offset; + message.utf16_length = message_in.utf16Length; + } + } + + if (*callback) (*callback)((compilation_info_request_status)status, cinfo); + }; + + wgpuShaderModuleGetCompilationInfo((WGPUShaderModule)get(), real_callback, userdata); + } + + void shader_module::set_label(std::string const & label) + { + wgpuShaderModuleSetLabel((WGPUShaderModule)get(), label.data()); + } + + void shader_module::reference(void * ptr) + { + wgpuShaderModuleReference((WGPUShaderModule)ptr); + } + + void shader_module::release(void * ptr) + { + wgpuShaderModuleRelease((WGPUShaderModule)ptr); + } + + detail::chained_struct_ptr to_chained_struct(shader_module::spirv_descriptor const & value) + { + WGPUShaderModuleSPIRVDescriptor chained = {}; + chained.chain.sType = WGPUSType_ShaderModuleSPIRVDescriptor; + chained.codeSize = value.code.size(); // TODO: is it in bytes or in words? + chained.code = value.code.data(); + return detail::make_chained_struct(chained); + } + + detail::chained_struct_ptr to_chained_struct(shader_module::wgsl_descriptor const & value) + { + WGPUShaderModuleWGSLDescriptor chained = {}; + chained.chain.sType = WGPUSType_ShaderModuleWGSLDescriptor; + chained.code = value.code; + return detail::make_chained_struct(chained); + } + + detail::chained_struct_ptr to_chained_struct(shader_module::glsl_descriptor const & value) + { + WGPUShaderModuleGLSLDescriptor chained = {}; + chained.chain.sType = (WGPUSType)WGPUSType_ShaderModuleGLSLDescriptor; + chained.stage = (WGPUShaderStage)value.stage; + chained.code = value.code; + chained.defineCount = value.defines.size(); + static_assert(sizeof(WGPUShaderDefine) == sizeof(shader_module::define)); + chained.defines = (WGPUShaderDefine *)value.defines.data(); + return detail::make_chained_struct(chained); + } + +}