Test drawing triangle
This commit is contained in:
1 parent
d654c7d999
commit
f7dfdd9666
2 files changed
+286
-270
No files matched your search
+282
-229
@@ -6,6 +6,7 @@
|
||||
#include "gpu/gpu_inc.h"
|
||||
#include "wapp/wapp.h"
|
||||
#include "cglm/struct.h"
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
wp_intern WpLogger logger = wpLogMakeLogger(wpStr8LitRo("main"));
|
||||
@@ -17,11 +18,29 @@ enum Errors {
|
||||
|
||||
typedef struct Vertex {
|
||||
vec3s position;
|
||||
vec3s normal;
|
||||
vec2s uv;
|
||||
vec3s color;
|
||||
} Vertex;
|
||||
typedef Vertex *VertexArray;
|
||||
|
||||
wp_intern const VertexArray vertices = wpArray(
|
||||
Vertex,
|
||||
(Vertex){
|
||||
.position = (vec3s){ .x = 0.0f, .y = -0.5f, .z = 0.0f },
|
||||
.color = (vec3s){ .r = 1.0f, .g = 0.0f, .b = 0.0f },
|
||||
},
|
||||
(Vertex){
|
||||
.position = (vec3s){ .x = -0.5f, .y = 0.5f, .z = 0.0f },
|
||||
.color = (vec3s){ .r = 0.0f, .g = 1.0f, .b = 0.0f },
|
||||
},
|
||||
(Vertex){
|
||||
.position = (vec3s){ .x = 0.5f, .y = 0.5f, .z = 0.0f },
|
||||
.color = (vec3s){ .r = 0.0f, .g = 0.0f, .b = 1.0f },
|
||||
}
|
||||
);
|
||||
|
||||
i32 main(void) {
|
||||
wpLogConfigure(NULL, NULL, WP_LOG_LEVEL_INFO);
|
||||
|
||||
WpAllocator arena = wpMemArenaAllocatorInit(MiB(64));
|
||||
WpAllocator temp_arena = wpMemArenaAllocatorInit(MiB(8));
|
||||
|
||||
@@ -78,36 +97,17 @@ i32 main(void) {
|
||||
wpStr8Format(&msg, "Created swapchain %p with %" PRIu64 " images", swapchain, swapchain_image_count);
|
||||
wpLogInfo(&logger, msg);
|
||||
|
||||
u64 vertices_size = sizeof(Vertex) * wpArrayCount(vertices);
|
||||
GpuBufferCreateRequest vert_idx_buf_request = {
|
||||
.device = device,
|
||||
.usage = GPU_BUFFER_USAGE_VERTEX_BUFFER | GPU_BUFFER_USAGE_INDEX_BUFFER,
|
||||
.size = MiB(1),
|
||||
.usage = GPU_BUFFER_USAGE_VERTEX_BUFFER,
|
||||
.size = vertices_size,
|
||||
};
|
||||
GpuBuffer vert_idx_buf = gpuCreateBuffer(&arena, vert_idx_buf_request);
|
||||
GpuBuffer vertex_buffer = gpuCreateBuffer(&arena, vert_idx_buf_request);
|
||||
memcpy(vertex_buffer.cpu_mapping, vertices, vertices_size);
|
||||
|
||||
wpStr8Format(&msg, "Created buffer with size %" PRIu64 " (CPU: %p, GPU: %" PRIu64 ")",
|
||||
vert_idx_buf.size, vert_idx_buf.cpu_mapping, vert_idx_buf.gpu_device_address);
|
||||
wpLogInfo(&logger, msg);
|
||||
|
||||
GpuBufferCreateRequest storage_buf_request = {
|
||||
.device = device,
|
||||
.usage = GPU_BUFFER_USAGE_STORAGE_BUFFER | GPU_BUFFER_USAGE_SHADER_DEVICE_ADDRESS,
|
||||
.size = KiB(4),
|
||||
};
|
||||
GpuBuffer storage_buf = gpuCreateBuffer(&arena, storage_buf_request);
|
||||
|
||||
wpStr8Format(&msg, "Created buffer with size %" PRIu64 " (CPU: %p, GPU: %" PRIu64 ")",
|
||||
storage_buf.size, storage_buf.cpu_mapping, storage_buf.gpu_device_address);
|
||||
wpLogInfo(&logger, msg);
|
||||
|
||||
GpuFence *fence = gpuCreateFence(&arena, device);
|
||||
|
||||
wpStr8Format(&msg, "Created fence %p", fence);
|
||||
wpLogInfo(&logger, msg);
|
||||
|
||||
GpuSemaphore *semaphore = gpuCreateSemaphore(&arena, device);
|
||||
|
||||
wpStr8Format(&msg, "Created semaphore %p", semaphore);
|
||||
vertex_buffer.size, vertex_buffer.cpu_mapping, vertex_buffer.gpu_device_address);
|
||||
wpLogInfo(&logger, msg);
|
||||
|
||||
GpuCommandPool *pool = gpuCreateCommandPool(&arena, device);
|
||||
@@ -117,89 +117,10 @@ i32 main(void) {
|
||||
|
||||
GpuCommandBufferAllocateRequest buf_alloc_request = {
|
||||
.pool = pool,
|
||||
.buffer_count = swapchain_image_count,
|
||||
.buffer_count = 1,
|
||||
};
|
||||
GpuCommandBufferPtrArray buffers = gpuAllocateCommandBuffers(&arena, &temp_arena, buf_alloc_request);
|
||||
|
||||
GpuCommandBufferBeginRequest cb_request = {
|
||||
.command_buffer = buffers[0],
|
||||
};
|
||||
gpuBeginCommandBuffer(cb_request);
|
||||
gpuEndCommandBuffer(buffers[0]);
|
||||
|
||||
GpuCommandBuffersSubmitRequest cmd_submit_request = {
|
||||
.device = device,
|
||||
.fence = fence,
|
||||
.command_buffers = wpArray(GpuCommandBuffer *, buffers[0]),
|
||||
};
|
||||
gpuSubmitCommandBuffers(&temp_arena, cmd_submit_request);
|
||||
|
||||
WpF32Array floats = wpArray(f32, 1.0f, 2.0f, 3.0f, 4.0f);
|
||||
GpuBufferCreateRequest storage_request = {
|
||||
.device = device,
|
||||
.size = sizeof(f32) * wpArrayCount(floats),
|
||||
.usage = GPU_BUFFER_USAGE_STORAGE_BUFFER,
|
||||
};
|
||||
GpuBuffer storage = gpuCreateBuffer(&arena, storage_request);
|
||||
memcpy(storage.cpu_mapping, (void *)floats, sizeof(f32) * wpArrayCount(floats));
|
||||
|
||||
GpuDescriptorPoolSizeArray pool_sizes = wpArray(
|
||||
GpuDescriptorPoolSize,
|
||||
(GpuDescriptorPoolSize){ .type = GPU_DESCRIPTOR_TYPE_STORAGE_BUFFER, .count = 3 },
|
||||
);
|
||||
GpuDescriptorPoolCreateRequest pool_request = {
|
||||
.descriptor_pool_sizes = pool_sizes,
|
||||
.device = device,
|
||||
.max_sets = 20,
|
||||
};
|
||||
GpuDescriptorPool *desc_pool = gpuCreateDescriptorPool(&arena, pool_request);
|
||||
|
||||
GpuDescriptorSetBindingArray bindings = wpArray(
|
||||
GpuDescriptorSetBinding,
|
||||
(GpuDescriptorSetBinding){ .binding = 0,
|
||||
.type = GPU_DESCRIPTOR_TYPE_STORAGE_BUFFER,
|
||||
.descriptor_count = 1,
|
||||
.stages = GPU_SHADER_STAGE_VERTEX | GPU_SHADER_STAGE_FRAGMENT |
|
||||
GPU_SHADER_STAGE_COMPUTE },
|
||||
(GpuDescriptorSetBinding){ .binding = 1,
|
||||
.type = GPU_DESCRIPTOR_TYPE_STORAGE_BUFFER,
|
||||
.descriptor_count = 1,
|
||||
.stages = GPU_SHADER_STAGE_VERTEX | GPU_SHADER_STAGE_FRAGMENT |
|
||||
GPU_SHADER_STAGE_COMPUTE },
|
||||
(GpuDescriptorSetBinding){ .binding = 2,
|
||||
.type = GPU_DESCRIPTOR_TYPE_STORAGE_BUFFER,
|
||||
.descriptor_count = 1,
|
||||
.stages = GPU_SHADER_STAGE_VERTEX | GPU_SHADER_STAGE_FRAGMENT |
|
||||
GPU_SHADER_STAGE_COMPUTE },
|
||||
);
|
||||
GpuDescriptorSetLayoutCreateRequest set_layout_request = {
|
||||
.device = device,
|
||||
.bindings = bindings,
|
||||
};
|
||||
GpuDescriptorSetLayout *set_layout = gpuCreateDescriptorSetLayout(&arena, &temp_arena, set_layout_request);
|
||||
|
||||
GpuDescriptorSetAllocateRequest sets_alloc_request = {
|
||||
.pool = desc_pool,
|
||||
.layouts = wpArray(GpuDescriptorSetLayout *, set_layout),
|
||||
};
|
||||
GpuDescriptorSetPtrArray sets = gpuAllocateDescriptorSets(&arena, &temp_arena, sets_alloc_request);
|
||||
|
||||
GpuDescriptorBufferInfoArray buffer_infos = wpArray(
|
||||
GpuDescriptorBufferInfo,
|
||||
(GpuDescriptorBufferInfo){ .buffer = &storage, .range = storage.size }
|
||||
);
|
||||
GpuDescriptorSetWriteRequest write_request = {
|
||||
.device = device,
|
||||
.write_infos = wpArray(
|
||||
GpuDescriptorSetWriteInfo,
|
||||
(GpuDescriptorSetWriteInfo){ .set = sets[0],
|
||||
.dst_binding = 0,
|
||||
.descriptor_type = GPU_DESCRIPTOR_TYPE_STORAGE_BUFFER,
|
||||
.buffer_infos = buffer_infos }
|
||||
),
|
||||
};
|
||||
gpuWriteDescriptorSets(&temp_arena, write_request);
|
||||
|
||||
WpFile *spirv = wpFileOpen(&temp_arena, &wpStr8LitRo("shaders/shader.spv"), WP_ACCESS_READ);
|
||||
u64 code_size = wpFileGetLength(spirv);
|
||||
|
||||
@@ -217,141 +138,273 @@ i32 main(void) {
|
||||
|
||||
wpFileClose(spirv);
|
||||
|
||||
GpuPipelineLayoutCreateRequest graphics_pl_request = {
|
||||
.device = device,
|
||||
.descriptor_set_layouts = wpArray(GpuDescriptorSetLayout *, set_layout),
|
||||
.push_constants = wpArray(
|
||||
GpuPushConstantRange,
|
||||
(GpuPushConstantRange){ .stages = GPU_SHADER_STAGE_VERTEX, .size = sizeof(u64) }
|
||||
),
|
||||
};
|
||||
GpuPipelineLayout *graphics_pipeline_layout = gpuCreatePipelineLayout(&arena, &temp_arena, graphics_pl_request);
|
||||
GpuPipelineLayoutCreateRequest graphics_pipeline_layout_request = { .device = device };
|
||||
GpuPipelineLayout *layout = gpuCreatePipelineLayout(&arena, &temp_arena, graphics_pipeline_layout_request);
|
||||
|
||||
GpuVertexInputBinding vert_binding = {
|
||||
.binding = 0,
|
||||
.stride = sizeof(Vertex),
|
||||
.input_rate = GPU_VERTEX_INPUT_RATE_VERTEX,
|
||||
};
|
||||
GpuVertexInputAttributeArray attributes = wpArray(
|
||||
GpuVertexInputAttribute,
|
||||
(GpuVertexInputAttribute){
|
||||
.location = 0,
|
||||
.binding = 0,
|
||||
.offset = offsetof(Vertex, position),
|
||||
.format = GPU_FORMAT_R32G32B32_SFLOAT,
|
||||
},
|
||||
(GpuVertexInputAttribute){
|
||||
.location = 1,
|
||||
.binding = 0,
|
||||
.offset = offsetof(Vertex, color),
|
||||
.format = GPU_FORMAT_R32G32B32_SFLOAT,
|
||||
},
|
||||
);
|
||||
GpuPipelineVertexInputStateInfo vert_info = {
|
||||
.bindings = wpArray(GpuVertexInputBinding, vert_binding),
|
||||
.attributes = attributes,
|
||||
};
|
||||
|
||||
GpuPipelineInputAssemblyStateInfo assembly_info = { .topology = GPU_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST };
|
||||
|
||||
GpuPipelineViewportStateInfo viewport_info = { .viewport_count = 1, .scissor_count = 1 };
|
||||
|
||||
GpuPipelineRasterizationStateInfo raster_info = { .line_width = 1.0f };
|
||||
|
||||
GpuPipelineDepthStencilStateInfo depth_info = {
|
||||
.depth_test_enable = true,
|
||||
.depth_write_enable = true,
|
||||
.depth_compare_op = GPU_COMPARE_OP_LESS_OR_EQUAL,
|
||||
};
|
||||
|
||||
GpuPipelineColorBlendAttachmentState color_attachment = { .color_write_mask = 0xf };
|
||||
GpuPipelineColorBlendStateInfo blend_info = {
|
||||
.attachments = wpArray(GpuPipelineColorBlendAttachmentState, color_attachment)
|
||||
};
|
||||
|
||||
GpuPipelineShaderStageInfoArray shader_stages = wpArray(
|
||||
GpuPipelineShaderStageInfo,
|
||||
(GpuPipelineShaderStageInfo){
|
||||
.shader = shader,
|
||||
.stage = GPU_SHADER_STAGE_VERTEX,
|
||||
.name = "vertMain",
|
||||
},
|
||||
(GpuPipelineShaderStageInfo){
|
||||
.shader = shader,
|
||||
.stage = GPU_SHADER_STAGE_FRAGMENT,
|
||||
.name = "fragMain",
|
||||
},
|
||||
);
|
||||
|
||||
GpuGraphicsPipelineCreateInfo info = {
|
||||
.vertex_input_state = vert_info,
|
||||
.input_assembly_state = assembly_info,
|
||||
.viewport_state = viewport_info,
|
||||
.rasterization_state = raster_info,
|
||||
.depth_stencil_state = depth_info,
|
||||
.color_blend_state = blend_info,
|
||||
.shader_stages = shader_stages,
|
||||
.layout = layout,
|
||||
.color_attachment_format = gpuGetSwapchainImageFormat(swapchain),
|
||||
.depth_attachment_format = gpuGetSwapchainDepthFormat(swapchain),
|
||||
};
|
||||
GpuGraphicsPipelinesCreateRequest graphics_pipelines_request = {
|
||||
.device = device,
|
||||
.infos = wpArray(
|
||||
GpuGraphicsPipelineCreateInfo,
|
||||
(GpuGraphicsPipelineCreateInfo){
|
||||
.vertex_input_state = (GpuPipelineVertexInputStateInfo){
|
||||
.bindings = wpArray(
|
||||
GpuVertexInputBinding,
|
||||
(GpuVertexInputBinding){ .binding = 0, .stride = sizeof(Vertex), .input_rate = GPU_VERTEX_INPUT_RATE_VERTEX }
|
||||
),
|
||||
.attributes = wpArray(
|
||||
GpuVertexInputAttribute,
|
||||
(GpuVertexInputAttribute){
|
||||
.location = 0,
|
||||
.binding = 0,
|
||||
.offset = offsetof(Vertex, position),
|
||||
.format = GPU_FORMAT_R32G32B32_SFLOAT,
|
||||
},
|
||||
(GpuVertexInputAttribute){
|
||||
.location = 1,
|
||||
.binding = 0,
|
||||
.offset = offsetof(Vertex, normal),
|
||||
.format = GPU_FORMAT_R32G32B32_SFLOAT,
|
||||
},
|
||||
(GpuVertexInputAttribute){
|
||||
.location = 2,
|
||||
.binding = 0,
|
||||
.offset = offsetof(Vertex, uv),
|
||||
.format = GPU_FORMAT_R32G32_SFLOAT,
|
||||
}
|
||||
)
|
||||
},
|
||||
.input_assembly_state = (GpuPipelineInputAssemblyStateInfo){
|
||||
.topology = GPU_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
|
||||
},
|
||||
.viewport_state = (GpuPipelineViewportStateInfo){
|
||||
.viewport_count = 1,
|
||||
.scissor_count = 1,
|
||||
},
|
||||
.rasterization_state = (GpuPipelineRasterizationStateInfo){ .line_width = 1.0f },
|
||||
.multisample_state = (GpuPipelineMultisampleStateInfo){
|
||||
.rasterization_samples = GPU_SAMPLE_COUNT_1
|
||||
},
|
||||
.depth_stencil_state = (GpuPipelineDepthStencilStateInfo){
|
||||
.depth_test_enable = true,
|
||||
.depth_write_enable = true,
|
||||
.depth_compare_op = GPU_COMPARE_OP_LESS_OR_EQUAL,
|
||||
},
|
||||
.color_blend_state = (GpuPipelineColorBlendStateInfo){
|
||||
.attachments = wpArray(
|
||||
GpuPipelineColorBlendAttachmentState,
|
||||
(GpuPipelineColorBlendAttachmentState){
|
||||
.color_write_mask = 0xf
|
||||
}
|
||||
)
|
||||
},
|
||||
.shader_stages = wpArray(
|
||||
GpuPipelineShaderStageInfo,
|
||||
(GpuPipelineShaderStageInfo){
|
||||
.shader = shader,
|
||||
.name = "vertMain",
|
||||
.stage = GPU_SHADER_STAGE_VERTEX,
|
||||
},
|
||||
(GpuPipelineShaderStageInfo){
|
||||
.shader = shader,
|
||||
.name = "fragMain",
|
||||
.stage = GPU_SHADER_STAGE_FRAGMENT,
|
||||
},
|
||||
),
|
||||
.layout = graphics_pipeline_layout,
|
||||
.color_attachment_format = gpuGetSwapchainImageFormat(swapchain),
|
||||
.depth_attachment_format = gpuGetSwapchainDepthFormat(swapchain),
|
||||
},
|
||||
),
|
||||
.infos = wpArray(GpuGraphicsPipelineCreateInfo, info),
|
||||
};
|
||||
GpuPipelinePtrArray graphics_pipelines = gpuCreateGraphicsPipelines(&arena, &temp_arena, graphics_pipelines_request);
|
||||
GpuPipelinePtrArray pipelines = gpuCreateGraphicsPipelines(&arena, &temp_arena, graphics_pipelines_request);
|
||||
|
||||
GpuPipelineLayoutCreateRequest compute_pl_request = {
|
||||
.device = device,
|
||||
.descriptor_set_layouts = wpArray(GpuDescriptorSetLayout *, set_layout),
|
||||
.push_constants = wpArray(
|
||||
GpuPushConstantRange,
|
||||
(GpuPushConstantRange){ .stages = GPU_SHADER_STAGE_COMPUTE, .size = sizeof(u64) }
|
||||
),
|
||||
};
|
||||
GpuPipelineLayout *compute_pipeline_layout = gpuCreatePipelineLayout(&arena, &temp_arena, compute_pl_request);
|
||||
GpuFence *fence = gpuCreateFence(&arena, device);
|
||||
GpuFencePtrArray fences = wpArray(GpuFence *, fence);
|
||||
GpuSemaphore *image_acquired_semaphore = gpuCreateSemaphore(&arena, device);
|
||||
GpuSemaphorePtrArray render_completed_semaphores = wpArrayAllocCapacity(GpuSemaphore *, &arena,
|
||||
swapchain_image_count,
|
||||
WP_ARRAY_INIT_FILLED);
|
||||
for (u64 i = 0; i < swapchain_image_count; ++i) {
|
||||
render_completed_semaphores[i] = gpuCreateSemaphore(&arena, device);
|
||||
}
|
||||
|
||||
GpuComputePipelinesCreateRequest compute_pipelines_request = {
|
||||
.device = device,
|
||||
.infos = wpArray(
|
||||
GpuComputePipelineCreateInfo,
|
||||
(GpuComputePipelineCreateInfo){
|
||||
.layout = compute_pipeline_layout,
|
||||
.shader_stage = (GpuPipelineShaderStageInfo){
|
||||
.shader = shader,
|
||||
.stage = GPU_SHADER_STAGE_COMPUTE,
|
||||
.name = "compMain",
|
||||
},
|
||||
while (!windowShouldClose(&window)) {
|
||||
wpMemArenaAllocatorClear(&temp_arena);
|
||||
|
||||
gpuWaitForFences(&temp_arena, (GpuWaitForFencesRequest){ .device = device,
|
||||
.fences = fences,
|
||||
.timeout = UINT64_MAX });
|
||||
gpuResetFences(&temp_arena, (GpuResetFencesRequest){ .device = device, .fences = fences });
|
||||
|
||||
GpuSwapchainAcquireImageResult acquire_result = gpuAcquireNextSwapchainImage(
|
||||
(GpuSwapchainAcquireImageRequest){
|
||||
.swapchain = swapchain,
|
||||
.semaphore = image_acquired_semaphore,
|
||||
.timeout = UINT64_MAX,
|
||||
}
|
||||
),
|
||||
};
|
||||
GpuPipelinePtrArray compute_pipelines = gpuCreateComputePipelines(&arena, &temp_arena, compute_pipelines_request);
|
||||
);
|
||||
|
||||
wpMemArenaAllocatorClear(&temp_arena);
|
||||
gpuResetCommandBuffer(buffers[0]);
|
||||
|
||||
// while (!windowShouldClose(&window)) {}
|
||||
gpuBeginCommandBuffer((GpuCommandBufferBeginRequest){ .command_buffer = buffers[0], .one_time_submit = true });
|
||||
|
||||
GpuImage *color_image = gpuGetSwapchainColorImage(
|
||||
(GpuSwapchainGetColorImageRequest){ .swapchain = swapchain,
|
||||
.index = acquire_result.image_index }
|
||||
);
|
||||
GpuImage *depth_image = gpuGetSwapchainDepthImage(swapchain);
|
||||
|
||||
// Transition layout for rendering
|
||||
GpuImageMemoryBarrier color_barrier = {
|
||||
.image = color_image,
|
||||
.src_stage_mask = GPU_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT,
|
||||
.src_access_mask = GPU_ACCESS_NONE,
|
||||
.dst_stage_mask = GPU_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT,
|
||||
.dst_access_mask = GPU_ACCESS_COLOR_ATTACHMENT_READ | GPU_ACCESS_COLOR_ATTACHMENT_WRITE,
|
||||
.old_layout = GPU_IMAGE_LAYOUT_UNDEFINED,
|
||||
.new_layout = GPU_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
|
||||
.subresource = (GpuImageSubresourceRange){
|
||||
.aspect = GPU_IMAGE_ASPECT_COLOR,
|
||||
.level_count = 1,
|
||||
.layer_count = 1,
|
||||
},
|
||||
};
|
||||
GpuImageMemoryBarrier depth_barrier = {
|
||||
.image = depth_image,
|
||||
.src_stage_mask = GPU_PIPELINE_STAGE_LATE_FRAGMENT_TESTS,
|
||||
.src_access_mask = GPU_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE,
|
||||
.dst_stage_mask = GPU_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS,
|
||||
.dst_access_mask = GPU_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE,
|
||||
.old_layout = GPU_IMAGE_LAYOUT_UNDEFINED,
|
||||
.new_layout = GPU_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL,
|
||||
.subresource = (GpuImageSubresourceRange){
|
||||
.aspect = GPU_IMAGE_ASPECT_DEPTH,
|
||||
.level_count = 1,
|
||||
.layer_count = 1,
|
||||
},
|
||||
};
|
||||
GpuImageMemoryBarrierArray to_render_barriers = wpArray(
|
||||
GpuImageMemoryBarrier,
|
||||
color_barrier,
|
||||
depth_barrier,
|
||||
);
|
||||
GpuDependencyInfo dep_info = { .image_memory_barriers = to_render_barriers };
|
||||
gpuCmdPipelineBarrier(&temp_arena, (GpuCmdPipelineBarrierRequest){ .command_buffer = buffers[0],
|
||||
.info = dep_info});
|
||||
|
||||
GpuRect2d render_area = {
|
||||
.offset = { .x = 0, .y = 0 },
|
||||
.extent = { .width = window.size.x, .height = window.size.y }
|
||||
};
|
||||
GpuRenderingAttachmentInfo color_attachment = {
|
||||
.image = color_image,
|
||||
.layout = GPU_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
|
||||
.load_op = GPU_ATTACHMENT_LOAD_OP_CLEAR,
|
||||
.store_op = GPU_ATTACHMENT_STORE_OP_STORE,
|
||||
.clear_value = (GpuClearValue){ .color = {0} },
|
||||
};
|
||||
GpuRenderingAttachmentInfo depth_attachment = {
|
||||
.image = depth_image,
|
||||
.layout = GPU_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL,
|
||||
.load_op = GPU_ATTACHMENT_LOAD_OP_CLEAR,
|
||||
.store_op = GPU_ATTACHMENT_STORE_OP_DONT_CARE,
|
||||
.clear_value = (GpuClearValue){ .depth_stencil = { .depth = 1.0f } },
|
||||
};
|
||||
GpuCmdBeginRenderingRequest render_request = {
|
||||
.command_buffer = buffers[0],
|
||||
.render_area = render_area,
|
||||
.color_attachments = wpArray(GpuRenderingAttachmentInfo, color_attachment),
|
||||
.depth_attachment = &depth_attachment,
|
||||
.layer_count = 1,
|
||||
};
|
||||
gpuCmdBeginRendering(&temp_arena, render_request);
|
||||
|
||||
GpuViewportArray viewports = wpArray(
|
||||
GpuViewport,
|
||||
(GpuViewport){ .x = 0, .y = 0,
|
||||
.width = window.size.x, .height = window.size.y,
|
||||
.min_depth = 0.0f, .max_depth = 1.0f },
|
||||
);
|
||||
gpuCmdSetViewport(&temp_arena, (GpuCmdSetViewportRequest){ .command_buffer = buffers[0],
|
||||
.viewports = viewports });
|
||||
GpuRect2dArray scissors = wpArray(
|
||||
GpuRect2d,
|
||||
(GpuRect2d){
|
||||
.offset = { .x = 0, .y = 0 },
|
||||
.extent = { .width = window.size.x, .height = window.size.y }
|
||||
},
|
||||
);
|
||||
gpuCmdSetScissor(&temp_arena, (GpuCmdSetScissorRequest){ .command_buffer = buffers[0],
|
||||
.scissors = scissors });
|
||||
|
||||
GpuCmdBindVertexBuffersRequest vert_bind_req = {
|
||||
.command_buffer = buffers[0],
|
||||
.vertex_buffers = wpArray(GpuBuffer, vertex_buffer),
|
||||
.offsets = wpArray(u64, 0),
|
||||
};
|
||||
gpuCmdBindVertexBuffers(&temp_arena, vert_bind_req);
|
||||
|
||||
gpuCmdBindPipeline((GpuCmdBindPipelineRequest){ .command_buffer = buffers[0],
|
||||
.bind_point = GPU_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
.pipeline = pipelines[0] });
|
||||
|
||||
gpuCmdDraw((GpuCmdDrawRequest){ .command_buffer = buffers[0],
|
||||
.vertex_count = wpArrayCount(vertices),
|
||||
.instance_count = 1 });
|
||||
|
||||
gpuCmdEndRendering(buffers[0]);
|
||||
|
||||
// Transition layout for presentation
|
||||
color_barrier.src_access_mask = GPU_ACCESS_COLOR_ATTACHMENT_WRITE;
|
||||
color_barrier.dst_access_mask = GPU_ACCESS_NONE;
|
||||
color_barrier.old_layout = GPU_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
||||
color_barrier.new_layout = GPU_IMAGE_LAYOUT_PRESENT_SRC;
|
||||
|
||||
dep_info.image_memory_barriers = wpArray(GpuImageMemoryBarrier, color_barrier);
|
||||
gpuCmdPipelineBarrier(&temp_arena, (GpuCmdPipelineBarrierRequest){ .command_buffer = buffers[0],
|
||||
.info = dep_info});
|
||||
|
||||
gpuEndCommandBuffer(buffers[0]);
|
||||
|
||||
GpuSemaphoreSubmitInfo wait_semaphore_info = {
|
||||
.semaphore = image_acquired_semaphore,
|
||||
.stage_mask = GPU_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT,
|
||||
};
|
||||
GpuSemaphoreSubmitInfo signal_semaphore_info = {
|
||||
.semaphore = render_completed_semaphores[acquire_result.image_index],
|
||||
.stage_mask = GPU_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT,
|
||||
};
|
||||
GpuCommandBuffersSubmitRequest submit_request = {
|
||||
.device = device,
|
||||
.fence = fence,
|
||||
.wait_semaphore_infos = wpArray(GpuSemaphoreSubmitInfo, wait_semaphore_info),
|
||||
.command_buffers = buffers,
|
||||
.signal_semaphore_infos = wpArray(GpuSemaphoreSubmitInfo, signal_semaphore_info),
|
||||
};
|
||||
gpuSubmitCommandBuffers(&temp_arena, submit_request);
|
||||
|
||||
GpuSwpachainPresentInfo present_info = {
|
||||
.swapchain = swapchain,
|
||||
.image_index = acquire_result.image_index,
|
||||
};
|
||||
GpuSwapchainsPresentRequest present_request = {
|
||||
.device = device,
|
||||
.wait_semaphores = wpArray(GpuSemaphore *, render_completed_semaphores[acquire_result.image_index]),
|
||||
.swapchain_infos = wpArray(GpuSwpachainPresentInfo, present_info),
|
||||
};
|
||||
gpuPresentSwapchains(&temp_arena, present_request);
|
||||
}
|
||||
|
||||
gpuDeviceWaitIdle(device);
|
||||
for (u64 i = 0; i < wpArrayCount(compute_pipelines); ++i) {
|
||||
gpuDestroyPipeline(compute_pipelines[i]);
|
||||
for (u64 i = 0; i < swapchain_image_count; ++i) {
|
||||
gpuDestroySemaphore(render_completed_semaphores[i]);
|
||||
}
|
||||
for (u64 i = 0; i < wpArrayCount(graphics_pipelines); ++i) {
|
||||
gpuDestroyPipeline(graphics_pipelines[i]);
|
||||
}
|
||||
gpuDestroyPipelineLayout(compute_pipeline_layout);
|
||||
gpuDestroyPipelineLayout(graphics_pipeline_layout);
|
||||
gpuDestroyShader(shader);
|
||||
gpuDestroyBuffer(&storage);
|
||||
gpuDestroyDescriptorSetLayout(set_layout);
|
||||
gpuDestroyDescriptorPool(desc_pool);
|
||||
gpuDestroyCommandPool(pool);
|
||||
gpuDestroySemaphore(semaphore);
|
||||
gpuDestroySemaphore(image_acquired_semaphore);
|
||||
gpuDestroyFence(fence);
|
||||
gpuDestroyBuffer(&storage_buf);
|
||||
gpuDestroyBuffer(&vert_idx_buf);
|
||||
for (u64 i = 0; i < wpArrayCount(pipelines); ++i) {
|
||||
gpuDestroyPipeline(pipelines[i]);
|
||||
}
|
||||
gpuDestroyPipelineLayout(layout);
|
||||
gpuDestroyShader(shader);
|
||||
gpuDestroyCommandPool(pool);
|
||||
gpuDestroyBuffer(&vertex_buffer);
|
||||
gpuDestroySwapchain(swapchain);
|
||||
gpuDestroySurface(surface);
|
||||
gpuDestroyDevice(device);
|
||||
|
||||
@@ -1,59 +1,22 @@
|
||||
struct VSInput {
|
||||
float3 Pos;
|
||||
float3 Normal;
|
||||
float2 UV;
|
||||
float3 Color;
|
||||
};
|
||||
|
||||
static float2 positions[3] = float2[](
|
||||
float2( 0.0, -0.5),
|
||||
float2( 0.5, 0.5),
|
||||
float2(-0.5, 0.5),
|
||||
);
|
||||
|
||||
static float3 colors[3] = float3[](
|
||||
float3(1.0, 0.0, 0.0),
|
||||
float3(0.0, 1.0, 0.0),
|
||||
float3(0.0, 0.0, 1.0),
|
||||
);
|
||||
|
||||
[[vk::binding(0, 0)]]
|
||||
StructuredBuffer<float> a;
|
||||
|
||||
[[vk::binding(1, 0)]]
|
||||
StructuredBuffer<float> b;
|
||||
|
||||
[[vk::binding(2, 0)]]
|
||||
RWStructuredBuffer<float> result;
|
||||
|
||||
[[vk::push_constant]]
|
||||
uniform uint count;
|
||||
|
||||
struct VertexOutput {
|
||||
float3 color;
|
||||
float3 Color;
|
||||
float4 sv_position : SV_Position;
|
||||
float3 Normal;
|
||||
float2 UV;
|
||||
};
|
||||
|
||||
[shader("vertex")]
|
||||
VertexOutput vertMain(VSInput input, uint vid : SV_VertexID) {
|
||||
VertexOutput output;
|
||||
output.color = colors[vid];
|
||||
output.Color = input.Color;
|
||||
output.sv_position = float4(input.Pos, 1.0);
|
||||
output.Normal = input.Normal;
|
||||
output.UV = input.UV;
|
||||
return output;
|
||||
}
|
||||
|
||||
[shader("fragment")]
|
||||
float4 fragMain(VertexOutput inVert) : SV_Target {
|
||||
return float4(inVert.color, 1.0);
|
||||
}
|
||||
|
||||
[shader("compute")]
|
||||
[numthreads(64, 1, 1)]
|
||||
void compMain(uint tid : SV_DispatchThreadID) {
|
||||
if (tid < count) {
|
||||
result[tid] = a[tid] + b[tid];
|
||||
}
|
||||
return float4(inVert.Color, 1.0);
|
||||
}
|
||||
Reference in new issue
Block a user