Last active
October 5, 2025 10:10
-
-
Save Themaister/49f97198b4dff61a0911613fb7f7fd31 to your computer and use it in GitHub Desktop.
Bringup sample #3 - descriptors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #version 460 | |
| layout(local_size_x = 16) in; | |
| layout(set = 0, binding = 0) writeonly buffer Output | |
| { | |
| uint data[]; | |
| } o; | |
| layout(set = 0, binding = 1) uniform usamplerBuffer i; | |
| void main() | |
| { | |
| o.data[gl_GlobalInvocationID.x] = texelFetch(i, int(gl_GlobalInvocationID.x)).x; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "device.hpp" | |
| #include "context.hpp" | |
| #include "logging.hpp" | |
| #include <stdlib.h> | |
| static const uint32_t copy_shader[] = | |
| #include "copy.h" | |
| ; | |
| static void run_some_code(Vulkan::Device &dev) | |
| { | |
| Vulkan::BufferCreateInfo info = {}; | |
| info.size = 64; | |
| info.domain = Vulkan::BufferDomain::Device; | |
| info.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; | |
| info.misc = Vulkan::BUFFER_MISC_ZERO_INITIALIZE_BIT; | |
| auto gpu_buffer = dev.create_buffer(info); | |
| dev.set_name(*gpu_buffer, "gpu-buffer"); | |
| info.domain = Vulkan::BufferDomain::Host; | |
| info.misc = 0; | |
| info.usage = VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT; | |
| uint8_t initial_data[16]; | |
| for (uint8_t i = 0; i < 16; i++) | |
| initial_data[i] = i; | |
| info.size = sizeof(initial_data); | |
| auto cpu_buffer = dev.create_buffer(info, initial_data); | |
| dev.set_name(*cpu_buffer, "cpu-buffer"); | |
| Vulkan::Program *prog = dev.request_program(copy_shader, sizeof(copy_shader)); | |
| // A buffer view is typed. The shader doesn't have to know that it's reading this format. | |
| // This is a very powerful feature of GPUs. | |
| Vulkan::BufferViewCreateInfo buffer_view_info = {}; | |
| buffer_view_info.buffer = cpu_buffer.get(); | |
| buffer_view_info.format = VK_FORMAT_R8_UINT; | |
| buffer_view_info.offset = 0; | |
| buffer_view_info.range = sizeof(initial_data); | |
| Vulkan::BufferViewHandle buffer_view = dev.create_buffer_view(buffer_view_info); | |
| Vulkan::CommandBufferHandle cmd = dev.request_command_buffer(Vulkan::CommandBuffer::Type::Generic); | |
| cmd->set_program(prog); | |
| // set and binding will be explained later. | |
| cmd->set_storage_buffer(/* set */ 0, /* binding */ 0, *gpu_buffer); | |
| cmd->set_buffer_view(/* set */ 0, /* binding */ 1, *buffer_view); | |
| cmd->dispatch(1, 1, 1); | |
| dev.submit(cmd); | |
| dev.next_frame_context(); | |
| } | |
| int main() | |
| { | |
| if (!Vulkan::Context::init_loader(nullptr)) | |
| return EXIT_FAILURE; | |
| Vulkan::Context ctx; | |
| if (!ctx.init_instance_and_device(nullptr, 0, nullptr, 0, Vulkan::CONTEXT_CREATION_ENABLE_ROBUSTNESS_2_BIT)) | |
| return EXIT_FAILURE; | |
| Vulkan::Device dev; | |
| dev.set_context(ctx); | |
| bool has_rdoc = Vulkan::Device::init_renderdoc_capture(); | |
| if (has_rdoc) | |
| dev.begin_renderdoc_capture(); | |
| run_some_code(dev); | |
| if (has_rdoc) | |
| dev.end_renderdoc_capture(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment