gryphn + vulkan stuff for depth textures

This commit is contained in:
Gregory Wells
2025-06-17 14:32:56 -04:00
parent e5a11b1ef4
commit 86f2ac6e5a
11 changed files with 99 additions and 48 deletions

View File

@@ -23,11 +23,26 @@ VkImageLayout vkGryphnImageLayout(gnImageLayout layout) {
case GN_LAYOUT_UNDEFINED: return VK_IMAGE_LAYOUT_UNDEFINED;
case GN_LAYOUT_PRESENTATION_QUEUE_IMAGE: return VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
case GN_LAYOUT_TRANSFER_DESTINATION: return VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
case GN_COLOR_ATTACHMENT: return VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
case GN_LAYOUT_COLOR_ATTACHMENT: return VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
case GN_LAYOUT_DEPTH_STENCIL: return VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
}
}
gnReturnCode gnCreateRenderPassDescriptorFn(struct gnRenderPassDescriptor_t* renderPass, struct gnOutputDevice_t* device, struct gnRenderPassDescriptorInfo_t info) {
VkPipelineStageFlags vkGryphnRenderPassStage(gnRenderPassStage stage) {
VkPipelineStageFlags flags = 0;
if ((stage & GN_COLOR_ATTACHMENT_OUTPUT) == GN_COLOR_ATTACHMENT_OUTPUT) flags |= VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
if ((stage & GN_EARLY_FRAGMENT_TEST) == GN_EARLY_FRAGMENT_TEST) flags |= VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
return flags;
}
VkAccessFlags vkGryphnRenderPassAccess(gnRenderPassAccess access) {
VkAccessFlags flags = 0;
if ((flags & GN_COLOR_ATTACHMENT_WRITE) == GN_COLOR_ATTACHMENT_WRITE) flags |= VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
if ((flags & GN_DEPTH_STENCIL_WRITE) == GN_DEPTH_STENCIL_WRITE) flags |= VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
return flags;
}
gnReturnCode gnCreateRenderPassDescriptorFn(struct gnRenderPassDescriptor_t* renderPass, struct gnOutputDevice_t* device, gnRenderPassDescriptorInfo info) {
renderPass->renderPassDescriptor = malloc(sizeof(gnPlatformRenderPassDescriptor));
renderPass->renderPassDescriptor->attachmentCount = info.attachmentCount;
@@ -50,11 +65,7 @@ gnReturnCode gnCreateRenderPassDescriptorFn(struct gnRenderPassDescriptor_t* ren
renderPass->renderPassDescriptor->subpassCount = info.subpassCount;
renderPass->renderPassDescriptor->subpasses = malloc(sizeof(VkSubpassDescription) * info.subpassCount);
renderPass->renderPassDescriptor->colorAttachments = malloc(sizeof(VkAttachmentReference*) * info.subpassCount);
VkAttachmentReference ref = {
.attachment = 0,
.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
};
renderPass->renderPassDescriptor->depthAttachments = malloc(sizeof(VkAttachmentReference) * info.subpassCount);
for (int i = 0; i < info.subpassCount; i++) {
renderPass->renderPassDescriptor->colorAttachments[i] = malloc(sizeof(VkAttachmentReference) * info.subpassInfos[i].colorAttachmentCount);
@@ -66,13 +77,20 @@ gnReturnCode gnCreateRenderPassDescriptorFn(struct gnRenderPassDescriptor_t* ren
};
}
renderPass->renderPassDescriptor->subpasses[i] = (VkSubpassDescription){
.flags = 0,
.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
.colorAttachmentCount = info.subpassInfos[i].colorAttachmentCount,
.pColorAttachments = renderPass->renderPassDescriptor->colorAttachments[i]
};
if (info.subpassInfos[i].depthAttachment != NULL) {
renderPass->renderPassDescriptor->depthAttachments[i] = (VkAttachmentReference){
.attachment = info.subpassInfos[i].depthAttachment->index,
.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
};
renderPass->renderPassDescriptor->subpasses[i].pDepthStencilAttachment = &renderPass->renderPassDescriptor->depthAttachments[i];
}
}
renderPass->renderPassDescriptor->dependencies = malloc(sizeof(VkSubpassDependency) * info.dependencyCount);
@@ -80,10 +98,10 @@ gnReturnCode gnCreateRenderPassDescriptorFn(struct gnRenderPassDescriptor_t* ren
renderPass->renderPassDescriptor->dependencies[i] = (VkSubpassDependency) {
.srcSubpass = (info.dependencies[i].source == GN_SUBPASS_EXTERNAL) ? VK_SUBPASS_EXTERNAL : info.dependencies[i].source,
.dstSubpass = (info.dependencies[i].destination == GN_SUBPASS_EXTERNAL) ? VK_SUBPASS_EXTERNAL : info.dependencies[i].destination,
.srcStageMask = info.dependencies[i].soruceStageMask,
.srcAccessMask = info.dependencies[i].sourceAccessMask,
.dstStageMask = info.dependencies[i].destinationStageMask,
.dstAccessMask = info.dependencies[i].destinationAccessMask
.srcStageMask = vkGryphnRenderPassStage(info.dependencies[i].soruceStageMask),
.srcAccessMask = vkGryphnRenderPassAccess(info.dependencies[i].sourceAccessMask),
.dstStageMask = vkGryphnRenderPassStage(info.dependencies[i].destinationStageMask),
.dstAccessMask = vkGryphnRenderPassAccess(info.dependencies[i].destinationAccessMask)
};
}
@@ -113,4 +131,5 @@ void gnDestroyRenderPassDescriptorFn(gnRenderPassDescriptor renderPass) {
free(renderPass->renderPassDescriptor->attachments);
free(renderPass->renderPassDescriptor->subpasses);
free(renderPass->renderPassDescriptor->dependencies);
free(renderPass->renderPassDescriptor->depthAttachments);
}

View File

@@ -13,4 +13,7 @@ typedef struct gnPlatformRenderPassDescriptor_t {
VkSubpassDependency* dependencies;
VkAttachmentReference** colorAttachments;
VkAttachmentReference* depthAttachments;
} gnPlatformRenderPassDescriptor;
VkPipelineStageFlags vkGryphnRenderPassStage(gnRenderPassStage stage);

View File

@@ -4,13 +4,8 @@
#include "sync/fence/vulkan_fence.h"
#include "commands/command_buffer/vulkan_command_buffer.h"
#include "output_device/vulkan_output_devices.h"
#include "core/renderpass/gryphn_render_pass_descriptor.h"
#include "renderpass/vulkan_render_pass_descriptor.h"
VkPipelineStageFlags vkGryphnWaitStage(enum gnRenderPassStage_e stage) {
switch(stage) {
case GN_COLOR_ATTACHMENT_OUTPUT: return VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
}
}
gnReturnCode gnSubmitFn(struct gnOutputDevice_t* device, struct gnSubmitInfo_t info) {
VK_SUBPASS_EXTERNAL;
@@ -18,7 +13,7 @@ gnReturnCode gnSubmitFn(struct gnOutputDevice_t* device, struct gnSubmitInfo_t i
VkSemaphore* waitSemaphores = malloc(sizeof(VkSemaphore) * info.waitCount);
VkPipelineStageFlags* waitStages = malloc(sizeof(VkPipelineStageFlags) * info.waitCount);
for (int i = 0; i < info.waitCount; i++) waitSemaphores[i] = info.waitSemaphores[i]->semaphore->semaphore;
for (int i = 0; i < info.waitCount; i++) waitStages[i] = vkGryphnWaitStage(info.waitStages[i]);
for (int i = 0; i < info.waitCount; i++) waitStages[i] = vkGryphnRenderPassStage(info.waitStages[i]);
VkCommandBuffer* commandBuffers = malloc(sizeof(VkCommandBuffer) * info.commandBufferCount);
for (int i = 0; i < info.commandBufferCount; i++) commandBuffers[i] = info.commandBuffers[i]->commandBuffer->buffer;

View File

@@ -25,7 +25,18 @@ VkSamplerAddressMode vkGryphnTextureWrap(gnTextureWrap wrap) {
}
}
void VkTransitionImageLayout(gnDevice device, VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout) {
VkImageAspectFlags vkGryphnGetAspectFlags(gnImageFormat format) {
VkImageAspectFlags aspectMask = 0;
if (format == GN_FORMAT_D32S8_UINT || format == GN_FORMAT_D24S8_UINT) aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
else { aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; }
return aspectMask;
}
gnBool vkGryphnIsDepthStencil(gnImageFormat format) { return (format == GN_FORMAT_D32S8_UINT || format == GN_FORMAT_D24S8_UINT); }
void VkTransitionImageLayout(gnDevice device, VkImage image, gnImageFormat format, VkImageLayout oldLayout, VkImageLayout newLayout) {
VkCommandBuffer transferBuffer = gnBeginVulkanTransferOperation(device);
VkPipelineStageFlags sourceStage, destinationStage;
@@ -43,7 +54,13 @@ void VkTransitionImageLayout(gnDevice device, VkImage image, VkFormat format, Vk
destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
destinationAccessMask = VK_ACCESS_SHADER_READ_BIT;
}
} else if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
sourceAccessMask = 0;
sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
destinationAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
destinationStage = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
}
VkImageMemoryBarrier barrier = {
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
@@ -58,7 +75,7 @@ void VkTransitionImageLayout(gnDevice device, VkImage image, VkFormat format, Vk
.newLayout = newLayout,
.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.subresourceRange.aspectMask = vkGryphnGetAspectFlags(format),
.subresourceRange.baseMipLevel = 0,
.subresourceRange.levelCount = 1,
.subresourceRange.baseArrayLayer = 0,
@@ -109,6 +126,8 @@ void VkCopyBufferToImage(VkGryphnBuffer buffer, VkGryphnImage image, uint32_t wi
gnEndVulkanTransferOperation(device, transferBuffer);
}
#include "stdio.h"
gnReturnCode gnCreateTextureFn(gnTexture texture, gnDevice device, const gnTextureInfo info) {
texture->texture = malloc(sizeof(struct gnPlatformTexture_t));
@@ -128,7 +147,6 @@ gnReturnCode gnCreateTextureFn(gnTexture texture, gnDevice device, const gnTextu
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
.tiling = VK_IMAGE_TILING_OPTIMAL,
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT,
.samples = VK_SAMPLE_COUNT_1_BIT,
.extent = {
.width = info.width,
@@ -141,6 +159,11 @@ gnReturnCode gnCreateTextureFn(gnTexture texture, gnDevice device, const gnTextu
.format = vkGryphnFormatToVulkanFormat(info.format)
};
if (vkGryphnIsDepthStencil(info.format))
imageInfo.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
else
imageInfo.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
if (vkCreateImage(device->outputDevice->device, &imageInfo, NULL, &texture->texture->image.image) != VK_SUCCESS)
return GN_FAILED_TO_CREATE_IMAGE;
@@ -171,7 +194,7 @@ gnReturnCode gnCreateTextureFn(gnTexture texture, gnDevice device, const gnTextu
.viewType = vkGryphnTextureTypeView(info.type),
.format = vkGryphnFormatToVulkanFormat(info.format),
.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.subresourceRange.aspectMask = vkGryphnGetAspectFlags(info.format),
.subresourceRange.baseMipLevel = 0,
.subresourceRange.levelCount = 1,
.subresourceRange.baseArrayLayer = 0,
@@ -210,6 +233,9 @@ gnReturnCode gnCreateTextureFn(gnTexture texture, gnDevice device, const gnTextu
if (vkCreateSampler(device->outputDevice->device, &samplerInfo, NULL, &texture->texture->sampler) != VK_SUCCESS)
return GN_FAILED_TO_CREATE_SAMPLER;
if (vkGryphnIsDepthStencil(info.format))
VkTransitionImageLayout(texture->device, texture->texture->image.image, texture->info.format, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
return GN_SUCCESS;
}
@@ -226,9 +252,9 @@ void gnTextureDataFn(gnTextureHandle texture, void* pixelData) {
}
//gnDevice device, VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout
VkTransitionImageLayout(texture->device, texture->texture->image.image, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
VkTransitionImageLayout(texture->device, texture->texture->image.image, texture->info.format, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
VkCopyBufferToImage(texture->texture->buffer, texture->texture->image, texture->texture->width, texture->texture->height, texture->device);
VkTransitionImageLayout(texture->device, texture->texture->image.image, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
VkTransitionImageLayout(texture->device, texture->texture->image.image, texture->info.format, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
texture->texture->beenWrittenToo = gnTrue;
}

View File

@@ -110,9 +110,11 @@ gnSurfaceDetails gnGetSurfaceDetailsFn(
VkFormat vkGryphnFormatToVulkanFormat(gnImageFormat format) {
switch (format) {
case GN_FORMAT_NONE: return VK_FORMAT_UNDEFINED;
case GN_FORMAT_BGRA8_SRGB: return VK_FORMAT_B8G8R8A8_SRGB;
case GN_FORMAT_RGBA8_SRGB: return VK_FORMAT_R8G8B8A8_SRGB;
default: return VK_FORMAT_UNDEFINED;
case GN_FORMAT_D32S8_UINT: return VK_FORMAT_D32_SFLOAT_S8_UINT;
case GN_FORMAT_D24S8_UINT: return VK_FORMAT_D24_UNORM_S8_UINT;
}
}
VkColorSpaceKHR vkGryphnColorSpaceToVulkanColorSpace(gnColorSpace colorSpace) {

View File

@@ -1,7 +1,7 @@
#include "gryphn_render_pass_descriptor.h"
#include "core/gryphn_platform_functions.h"
gnReturnCode gnCreateRenderPassDescriptor(gnRenderPassDescriptorHandle* renderPass, gnOutputDeviceHandle device, struct gnRenderPassDescriptorInfo_t info) {
gnReturnCode gnCreateRenderPassDescriptor(gnRenderPassDescriptorHandle* renderPass, gnOutputDeviceHandle device, gnRenderPassDescriptorInfo info) {
*renderPass = malloc(sizeof(struct gnRenderPassDescriptor_t));
(*renderPass)->device = device;
(*renderPass)->info = info;

View File

@@ -4,12 +4,14 @@
#include "utils/gryphn_error_code.h"
#include "core/gryphn_handles.h"
typedef enum gnRenderPassStage_e {
GN_COLOR_ATTACHMENT_OUTPUT = 0x00000400
typedef enum gnRenderPassStage {
GN_COLOR_ATTACHMENT_OUTPUT = 1,
GN_EARLY_FRAGMENT_TEST = 2
} gnRenderPassStage; // I stole these from vulkan to make that conversion easier
typedef enum gnRenderPassAccess_e {
GN_COLOR_ATTACHMENT_WRITE = 0x00000100
typedef enum gnRenderPassAccess {
GN_COLOR_ATTACHMENT_WRITE = 1,
GN_DEPTH_STENCIL_WRITE = 2
} gnRenderPassAccess;
typedef enum gnLoadOperation_e {
@@ -39,7 +41,8 @@ typedef struct gnSubpassAttachmentInfo_t {
typedef struct gnSubpassInfo_t {
uint32_t colorAttachmentCount;
struct gnSubpassAttachmentInfo_t* colorAttachments;
gnSubpassAttachmentInfo* colorAttachments;
gnSubpassAttachmentInfo* depthAttachment;
} gnSubpassInfo;
#define GN_SUBPASS_EXTERNAL -1
@@ -47,31 +50,31 @@ typedef struct gnSubpassInfo_t {
typedef struct gnSubpassDependencyInfo_t {
int source, destination;
enum gnRenderPassStage_e soruceStageMask;
enum gnRenderPassAccess_e sourceAccessMask;
gnRenderPassStage soruceStageMask;
gnRenderPassAccess sourceAccessMask;
enum gnRenderPassStage_e destinationStageMask;
enum gnRenderPassAccess_e destinationAccessMask;
gnRenderPassStage destinationStageMask;
gnRenderPassAccess destinationAccessMask;
} gnSubpassDependencyInfo;
typedef struct gnRenderPassDescriptorInfo_t {
typedef struct gnRenderPassDescriptorInfo {
uint32_t attachmentCount;
struct gnRenderPassAttachmentInfo_t* attachmentInfos;
gnRenderPassAttachmentInfo* attachmentInfos;
uint32_t subpassCount;
struct gnSubpassInfo_t* subpassInfos;
gnSubpassInfo* subpassInfos;
uint32_t dependencyCount;
struct gnSubpassDependencyInfo_t* dependencies;
gnSubpassDependencyInfo* dependencies;
} gnRenderPassDescriptorInfo;
#ifdef GN_REVEAL_IMPL
struct gnRenderPassDescriptor_t {
struct gnPlatformRenderPassDescriptor_t* renderPassDescriptor;
struct gnRenderPassDescriptorInfo_t info;
struct gnOutputDevice_t* device;
gnRenderPassDescriptorInfo info;
gnDeviceHandle device;
};
#endif
gnReturnCode gnCreateRenderPassDescriptor(gnRenderPassDescriptorHandle* renderPass, gnOutputDeviceHandle device, struct gnRenderPassDescriptorInfo_t info);
gnReturnCode gnCreateRenderPassDescriptor(gnRenderPassDescriptorHandle* renderPass, gnOutputDeviceHandle device, gnRenderPassDescriptorInfo info);
void gnDestroyRenderPassDescriptor(gnRenderPassDescriptorHandle renderPass);

View File

@@ -1,17 +1,18 @@
#pragma once
#include "stdint.h"
#include "core/sync/fence/gryphn_fence.h"
#include "core/renderpass/gryphn_render_pass_descriptor.h"
#include "core/gryphn_handles.h"
typedef struct gnSubmitInfo_t {
uint32_t waitCount;
enum gnRenderPassStage_e* waitStages;
gnRenderPassStage* waitStages;
gnSemaphoreHandle* waitSemaphores;
uint32_t signalCount;
gnSemaphoreHandle* signalSemaphores;
uint32_t commandBufferCount;
gnCommandBufferHandle* commandBuffers;
uint32_t queueIndex;
struct gnFence_t* fence;
gnFenceHandle fence;
} gnSubmitInfo;
gnReturnCode gnSubmit(gnOutputDevice device, gnSubmitInfo info);

View File

@@ -4,6 +4,7 @@
gnReturnCode gnCreateTexture(gnTexture* texture, gnDevice device, const gnTextureInfo info) {
*texture = malloc(sizeof(struct gnTexture_t));
(*texture)->device = device;
(*texture)->info = info;
return device->deviceFunctions->_gnCreateTexture(*texture, device, info);
}

View File

@@ -29,6 +29,7 @@ typedef struct gnTextureInfo {
struct gnTexture_t {
struct gnPlatformTexture_t* texture;
gnDeviceHandle device;
gnTextureInfo info;
};
#endif