Compare commits

...
3 Commits
Author SHA1 Message Date
abdelrahman 136853328a Render texture and fix camera target 2026-11-01 17:38:22 +00:00
abdelrahman 9536dbc550 Copy the temp buffer to the output image 2026-11-01 03:03:30 +00:00
abdelrahman 466e0345b8 Silence some warnings 2026-11-01 01:31:25 +00:00
5 changed files with 254 additions and 89 deletions

No files matched your search

+1 -1
View File
@@ -73,7 +73,7 @@ copy-assets:
@cp -r $(ASSETS_DIR) $(BUILDDIR) @cp -r $(ASSETS_DIR) $(BUILDDIR)
run: run:
@cd $(BUILDDIR) && ./$(OUT_NAME) @cd $(BUILDDIR) && VK_LAYER_MESSAGE_ID_FILTER=0x8adbf7e3,0x8b6f2f9a ./$(OUT_NAME)
clean: clean:
@rm -rf $(BUILD_ROOT) @rm -rf $(BUILD_ROOT)
+17 -7
View File
@@ -355,8 +355,8 @@ typedef struct GpuBufferCreateRequest {
} GpuBufferCreateRequest; } GpuBufferCreateRequest;
typedef struct GpuBufferFlushRequest { typedef struct GpuBufferFlushRequest {
GpuDevice *device;
GpuBuffer buffer; GpuBuffer buffer;
GpuDevice *device;
u64 offset; u64 offset;
u64 size; u64 size;
} GpuBufferFlushRequest; } GpuBufferFlushRequest;
@@ -1166,12 +1166,21 @@ typedef struct GpuCmdDispatchBaseRequest {
u32 group_count_z; u32 group_count_z;
} GpuCmdDispatchBaseRequest; } GpuCmdDispatchBaseRequest;
typedef struct GpuQueueSubmitRequest { typedef struct GpuBufferImageCopy {
GpuSemaphorePtrArray wait_semaphores; GpuImageSubresourceLayers image_subresource;
GpuPipelineStageFlagsArray wait_dst_stage_masks; GpuOffset3d image_offset;
GpuCommandBufferPtrArray command_buffers; GpuExtent3d image_extent;
GpuSemaphorePtrArray signal_semaphores; u64 buffer_offset;
} GpuQueueSubmitRequest; } GpuBufferImageCopy;
typedef GpuBufferImageCopy *GpuBufferImageCopyArray;
typedef struct GpuCmdCopyBufferToImageRequest {
GpuCommandBuffer *command_buffer;
GpuBuffer *src_buffer;
GpuImage *dst_image;
GpuImageLayout dst_image_layout;
GpuBufferImageCopyArray copy_regions;
} GpuCmdCopyBufferToImageRequest;
GpuInstance *gpuInit(const WpAllocator *allocator, GpuInitRequest request); GpuInstance *gpuInit(const WpAllocator *allocator, GpuInitRequest request);
GpuPhysicalDevice gpuFindPhysicalDevice(const WpAllocator *allocator, const WpAllocator *temp, GpuPhysicalDevice gpuFindPhysicalDevice(const WpAllocator *allocator, const WpAllocator *temp,
@@ -1252,6 +1261,7 @@ void gpuCmdDraw(GpuCmdDrawRequest request);
void gpuCmdDrawIndexed(GpuCmdDrawIndexedRequest request); void gpuCmdDrawIndexed(GpuCmdDrawIndexedRequest request);
void gpuCmdDispatch(GpuCmdDispatchRequest request); void gpuCmdDispatch(GpuCmdDispatchRequest request);
void gpuCmdDispatchBase(GpuCmdDispatchBaseRequest request); void gpuCmdDispatchBase(GpuCmdDispatchBaseRequest request);
void gpuCmdCopyBufferToImage(const WpAllocator *temp, GpuCmdCopyBufferToImageRequest request);
#ifdef WP_PLATFORM_CPP #ifdef WP_PLATFORM_CPP
END_C_LINKAGE END_C_LINKAGE
+148 -5
View File
@@ -77,6 +77,7 @@ typedef VkRenderingAttachmentInfo *VkRenderingAttachmen
typedef VkSemaphoreSubmitInfo *VkSemaphoreSubmitInfoArray; typedef VkSemaphoreSubmitInfo *VkSemaphoreSubmitInfoArray;
typedef VkCommandBufferSubmitInfo *VkCommandBufferSubmitInfoArray; typedef VkCommandBufferSubmitInfo *VkCommandBufferSubmitInfoArray;
typedef VkImageBlit2 *VkImageBlit2Array; typedef VkImageBlit2 *VkImageBlit2Array;
typedef VkBufferImageCopy *VkBufferImageCopyArray;
struct GpuInstance { struct GpuInstance {
VkInstance vk_instance; VkInstance vk_instance;
@@ -320,6 +321,8 @@ wp_intern u32 calcMaxMipLevelsForSize(ivec3s size);
GpuInstance *gpuInit(const WpAllocator *allocator, GpuInitRequest request) { GpuInstance *gpuInit(const WpAllocator *allocator, GpuInitRequest request) {
assertWithLogger(volkInitialize() == VK_SUCCESS, &logger, "Failed to initialise volk"); assertWithLogger(volkInitialize() == VK_SUCCESS, &logger, "Failed to initialise volk");
stbi_set_flip_vertically_on_load(true);
VpProfileProperties profile = { VpProfileProperties profile = {
.profileName = VP_TEST_WINDOW_RENDERER_NAME, .profileName = VP_TEST_WINDOW_RENDERER_NAME,
.specVersion = VP_TEST_WINDOW_RENDERER_SPEC_VERSION, .specVersion = VP_TEST_WINDOW_RENDERER_SPEC_VERSION,
@@ -858,6 +861,8 @@ GpuSampler *gpuCreateSampler(const WpAllocator *allocator, GpuSamplerCreateReque
wpLogDebug(&logger, wpStr8Lit("Created Vulkan sampler")); wpLogDebug(&logger, wpStr8Lit("Created Vulkan sampler"));
output->device = request.device;
return output; return output;
} }
@@ -890,8 +895,24 @@ GpuImage *gpuCreateImage(const WpAllocator *allocator, GpuImageCreateRequest req
.initialLayout = vkImageLayoutFromGpuImageLayout(request.layout), .initialLayout = vkImageLayoutFromGpuImageLayout(request.layout),
}; };
// Check if the image requires a dedicated allocation
VkMemoryDedicatedRequirements dedicated_memory_reqs = {
.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS,
};
VkMemoryRequirements2 memory_reqs = {
.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
.pNext = &dedicated_memory_reqs,
};
VkDeviceImageMemoryRequirements image_memory_reqs = {
.sType = VK_STRUCTURE_TYPE_DEVICE_IMAGE_MEMORY_REQUIREMENTS,
.pCreateInfo = &image_create_info,
};
vkGetDeviceImageMemoryRequirements(request.device->vk_device, &image_memory_reqs, &memory_reqs);
b8 requires_dedicated_memory = dedicated_memory_reqs.requiresDedicatedAllocation ||
dedicated_memory_reqs.prefersDedicatedAllocation;
VmaAllocationCreateInfo alloc_create_info = { VmaAllocationCreateInfo alloc_create_info = {
.flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT, .flags = requires_dedicated_memory ? VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT : 0,
.usage = VMA_MEMORY_USAGE_AUTO, .usage = VMA_MEMORY_USAGE_AUTO,
}; };
@@ -934,11 +955,14 @@ GpuImage *gpuLoadImageFromFile(const WpAllocator *allocator, const WpAllocator *
wpStr8CopyToCstr(buf, &request.filename, request.filename.size + 1); wpStr8CopyToCstr(buf, &request.filename, request.filename.size + 1);
i32 width, height, channels; i32 width, height, channels;
const u8 *data = stbi_load(buf, &width, &height, &channels, 4); i32 requested_channels = 4;
const u8 *data = stbi_load(buf, &width, &height, &channels, requested_channels);
assertWithLogger(data != NULL, &logger, "Failed to load image data"); assertWithLogger(data != NULL, &logger, "Failed to load image data");
GpuDevice *device = request.command_pool->device;
GpuImageCreateRequest img_request = { GpuImageCreateRequest img_request = {
.device = request.command_pool->device, .device = device,
.size = { .x = width, .y = height, .z = 1 }, .size = { .x = width, .y = height, .z = 1 },
.type = GPU_IMAGE_TYPE_2D, .type = GPU_IMAGE_TYPE_2D,
.format = GPU_FORMAT_R8G8B8A8_SRGB, .format = GPU_FORMAT_R8G8B8A8_SRGB,
@@ -956,14 +980,15 @@ GpuImage *gpuLoadImageFromFile(const WpAllocator *allocator, const WpAllocator *
}; };
GpuImage *output = gpuCreateImage(allocator, img_request); GpuImage *output = gpuCreateImage(allocator, img_request);
u64 data_size = width * height * channels * sizeof(u8); u64 data_size = width * height * requested_channels * sizeof(u8);
GpuBufferCreateRequest temp_buf_request = { GpuBufferCreateRequest temp_buf_request = {
.device = request.command_pool->device, .device = device,
.size = data_size, .size = data_size,
.usage = GPU_BUFFER_USAGE_TRANSFER_SRC, .usage = GPU_BUFFER_USAGE_TRANSFER_SRC,
}; };
GpuBuffer temp_storage = gpuCreateBuffer(allocator, temp_buf_request); GpuBuffer temp_storage = gpuCreateBuffer(allocator, temp_buf_request);
memcpy(temp_storage.cpu_mapping, data, data_size); memcpy(temp_storage.cpu_mapping, data, data_size);
gpuFlushBuffer((GpuBufferFlushRequest){ .device = device, .buffer = temp_storage });
stbi_image_free((void *)data); stbi_image_free((void *)data);
@@ -974,6 +999,95 @@ GpuImage *gpuLoadImageFromFile(const WpAllocator *allocator, const WpAllocator *
GpuCommandBufferPtrArray cbs = gpuAllocateCommandBuffers(allocator, temp, cb_request); GpuCommandBufferPtrArray cbs = gpuAllocateCommandBuffers(allocator, temp, cb_request);
GpuCommandBuffer *cb = cbs[0]; GpuCommandBuffer *cb = cbs[0];
gpuBeginCommandBuffer((GpuCommandBufferBeginRequest){ .command_buffer = cb, .one_time_submit = true });
GpuImageMemoryBarrier image_barrier = {
.image = output,
.src_stage_mask = GPU_PIPELINE_STAGE_NONE,
.src_access_mask = GPU_ACCESS_NONE,
.dst_stage_mask = GPU_PIPELINE_STAGE_TRANSFER,
.dst_access_mask = GPU_ACCESS_TRANSFER_WRITE,
.old_layout = GPU_IMAGE_LAYOUT_UNDEFINED,
.new_layout = GPU_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
.subresource = {
.aspect = GPU_IMAGE_ASPECT_COLOR,
.level_count = 1,
.layer_count = 1,
}
};
GpuCmdPipelineBarrierRequest barrier_req = {
.command_buffer = cb,
.info = (GpuDependencyInfo){
.image_memory_barriers = wpArray(GpuImageMemoryBarrier, image_barrier),
},
};
gpuCmdPipelineBarrier(temp, barrier_req);
GpuCmdCopyBufferToImageRequest copy_req = {
.command_buffer = cb,
.src_buffer = &temp_storage,
.dst_image = output,
.dst_image_layout = GPU_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
.copy_regions = wpArray(
GpuBufferImageCopy,
(GpuBufferImageCopy){
.image_subresource = { .layer_count = 1, .aspect_mask = GPU_IMAGE_ASPECT_COLOR },
.image_extent = { .width = width, .height = height, .depth = 1 },
}
),
};
gpuCmdCopyBufferToImage(temp, copy_req);
image_barrier = (GpuImageMemoryBarrier){
.image = output,
.src_stage_mask = GPU_PIPELINE_STAGE_TRANSFER,
.src_access_mask = GPU_ACCESS_TRANSFER_WRITE,
.dst_stage_mask = GPU_PIPELINE_STAGE_FRAGMENT_SHADER,
.dst_access_mask = GPU_ACCESS_SHADER_READ,
.old_layout = GPU_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
.new_layout = GPU_IMAGE_LAYOUT_READ_ONLY_OPTIMAL,
.subresource = {
.aspect = GPU_IMAGE_ASPECT_COLOR,
.level_count = 1,
.layer_count = 1,
}
};
barrier_req = (GpuCmdPipelineBarrierRequest){
.command_buffer = cb,
.info = (GpuDependencyInfo){
.image_memory_barriers = wpArray(GpuImageMemoryBarrier, image_barrier),
},
};
gpuCmdPipelineBarrier(temp, barrier_req);
gpuEndCommandBuffer(cb);
GpuFence *fence = gpuCreateFence(allocator, device);
GpuResetFencesRequest reset_req = {
.device = device,
.fences = wpArray(GpuFence *, fence)
};
gpuResetFences(temp, reset_req);
GpuCommandBuffersSubmitRequest submit_req = {
.device = device,
.command_buffers = cbs,
.fence = fence,
};
gpuSubmitCommandBuffers(temp, submit_req);
GpuWaitForFencesRequest wait_req = {
.device = device,
.fences = wpArray(GpuFence *, fence),
.timeout = UINT64_MAX,
};
gpuWaitForFences(temp, wait_req);
gpuDestroyFence(fence);
gpuDestroyBuffer(&temp_storage);
return output; return output;
} }
@@ -2193,6 +2307,35 @@ void gpuCmdDispatchBase(GpuCmdDispatchBaseRequest request) {
request.group_count_z); request.group_count_z);
} }
void gpuCmdCopyBufferToImage(const WpAllocator *temp, GpuCmdCopyBufferToImageRequest request) {
u64 region_count = wpArrayCount(request.copy_regions);
VkBufferImageCopyArray copy_regions = wpArrayAllocCapacity(VkBufferImageCopy, temp, region_count,
WP_ARRAY_INIT_FILLED);
assertWithLogger(copy_regions != NULL, &logger, "Failed to allocate temp copy buffer to image arrays");
for (u64 i = 0; i < region_count; ++i) {
copy_regions[i] = (VkBufferImageCopy){
.bufferOffset = request.copy_regions[i].buffer_offset,
.imageSubresource = (VkImageSubresourceLayers){
.aspectMask = vkImageAspectFromGpuImageAspect(request.copy_regions[i].image_subresource.aspect_mask),
.baseArrayLayer = request.copy_regions[i].image_subresource.base_array_layer,
.layerCount = request.copy_regions[i].image_subresource.layer_count,
.mipLevel = request.copy_regions[i].image_subresource.mip_level,
},
.imageOffset = (VkOffset3D){ .x = request.copy_regions[i].image_offset.x,
.y = request.copy_regions[i].image_offset.y,
.z = request.copy_regions[i].image_offset.z },
.imageExtent = (VkExtent3D){ .width = request.copy_regions[i].image_extent.width,
.height = request.copy_regions[i].image_extent.height,
.depth = request.copy_regions[i].image_extent.depth },
};
}
vkCmdCopyBufferToImage(request.command_buffer->vk_buffer, request.src_buffer->internal->vk_buffer,
request.dst_image->vk_image, vkImageLayoutFromGpuImageLayout(request.dst_image_layout),
wpArrayCount(copy_regions), copy_regions);
}
wp_intern VkExtensions getInstanceExtensions(void) { wp_intern VkExtensions getInstanceExtensions(void) {
Config config = configGet(); Config config = configGet();
VkExtensions output = {0}; VkExtensions output = {0};
+79 -72
View File
@@ -20,7 +20,7 @@ enum Errors {
typedef struct Vertex { typedef struct Vertex {
vec3s position; vec3s position;
vec3s color; vec3s color;
// vec2s uv; vec2s uv;
} Vertex; } Vertex;
typedef Vertex *VertexArray; typedef Vertex *VertexArray;
@@ -33,7 +33,6 @@ typedef ShaderData *ShaderDataArray;
typedef struct Camera { typedef struct Camera {
vec3s position; vec3s position;
vec3s target;
vec3s up; vec3s up;
} Camera; } Camera;
@@ -48,19 +47,23 @@ wp_intern const VertexArray vertices = wpArray(
Vertex, Vertex,
(Vertex){ (Vertex){
.position = (vec3s){ .x = -0.5f, .y = -0.5f, .z = 0.0f }, .position = (vec3s){ .x = -0.5f, .y = -0.5f, .z = 0.0f },
.color = (vec3s){ .r = 1.0f, .g = 0.0f, .b = 0.0f }, .color = (vec3s){ .r = 1.0f, .g = 1.0f, .b = 1.0f },
.uv = (vec2s){ .x = 0.0f, .y = 0.0f },
}, },
(Vertex){ (Vertex){
.position = (vec3s){ .x = -0.5f, .y = 0.5f, .z = 0.0f }, .position = (vec3s){ .x = -0.5f, .y = 0.5f, .z = 0.0f },
.color = (vec3s){ .r = 0.0f, .g = 1.0f, .b = 0.0f }, .color = (vec3s){ .r = 1.0f, .g = 1.0f, .b = 1.0f },
.uv = (vec2s){ .x = 0.0f, .y = 1.0f },
}, },
(Vertex){ (Vertex){
.position = (vec3s){ .x = 0.5f, .y = -0.5f, .z = 0.0f }, .position = (vec3s){ .x = 0.5f, .y = -0.5f, .z = 0.0f },
.color = (vec3s){ .r = 0.0f, .g = 0.0f, .b = 1.0f }, .color = (vec3s){ .r = 1.0f, .g = 1.0f, .b = 1.0f },
.uv = (vec2s){ .x = 1.0f, .y = 0.0f },
}, },
(Vertex){ (Vertex){
.position = (vec3s){ .x = 0.5f, .y = 0.5f, .z = 0.0f }, .position = (vec3s){ .x = 0.5f, .y = 0.5f, .z = 0.0f },
.color = (vec3s){ .r = 1.0f, .g = 1.0f, .b = 1.0f }, .color = (vec3s){ .r = 1.0f, .g = 1.0f, .b = 1.0f },
.uv = (vec2s){ .x = 1.0f, .y = 1.0f },
}, },
); );
wp_intern const WpU16Array indices = wpArray( wp_intern const WpU16Array indices = wpArray(
@@ -69,6 +72,7 @@ wp_intern const WpU16Array indices = wpArray(
1, 3, 2, 1, 3, 2,
); );
wp_intern mat4s calculateModelViewProjection(vec3s translation, Camera camera, Projection projection);
wp_intern mat4s modelViewProjection(mat4s model, mat4s view, mat4s proj); wp_intern mat4s modelViewProjection(mat4s model, mat4s view, mat4s proj);
i32 main(void) { i32 main(void) {
@@ -165,11 +169,22 @@ i32 main(void) {
wpFileRead(code, spirv, code_size); wpFileRead(code, spirv, code_size);
// GpuLoadImageFromFileRequest load_img_req = { GpuLoadImageFromFileRequest load_img_req = {
// .command_pool = pool, .command_pool = pool,
// .filename = wpStr8LitRo("assets/collections.png"), .filename = wpStr8LitRo("assets/collections.png"),
// }; };
// GpuImage *texture = gpuLoadImageFromFile(&arena, &temp_arena, load_img_req); GpuImage *texture = gpuLoadImageFromFile(&arena, &temp_arena, load_img_req);
GpuSamplerCreateRequest sampler_req = {
.device = device,
.min_filter = GPU_FILTER_LINEAR,
.mag_filter = GPU_FILTER_LINEAR,
.address_mode_u = GPU_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
.address_mode_v = GPU_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
.address_mode_w = GPU_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
.mipmap_mode = GPU_SAMPLER_MIPMAP_MODE_LINEAR,
};
GpuSampler *sampler = gpuCreateSampler(&arena, sampler_req);
GpuShaderCreateRequest shader_request = { GpuShaderCreateRequest shader_request = {
.device = device, .device = device,
@@ -194,7 +209,6 @@ i32 main(void) {
Camera camera = { Camera camera = {
.position = { .x = 0.0f, .y = 0.0f, .z = 2.0f }, .position = { .x = 0.0f, .y = 0.0f, .z = 2.0f },
.target = { 0 },
.up = { 0.0f, 1.0f, 0.0f }, .up = { 0.0f, 1.0f, 0.0f },
}; };
Projection projection = { Projection projection = {
@@ -207,18 +221,10 @@ i32 main(void) {
ShaderDataArray data = wpArray( ShaderDataArray data = wpArray(
ShaderData, ShaderData,
(ShaderData){ (ShaderData){
.mvp = modelViewProjection( .mvp = calculateModelViewProjection(translations[0], camera, projection),
glms_translate(glms_mat4_identity(), translations[0]),
glms_lookat(camera.position, camera.target, camera.up),
glms_perspective(glm_rad(projection.fov_deg), projection.aspect, projection.near, projection.far)
),
}, },
(ShaderData){ (ShaderData){
.mvp = modelViewProjection( .mvp = calculateModelViewProjection(translations[1], camera, projection),
glms_translate(glms_mat4_identity(), translations[1]),
glms_lookat(camera.position, camera.target, camera.up),
glms_perspective(glm_rad(projection.fov_deg), projection.aspect, projection.near, projection.far)
),
}, },
); );
memcpy(shader_data.cpu_mapping, data, wpArrayItemSize(data) * wpArrayCount(data)); memcpy(shader_data.cpu_mapping, data, wpArrayItemSize(data) * wpArrayCount(data));
@@ -226,7 +232,8 @@ i32 main(void) {
GpuDescriptorPoolSizeArray pool_sizes = wpArray( GpuDescriptorPoolSizeArray pool_sizes = wpArray(
GpuDescriptorPoolSize, GpuDescriptorPoolSize,
(GpuDescriptorPoolSize){ .type = GPU_DESCRIPTOR_TYPE_STORAGE_BUFFER, .count = 1 } (GpuDescriptorPoolSize){ .type = GPU_DESCRIPTOR_TYPE_STORAGE_BUFFER, .count = 1 },
(GpuDescriptorPoolSize){ .type = GPU_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, .count = 1 }
); );
GpuDescriptorPoolCreateRequest desc_pool_req = { GpuDescriptorPoolCreateRequest desc_pool_req = {
.descriptor_pool_sizes = pool_sizes, .descriptor_pool_sizes = pool_sizes,
@@ -242,6 +249,12 @@ i32 main(void) {
.type = GPU_DESCRIPTOR_TYPE_STORAGE_BUFFER, .type = GPU_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.descriptor_count = 1, .descriptor_count = 1,
.stages = GPU_SHADER_STAGE_VERTEX, .stages = GPU_SHADER_STAGE_VERTEX,
},
(GpuDescriptorSetBinding){
.binding = 1,
.type = GPU_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.descriptor_count = 1,
.stages = GPU_SHADER_STAGE_FRAGMENT,
} }
); );
GpuDescriptorSetLayoutCreateRequest desc_set_layout_req = { GpuDescriptorSetLayoutCreateRequest desc_set_layout_req = {
@@ -259,14 +272,28 @@ i32 main(void) {
GpuDescriptorSetWriteInfo shader_data_write_info = { GpuDescriptorSetWriteInfo shader_data_write_info = {
.set = descriptor_sets[0], .set = descriptor_sets[0],
.descriptor_type = GPU_DESCRIPTOR_TYPE_STORAGE_BUFFER, .descriptor_type = GPU_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.dst_binding = 0,
.buffer_infos = wpArray( .buffer_infos = wpArray(
GpuDescriptorBufferInfo, GpuDescriptorBufferInfo,
(GpuDescriptorBufferInfo){ .buffer = &shader_data }, (GpuDescriptorBufferInfo){ .buffer = &shader_data },
), ),
}; };
GpuDescriptorSetWriteInfo texture_write_info = {
.set = descriptor_sets[0],
.descriptor_type = GPU_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.dst_binding = 1,
.image_infos = wpArray(
GpuDescriptorImageInfo,
(GpuDescriptorImageInfo){
.image = texture,
.image_layout = GPU_IMAGE_LAYOUT_READ_ONLY_OPTIMAL,
.sampler = sampler,
}
),
};
GpuDescriptorSetWriteRequest desc_set_req = { GpuDescriptorSetWriteRequest desc_set_req = {
.device = device, .device = device,
.write_infos = wpArray(GpuDescriptorSetWriteInfo, shader_data_write_info), .write_infos = wpArray(GpuDescriptorSetWriteInfo, shader_data_write_info, texture_write_info),
}; };
gpuWriteDescriptorSets(&temp_arena, desc_set_req); gpuWriteDescriptorSets(&temp_arena, desc_set_req);
@@ -295,12 +322,12 @@ i32 main(void) {
.offset = offsetof(Vertex, color), .offset = offsetof(Vertex, color),
.format = GPU_FORMAT_R32G32B32_SFLOAT, .format = GPU_FORMAT_R32G32B32_SFLOAT,
}, },
// (GpuVertexInputAttribute){ (GpuVertexInputAttribute){
// .location = 2, .location = 2,
// .binding = 0, .binding = 0,
// .offset = offsetof(Vertex, uv), .offset = offsetof(Vertex, uv),
// .format = GPU_FORMAT_R32G32_SFLOAT, .format = GPU_FORMAT_R32G32_SFLOAT,
// }, },
); );
GpuPipelineVertexInputStateInfo vert_info = { GpuPipelineVertexInputStateInfo vert_info = {
.bindings = wpArray(GpuVertexInputBinding, vert_binding), .bindings = wpArray(GpuVertexInputBinding, vert_binding),
@@ -376,63 +403,29 @@ i32 main(void) {
if (windowIsKeyPressed(KEYBOARD_KEY_UP)) { if (windowIsKeyPressed(KEYBOARD_KEY_UP)) {
camera.position.z -= 0.01f; camera.position.z -= 0.01f;
data[0].mvp = modelViewProjection( data[0].mvp = calculateModelViewProjection(translations[0], camera, projection);
glms_translate(glms_mat4_identity(), translations[0]), data[1].mvp = calculateModelViewProjection(translations[1], camera, projection);
glms_lookat(camera.position, camera.target, camera.up),
glms_perspective(glm_rad(projection.fov_deg), projection.aspect, projection.near, projection.far)
);
data[1].mvp = modelViewProjection(
glms_translate(glms_mat4_identity(), translations[1]),
glms_lookat(camera.position, camera.target, camera.up),
glms_perspective(glm_rad(projection.fov_deg), projection.aspect, projection.near, projection.far)
);
memcpy(shader_data.cpu_mapping, data, wpArrayItemSize(data) * wpArrayCount(data)); memcpy(shader_data.cpu_mapping, data, wpArrayItemSize(data) * wpArrayCount(data));
gpuFlushBuffer((GpuBufferFlushRequest){ .device = device, .buffer = shader_data }); gpuFlushBuffer((GpuBufferFlushRequest){ .device = device, .buffer = shader_data });
} }
if (windowIsKeyPressed(KEYBOARD_KEY_DOWN)) { if (windowIsKeyPressed(KEYBOARD_KEY_DOWN)) {
camera.position.z += 0.01f; camera.position.z += 0.01f;
data[0].mvp = modelViewProjection( data[0].mvp = calculateModelViewProjection(translations[0], camera, projection);
glms_translate(glms_mat4_identity(), translations[0]), data[1].mvp = calculateModelViewProjection(translations[1], camera, projection);
glms_lookat(camera.position, camera.target, camera.up),
glms_perspective(glm_rad(projection.fov_deg), projection.aspect, projection.near, projection.far)
);
data[1].mvp = modelViewProjection(
glms_translate(glms_mat4_identity(), translations[1]),
glms_lookat(camera.position, camera.target, camera.up),
glms_perspective(glm_rad(projection.fov_deg), projection.aspect, projection.near, projection.far)
);
memcpy(shader_data.cpu_mapping, data, wpArrayItemSize(data) * wpArrayCount(data)); memcpy(shader_data.cpu_mapping, data, wpArrayItemSize(data) * wpArrayCount(data));
gpuFlushBuffer((GpuBufferFlushRequest){ .device = device, .buffer = shader_data }); gpuFlushBuffer((GpuBufferFlushRequest){ .device = device, .buffer = shader_data });
} }
if (windowIsKeyPressed(KEYBOARD_KEY_RIGHT)) { if (windowIsKeyPressed(KEYBOARD_KEY_RIGHT)) {
camera.position.x += 0.01f; camera.position.x += 0.01f;
camera.target.x += 0.01f; data[0].mvp = calculateModelViewProjection(translations[0], camera, projection);
data[0].mvp = modelViewProjection( data[1].mvp = calculateModelViewProjection(translations[1], camera, projection);
glms_translate(glms_mat4_identity(), translations[0]),
glms_lookat(camera.position, camera.target, camera.up),
glms_perspective(glm_rad(projection.fov_deg), projection.aspect, projection.near, projection.far)
);
data[1].mvp = modelViewProjection(
glms_translate(glms_mat4_identity(), translations[1]),
glms_lookat(camera.position, camera.target, camera.up),
glms_perspective(glm_rad(projection.fov_deg), projection.aspect, projection.near, projection.far)
);
memcpy(shader_data.cpu_mapping, data, wpArrayItemSize(data) * wpArrayCount(data)); memcpy(shader_data.cpu_mapping, data, wpArrayItemSize(data) * wpArrayCount(data));
gpuFlushBuffer((GpuBufferFlushRequest){ .device = device, .buffer = shader_data }); gpuFlushBuffer((GpuBufferFlushRequest){ .device = device, .buffer = shader_data });
} }
if (windowIsKeyPressed(KEYBOARD_KEY_LEFT)) { if (windowIsKeyPressed(KEYBOARD_KEY_LEFT)) {
camera.position.x -= 0.01f; camera.position.x -= 0.01f;
camera.target.x -= 0.01f; data[0].mvp = calculateModelViewProjection(translations[0], camera, projection);
data[0].mvp = modelViewProjection( data[1].mvp = calculateModelViewProjection(translations[1], camera, projection);
glms_translate(glms_mat4_identity(), translations[0]),
glms_lookat(camera.position, camera.target, camera.up),
glms_perspective(glm_rad(projection.fov_deg), projection.aspect, projection.near, projection.far)
);
data[1].mvp = modelViewProjection(
glms_translate(glms_mat4_identity(), translations[1]),
glms_lookat(camera.position, camera.target, camera.up),
glms_perspective(glm_rad(projection.fov_deg), projection.aspect, projection.near, projection.far)
);
memcpy(shader_data.cpu_mapping, data, wpArrayItemSize(data) * wpArrayCount(data)); memcpy(shader_data.cpu_mapping, data, wpArrayItemSize(data) * wpArrayCount(data));
gpuFlushBuffer((GpuBufferFlushRequest){ .device = device, .buffer = shader_data }); gpuFlushBuffer((GpuBufferFlushRequest){ .device = device, .buffer = shader_data });
} }
@@ -626,7 +619,8 @@ i32 main(void) {
gpuDestroyDescriptorPool(desc_pool); gpuDestroyDescriptorPool(desc_pool);
gpuDestroyBuffer(&shader_data); gpuDestroyBuffer(&shader_data);
gpuDestroyShader(shader); gpuDestroyShader(shader);
// gpuDestroyImage(texture); gpuDestroySampler(sampler);
gpuDestroyImage(texture);
gpuDestroyCommandPool(pool); gpuDestroyCommandPool(pool);
gpuDestroyBuffer(&vert_idx_buffer); gpuDestroyBuffer(&vert_idx_buffer);
gpuDestroySwapchain(swapchain); gpuDestroySwapchain(swapchain);
@@ -645,6 +639,19 @@ i32 main(void) {
return ERROR_NONE; return ERROR_NONE;
} }
wp_intern mat4s calculateModelViewProjection(vec3s translation, Camera camera, Projection projection) {
vec3s target = {
.x = camera.position.x,
.y = camera.position.y,
.z = camera.position.z - 2.0f
};
return modelViewProjection(
glms_translate(glms_mat4_identity(), translation),
glms_lookat(camera.position, target, camera.up),
glms_perspective(glm_rad(projection.fov_deg), projection.aspect, projection.near, projection.far)
);
}
wp_intern mat4s modelViewProjection(mat4s model, mat4s view, mat4s proj) { wp_intern mat4s modelViewProjection(mat4s model, mat4s view, mat4s proj) {
// Matrix to flip the Y-axis since in Vulkan Y points down // Matrix to flip the Y-axis since in Vulkan Y points down
mat4s clip = { mat4s clip = {
+9 -4
View File
@@ -1,7 +1,7 @@
struct VSInput { struct VSInput {
float3 Pos; float3 Pos;
float3 Color; float3 Color;
// float2 Uv; float2 Uv;
}; };
struct ShaderData { struct ShaderData {
@@ -9,20 +9,25 @@ struct ShaderData {
}; };
StructuredBuffer<ShaderData> data; StructuredBuffer<ShaderData> data;
Sampler2D texture;
struct VertexOutput { struct VertexOutput {
float3 Color;
float4 sv_position : SV_Position; float4 sv_position : SV_Position;
float3 Color;
float2 Uv;
}; };
[shader("vertex")] [shader("vertex")]
VertexOutput vertMain(VSInput input, uint idx: SV_InstanceID) { VertexOutput vertMain(VSInput input, uint idx: SV_InstanceID) {
VertexOutput output; VertexOutput output;
output.Color = input.Color;
output.sv_position = mul(data[idx].mvp, float4(input.Pos, 1.0)); output.sv_position = mul(data[idx].mvp, float4(input.Pos, 1.0));
output.Color = input.Color;
output.Uv = input.Uv;
return output; return output;
} }
[shader("fragment")] [shader("fragment")]
float4 fragMain(VertexOutput inVert) : SV_Target { float4 fragMain(VertexOutput inVert) : SV_Target {
return float4(inVert.Color, 1.0); float4 color = texture.Sample(inVert.Uv) * float4(inVert.Color, 1.0f);
return color;
} }