rtx: move upscaling back from denoiser

This commit is contained in:
Ivan 'provod' Avdeev 2021-11-07 10:07:01 -08:00 committed by Ivan Avdeev
parent c89dfee7b8
commit f570e59961
5 changed files with 98 additions and 93 deletions

View File

@ -5,6 +5,24 @@ layout(local_size_x = 16, local_size_y = 8, local_size_z = 1) in;
layout(set = 0, binding = 0, rgba8) uniform image2D source;
layout(set = 0, binding = 1, rgba8) uniform image2D dest;
// Blatantly copypasted from https://www.shadertoy.com/view/XsGfWV
vec3 aces_tonemap(vec3 color){
mat3 m1 = mat3(
0.59719, 0.07600, 0.02840,
0.35458, 0.90834, 0.13383,
0.04823, 0.01566, 0.83777
);
mat3 m2 = mat3(
1.60475, -0.10208, -0.00327,
-0.53108, 1.10813, -0.07276,
-0.07367, -0.00605, 1.07602
);
vec3 v = m1 * color;
vec3 a = v * (v + 0.0245786) - 0.000090537;
vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;
return pow(clamp(m2 * (a / b), 0.0, 1.0), vec3(1.0 / 2.2));
}
void main() {
ivec2 res = ivec2(imageSize(source));
ivec2 pix = ivec2(gl_GlobalInvocationID);
@ -12,7 +30,7 @@ void main() {
vec3 colour = vec3(0.);
if (all(lessThan(pix, res)))
colour = imageLoad(source, pix).rgb;
colour = aces_tonemap(imageLoad(source, pix).rgb);
imageStore(dest, pix, vec4(colour, 1.));
}

View File

@ -288,24 +288,6 @@ vec3 traceAdditive(vec3 origin, vec3 direction, float ray_distance) {
return payload_additive.color * color_factor;
}
// Blatantly copypasted from https://www.shadertoy.com/view/XsGfWV
vec3 aces_tonemap(vec3 color){
mat3 m1 = mat3(
0.59719, 0.07600, 0.02840,
0.35458, 0.90834, 0.13383,
0.04823, 0.01566, 0.83777
);
mat3 m2 = mat3(
1.60475, -0.10208, -0.00327,
-0.53108, 1.10813, -0.07276,
-0.07367, -0.00605, 1.07602
);
vec3 v = m1 * color;
vec3 a = v * (v + 0.0245786) - 0.000090537;
vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;
return pow(clamp(m2 * (a / b), 0.0, 1.0), vec3(1.0 / 2.2));
}
void main() {
rand01_state = push_constants.random_seed + gl_LaunchIDEXT.x * 1833 + gl_LaunchIDEXT.y * 31337;
vec2 uv = (gl_LaunchIDEXT.xy + .5) / gl_LaunchSizeEXT.xy * 2. - 1.;
@ -439,7 +421,6 @@ void main() {
} // for all bounces
C /= color_factor;
C = aces_tonemap(C);
imageStore(image, ivec2(gl_LaunchIDEXT.xy), vec4(C, 1.));
}

View File

@ -5,49 +5,6 @@
#include "eiface.h" // ARRAYSIZE
static void blitImage( VkCommandBuffer cmdbuf, VkImage src, VkImage dst, int src_width, int src_height, int dst_width, int dst_height )
{
// Blit raytraced image to frame buffer
{
VkImageBlit region = {0};
region.srcOffsets[1].x = src_width;
region.srcOffsets[1].y = src_height;
region.srcOffsets[1].z = 1;
region.dstOffsets[1].x = dst_width;
region.dstOffsets[1].y = dst_height;
region.dstOffsets[1].z = 1;
region.srcSubresource.aspectMask = region.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
region.srcSubresource.layerCount = region.dstSubresource.layerCount = 1;
vkCmdBlitImage(cmdbuf, src, VK_IMAGE_LAYOUT_GENERAL,
dst, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region,
VK_FILTER_NEAREST);
}
{
VkImageMemoryBarrier image_barriers[] = {
{
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.image = dst,
.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
.subresourceRange =
(VkImageSubresourceRange){
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
},
}};
vkCmdPipelineBarrier(cmdbuf,
VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
0, 0, NULL, 0, NULL, ARRAYSIZE(image_barriers), image_barriers);
}
}
enum {
DenoiserBinding_SourceImage = 0,
DenoiserBinding_DestImage = 1,
@ -129,13 +86,13 @@ void XVK_DenoiserDenoise( const xvk_denoiser_args_t* args ) {
g_denoiser.desc_values[DenoiserBinding_SourceImage].image = (VkDescriptorImageInfo){
.sampler = VK_NULL_HANDLE,
.imageView = args->in.image_view,
.imageView = args->view_src,
.imageLayout = VK_IMAGE_LAYOUT_GENERAL,
};
g_denoiser.desc_values[DenoiserBinding_DestImage].image = (VkDescriptorImageInfo){
.sampler = VK_NULL_HANDLE,
.imageView = args->out.image_view,
.imageView = args->view_dst,
.imageLayout = VK_IMAGE_LAYOUT_GENERAL,
};
@ -143,5 +100,5 @@ void XVK_DenoiserDenoise( const xvk_denoiser_args_t* args ) {
vkCmdBindPipeline(args->cmdbuf, VK_PIPELINE_BIND_POINT_COMPUTE, g_denoiser.pipeline);
vkCmdBindDescriptorSets(args->cmdbuf, VK_PIPELINE_BIND_POINT_COMPUTE, g_denoiser.descriptors.pipeline_layout, 0, 1, g_denoiser.descriptors.desc_sets + 0, 0, NULL);
vkCmdDispatch(args->cmdbuf, (args->out.width + WG_W - 1) / WG_W, (args->out.height + WG_H - 1) / WG_H, 1);
vkCmdDispatch(args->cmdbuf, (args->width + WG_W - 1) / WG_W, (args->height + WG_H - 1) / WG_H, 1);
}

View File

@ -7,16 +7,8 @@ void XVK_DenoiserDestroy( void );
typedef struct {
VkCommandBuffer cmdbuf;
struct {
VkImageView image_view;
uint32_t width, height;
} in;
struct {
VkImageView image_view;
uint32_t width, height;
} out;
uint32_t width, height;
VkImageView view_src, view_dst;
} xvk_denoiser_args_t;
void XVK_DenoiserDenoise( const xvk_denoiser_args_t* args );

View File

@ -770,10 +770,55 @@ static void clearVkImage( VkCommandBuffer cmdbuf, VkImage image ) {
vkCmdClearColorImage(cmdbuf, image, VK_IMAGE_LAYOUT_GENERAL, &clear_value, 1, &image_barriers->subresourceRange);
}
static void blitImage( VkCommandBuffer cmdbuf, VkImage src, VkImage dst, int src_width, int src_height, int dst_width, int dst_height )
{
// Blit raytraced image to frame buffer
{
VkImageBlit region = {0};
region.srcOffsets[1].x = src_width;
region.srcOffsets[1].y = src_height;
region.srcOffsets[1].z = 1;
region.dstOffsets[1].x = dst_width;
region.dstOffsets[1].y = dst_height;
region.dstOffsets[1].z = 1;
region.srcSubresource.aspectMask = region.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
region.srcSubresource.layerCount = region.dstSubresource.layerCount = 1;
vkCmdBlitImage(cmdbuf,
src, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
dst, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1, &region,
VK_FILTER_NEAREST);
}
{
VkImageMemoryBarrier image_barriers[] = {
{
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.image = dst,
.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
.subresourceRange =
(VkImageSubresourceRange){
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
},
}};
vkCmdPipelineBarrier(cmdbuf,
VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
0, 0, NULL, 0, NULL, ARRAYSIZE(image_barriers), image_barriers);
}
}
void VK_RayFrameEnd(const vk_ray_frame_render_args_t* args)
{
const VkCommandBuffer cmdbuf = args->cmdbuf;
const vk_image_t* frame_src = g_rtx.frames + ((g_rtx.frame_number + 1) % 2);
const vk_image_t* frame_denoiser_dst = g_rtx.frames + ((g_rtx.frame_number + 1) % 2);
const vk_image_t* frame_dst = g_rtx.frames + (g_rtx.frame_number % 2);
ASSERT(vk_core.rtx);
@ -851,7 +896,7 @@ void VK_RayFrameEnd(const vk_ray_frame_render_args_t* args)
},
{
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.image = args->dst.image,
.image = frame_denoiser_dst->image,
.srcAccessMask = 0,
.dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
@ -875,16 +920,10 @@ void VK_RayFrameEnd(const vk_ray_frame_render_args_t* args)
{
const xvk_denoiser_args_t denoiser_args = {
.cmdbuf = cmdbuf,
.in = {
.image_view = frame_dst->view,
.width = FRAME_WIDTH,
.height = FRAME_HEIGHT,
},
.out = {
.image_view = args->dst.image_view,
.width = args->dst.width,
.height = args->dst.height,
},
.width = FRAME_WIDTH,
.height = FRAME_HEIGHT,
.view_src = frame_dst->view,
.view_dst = frame_denoiser_dst->view,
};
XVK_DenoiserDenoise( &denoiser_args );
@ -894,11 +933,26 @@ void VK_RayFrameEnd(const vk_ray_frame_render_args_t* args)
const VkImageMemoryBarrier image_barriers[] = {
{
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.image = args->dst.image,
.image = frame_denoiser_dst->image,
.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT,
.oldLayout = VK_IMAGE_LAYOUT_GENERAL,
.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
.subresourceRange =
(VkImageSubresourceRange){
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
},
}, {
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.image = args->dst.image,
.srcAccessMask = 0,
.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
.subresourceRange =
(VkImageSubresourceRange){
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
@ -910,10 +964,13 @@ void VK_RayFrameEnd(const vk_ray_frame_render_args_t* args)
}};
vkCmdPipelineBarrier(args->cmdbuf,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT,
0, 0, NULL, 0, NULL, ARRAYSIZE(image_barriers), image_barriers);
}
blitImage(args->cmdbuf, frame_denoiser_dst->image, args->dst.image,
FRAME_WIDTH, FRAME_HEIGHT,
args->dst.width, args->dst.height);
}
}
static void createLayouts( void ) {