Skip to content

Commit

Permalink
feat: add octahedron normal vector encoding & improve gbuffer efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffstadt committed Dec 5, 2024
1 parent d875e69 commit f9f8d2a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 36 deletions.
41 changes: 21 additions & 20 deletions extensions/pl_renderer_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ pl_refr_initialize(plWindow* ptWindow)
{ .tFormat = PL_FORMAT_D32_FLOAT_S8_UINT, .bDepth = true }, // depth buffer
{ .tFormat = PL_FORMAT_R32G32B32A32_FLOAT }, // final output
{ .tFormat = PL_FORMAT_R8G8B8A8_SRGB }, // albedo
{ .tFormat = PL_FORMAT_R16G16B16A16_FLOAT }, // normal
{ .tFormat = PL_FORMAT_R16G16_FLOAT }, // normal
{ .tFormat = PL_FORMAT_R32G32B32A32_FLOAT }, // position
{ .tFormat = PL_FORMAT_R32G32B32A32_FLOAT }, // AO, roughness, metallic, specular weight
{ .tFormat = PL_FORMAT_R16G16B16A16_FLOAT }, // AO, roughness, metallic, specular weight
},
.atSubpasses = {
{ // G-buffer fill
Expand Down Expand Up @@ -352,7 +352,7 @@ pl_refr_initialize(plWindow* ptWindow)
pl_refr_create_global_shaders();

const plComputeShaderDesc tComputeShaderDesc = {
.tShader = gptShader->compile_glsl("../shaders/jumpfloodalgo.comp", "main", NULL),
.tShader = gptShader->load_glsl("../shaders/jumpfloodalgo.comp", "main", NULL, NULL),
.atBindGroupLayouts = {
{
.atTextureBindings = {
Expand Down Expand Up @@ -556,7 +556,8 @@ pl_refr_create_view(uint32_t uSceneHandle, plVec2 tDimensions)

const plTextureDesc tNormalTextureDesc = {
.tDimensions = {ptView->tTargetSize.x, ptView->tTargetSize.y, 1},
.tFormat = PL_FORMAT_R16G16B16A16_FLOAT,
// .tFormat = PL_FORMAT_R16G16B16A16_FLOAT,
.tFormat = PL_FORMAT_R16G16_FLOAT,
.uLayers = 1,
.uMips = 1,
.tType = PL_TEXTURE_TYPE_2D,
Expand Down Expand Up @@ -606,7 +607,7 @@ pl_refr_create_view(uint32_t uSceneHandle, plVec2 tDimensions)

const plTextureDesc tEmmissiveTexDesc = {
.tDimensions = {ptView->tTargetSize.x, ptView->tTargetSize.y, 1},
.tFormat = PL_FORMAT_R32G32B32A32_FLOAT,
.tFormat = PL_FORMAT_R16G16B16A16_FLOAT,
.uLayers = 1,
.uMips = 1,
.tType = PL_TEXTURE_TYPE_2D,
Expand Down Expand Up @@ -952,7 +953,7 @@ pl_refr_resize_view(uint32_t uSceneHandle, uint32_t uViewHandle, plVec2 tDimensi

const plTextureDesc tNormalTextureDesc = {
.tDimensions = {ptView->tTargetSize.x, ptView->tTargetSize.y, 1},
.tFormat = PL_FORMAT_R16G16B16A16_FLOAT,
.tFormat = PL_FORMAT_R16G16_FLOAT,
.uLayers = 1,
.uMips = 1,
.tType = PL_TEXTURE_TYPE_2D,
Expand Down Expand Up @@ -989,7 +990,7 @@ pl_refr_resize_view(uint32_t uSceneHandle, uint32_t uViewHandle, plVec2 tDimensi

const plTextureDesc tEmmissiveTexDesc = {
.tDimensions = {ptView->tTargetSize.x, ptView->tTargetSize.y, 1},
.tFormat = PL_FORMAT_R32G32B32A32_FLOAT,
.tFormat = PL_FORMAT_R16G16B16A16_FLOAT,
.uLayers = 1,
.uMips = 1,
.tType = PL_TEXTURE_TYPE_2D,
Expand Down Expand Up @@ -1037,7 +1038,7 @@ pl_refr_resize_view(uint32_t uSceneHandle, uint32_t uViewHandle, plVec2 tDimensi
ptView->tFinalTextureHandle[i] = gptDrawBackend->create_bind_group_for_texture(ptView->tFinalTexture[i]);
ptView->tRawOutputTexture[i] = pl__refr_create_texture(&tRawOutputTextureDesc, "offscreen raw", i, PL_TEXTURE_USAGE_SAMPLED);
ptView->tAlbedoTexture[i] = pl__refr_create_texture(&tAlbedoTextureDesc, "albedo original", i, PL_TEXTURE_USAGE_COLOR_ATTACHMENT);
ptView->tNormalTexture[i] = pl__refr_create_texture(&tNormalTextureDesc, "normal original", i, PL_TEXTURE_USAGE_COLOR_ATTACHMENT);
ptView->tNormalTexture[i] = pl__refr_create_texture(&tNormalTextureDesc, "normal resize", i, PL_TEXTURE_USAGE_COLOR_ATTACHMENT);
ptView->tPositionTexture[i] = pl__refr_create_texture(&tAttachmentTextureDesc, "position original", i, PL_TEXTURE_USAGE_COLOR_ATTACHMENT);
ptView->tAOMetalRoughnessTexture[i] = pl__refr_create_texture(&tEmmissiveTexDesc, "metalroughness original", i, PL_TEXTURE_USAGE_COLOR_ATTACHMENT);
ptView->tDepthTexture[i] = pl__refr_create_texture(&tDepthTextureDesc, "offscreen depth original", i, PL_TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT);
Expand Down Expand Up @@ -1206,8 +1207,8 @@ pl_refr_load_skybox_from_panorama(uint32_t uSceneHandle, const char* pcPath, int
{
// create skybox shader
plShaderDesc tSkyboxShaderDesc = {
.tPixelShader = gptShader->compile_glsl("../shaders/skybox.frag", "main", NULL),
.tVertexShader = gptShader->compile_glsl("../shaders/skybox.vert", "main", NULL),
.tPixelShader = gptShader->load_glsl("../shaders/skybox.frag", "main", NULL, NULL),
.tVertexShader = gptShader->load_glsl("../shaders/skybox.vert", "main", NULL, NULL),
.tGraphicsState = {
.ulDepthWriteEnabled = 0,
.ulDepthMode = PL_COMPARE_MODE_LESS_OR_EQUAL,
Expand Down Expand Up @@ -1278,7 +1279,7 @@ pl_refr_load_skybox_from_panorama(uint32_t uSceneHandle, const char* pcPath, int
{
int aiSkyboxSpecializationData[] = {iResolution, iPanoramaWidth, iPanoramaHeight};
const plComputeShaderDesc tSkyboxComputeShaderDesc = {
.tShader = gptShader->compile_glsl("../shaders/panorama_to_cubemap.comp", "main", NULL),
.tShader = gptShader->load_glsl("../shaders/panorama_to_cubemap.comp", "main", NULL, NULL),
.pTempConstantData = aiSkyboxSpecializationData,
.atConstants = {
{ .uID = 0, .uOffset = 0, .tType = PL_DATA_TYPE_INT},
Expand Down Expand Up @@ -1503,7 +1504,7 @@ pl_refr_load_skybox_from_panorama(uint32_t uSceneHandle, const char* pcPath, int
pl_begin_cpu_sample(gptProfile, 0, "step 1");

plComputeShaderDesc tFilterComputeShaderDesc = {
.tShader = gptShader->compile_glsl("../shaders/filter_environment.comp", "main", NULL),
.tShader = gptShader->load_glsl("../shaders/filter_environment.comp", "main", NULL, NULL),
.atConstants = {
{ .uID = 0, .uOffset = 0, .tType = PL_DATA_TYPE_INT},
{ .uID = 1, .uOffset = 4, .tType = PL_DATA_TYPE_FLOAT},
Expand Down Expand Up @@ -2134,8 +2135,8 @@ pl_refr_reload_scene_shaders(uint32_t uSceneHandle)
const plLightComponent* sbtLights = ptScene->tComponentLibrary.tLightComponentManager.pComponents;
int aiLightingConstantData[] = {iSceneWideRenderingFlags, pl_sb_size(sbtLights)};
plShaderDesc tLightingShaderDesc = {
.tPixelShader = gptShader->compile_glsl("../shaders/lighting.frag", "main", NULL),
.tVertexShader = gptShader->compile_glsl("../shaders/lighting.vert", "main", NULL),
.tPixelShader = gptShader->load_glsl("../shaders/lighting.frag", "main", NULL, NULL),
.tVertexShader = gptShader->load_glsl("../shaders/lighting.vert", "main", NULL, NULL),
.tGraphicsState = {
.ulDepthWriteEnabled = 0,
.ulDepthMode = PL_COMPARE_MODE_ALWAYS,
Expand Down Expand Up @@ -2224,8 +2225,8 @@ pl_refr_reload_scene_shaders(uint32_t uSceneHandle)
}

const plShaderDesc tTonemapShaderDesc = {
.tPixelShader = gptShader->compile_glsl("../shaders/tonemap.frag", "main", NULL),
.tVertexShader = gptShader->compile_glsl("../shaders/full_quad.vert", "main", NULL),
.tPixelShader = gptShader->load_glsl("../shaders/tonemap.frag", "main", NULL, NULL),
.tVertexShader = gptShader->load_glsl("../shaders/full_quad.vert", "main", NULL, NULL),
.tGraphicsState = {
.ulDepthWriteEnabled = 0,
.ulDepthMode = PL_COMPARE_MODE_ALWAYS,
Expand Down Expand Up @@ -2737,8 +2738,8 @@ pl_refr_finalize_scene(uint32_t uSceneHandle)
{
int aiLightingConstantData[] = {iSceneWideRenderingFlags, pl_sb_size(sbtLights)};
plShaderDesc tLightingShaderDesc = {
.tPixelShader = gptShader->compile_glsl("../shaders/lighting.frag", "main", NULL),
.tVertexShader = gptShader->compile_glsl("../shaders/lighting.vert", "main", NULL),
.tPixelShader = gptShader->load_glsl("../shaders/lighting.frag", "main", NULL, NULL),
.tVertexShader = gptShader->load_glsl("../shaders/lighting.vert", "main", NULL, NULL),
.tGraphicsState = {
.ulDepthWriteEnabled = 0,
.ulDepthMode = PL_COMPARE_MODE_ALWAYS,
Expand Down Expand Up @@ -2827,8 +2828,8 @@ pl_refr_finalize_scene(uint32_t uSceneHandle)
}

const plShaderDesc tTonemapShaderDesc = {
.tPixelShader = gptShader->compile_glsl("../shaders/tonemap.frag", "main", NULL),
.tVertexShader = gptShader->compile_glsl("../shaders/full_quad.vert", "main", NULL),
.tPixelShader = gptShader->load_glsl("../shaders/tonemap.frag", "main", NULL, NULL),
.tVertexShader = gptShader->load_glsl("../shaders/full_quad.vert", "main", NULL, NULL),
.tGraphicsState = {
.ulDepthWriteEnabled = 0,
.ulDepthMode = PL_COMPARE_MODE_ALWAYS,
Expand Down
26 changes: 13 additions & 13 deletions extensions/pl_renderer_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1185,8 +1185,8 @@ pl_refr_create_global_shaders(void)
int aiConstantData[6] = {0, 0, 0, 0, 0, 1};

plShaderDesc tDeferredShaderDescription = {
.tPixelShader = gptShader->compile_glsl("../shaders/primitive.frag", "main", NULL),
.tVertexShader = gptShader->compile_glsl("../shaders/primitive.vert", "main", NULL),
.tPixelShader = gptShader->load_glsl("../shaders/primitive.frag", "main", NULL, NULL),
.tVertexShader = gptShader->load_glsl("../shaders/primitive.vert", "main", NULL, NULL),
.tGraphicsState = {
.ulDepthWriteEnabled = 1,
.ulDepthMode = PL_COMPARE_MODE_LESS,
Expand Down Expand Up @@ -1263,8 +1263,8 @@ pl_refr_create_global_shaders(void)
gptData->tDeferredShader = gptGfx->create_shader(gptData->ptDevice, &tDeferredShaderDescription);

plShaderDesc tForwardShaderDescription = {
.tPixelShader = gptShader->compile_glsl("../shaders/transparent.frag", "main", NULL),
.tVertexShader = gptShader->compile_glsl("../shaders/transparent.vert", "main", NULL),
.tPixelShader = gptShader->load_glsl("../shaders/transparent.frag", "main", NULL, NULL),
.tVertexShader = gptShader->load_glsl("../shaders/transparent.vert", "main", NULL, NULL),
.tGraphicsState = {
.ulDepthWriteEnabled = 0,
.ulDepthMode = PL_COMPARE_MODE_LESS_OR_EQUAL,
Expand Down Expand Up @@ -1350,8 +1350,8 @@ pl_refr_create_global_shaders(void)
gptData->tForwardShader = gptGfx->create_shader(gptData->ptDevice, &tForwardShaderDescription);

plShaderDesc tShadowShaderDescription = {
.tPixelShader = gptShader->compile_glsl("../shaders/shadow.frag", "main", NULL),
.tVertexShader = gptShader->compile_glsl("../shaders/shadow.vert", "main", NULL),
.tPixelShader = gptShader->load_glsl("../shaders/shadow.frag", "main", NULL, NULL),
.tVertexShader = gptShader->load_glsl("../shaders/shadow.vert", "main", NULL, NULL),
.tGraphicsState = {
.ulDepthWriteEnabled = 1,
.ulDepthMode = PL_COMPARE_MODE_LESS_OR_EQUAL,
Expand Down Expand Up @@ -1407,8 +1407,8 @@ pl_refr_create_global_shaders(void)
gptData->tShadowShader = gptGfx->create_shader(gptData->ptDevice, &tShadowShaderDescription);

const plShaderDesc tPickShaderDescription = {
.tPixelShader = gptShader->compile_glsl("../shaders/picking.frag", "main", NULL),
.tVertexShader = gptShader->compile_glsl("../shaders/picking.vert", "main", NULL),
.tPixelShader = gptShader->load_glsl("../shaders/picking.frag", "main", NULL, NULL),
.tVertexShader = gptShader->load_glsl("../shaders/picking.vert", "main", NULL, NULL),
.tGraphicsState = {
.ulDepthWriteEnabled = 1,
.ulDepthMode = PL_COMPARE_MODE_LESS_OR_EQUAL,
Expand Down Expand Up @@ -1443,8 +1443,8 @@ pl_refr_create_global_shaders(void)
gptData->tPickShader = gptGfx->create_shader(gptData->ptDevice, &tPickShaderDescription);

const plShaderDesc tUVShaderDesc = {
.tPixelShader = gptShader->compile_glsl("../shaders/uvmap.frag", "main", NULL),
.tVertexShader = gptShader->compile_glsl("../shaders/uvmap.vert", "main", NULL),
.tPixelShader = gptShader->load_glsl("../shaders/uvmap.frag", "main", NULL, NULL),
.tVertexShader = gptShader->load_glsl("../shaders/uvmap.vert", "main", NULL, NULL),
.tGraphicsState = {
.ulDepthWriteEnabled = 0,
.ulDepthMode = PL_COMPARE_MODE_ALWAYS,
Expand Down Expand Up @@ -1478,8 +1478,8 @@ pl_refr_create_global_shaders(void)

// create skybox shader
plShaderDesc tSkyboxShaderDesc = {
.tPixelShader = gptShader->compile_glsl("../shaders/skybox.frag", "main", NULL),
.tVertexShader = gptShader->compile_glsl("../shaders/skybox.vert", "main", NULL),
.tPixelShader = gptShader->load_glsl("../shaders/skybox.frag", "main", NULL, NULL),
.tVertexShader = gptShader->load_glsl("../shaders/skybox.vert", "main", NULL, NULL),
.tGraphicsState = {
.ulDepthWriteEnabled = 0,
.ulDepthMode = PL_COMPARE_MODE_LESS_OR_EQUAL,
Expand Down Expand Up @@ -1674,7 +1674,7 @@ pl__add_drawable_skin_data_to_global_buffer(plRefScene* ptScene, uint32_t uDrawa

int aiSpecializationData[] = {(int)ulVertexStreamMask, (int)uStride, (int)ptMesh->ulVertexStreamMask, (int)uDestStride};
const plComputeShaderDesc tComputeShaderDesc = {
.tShader = gptShader->compile_glsl("../shaders/skinning.comp", "main", NULL),
.tShader = gptShader->load_glsl("../shaders/skinning.comp", "main", NULL, NULL),
.pTempConstantData = aiSpecializationData,
.atConstants = {
{ .uID = 0, .uOffset = 0, .tType = PL_DATA_TYPE_INT},
Expand Down
15 changes: 14 additions & 1 deletion shaders/lighting.frag
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,25 @@ float filterPCF(vec4 sc, uint cascadeIndex)
return shadowFactor / count;
}

vec3 Decode( vec2 f )
{
f = f * 2.0 - 1.0;

// https://twitter.com/Stubbesaurus/status/937994790553227264
vec3 n = vec3( f.x, f.y, 1.0 - abs( f.x ) - abs( f.y ) );
float t = max( -n.z, 0.0 );
n.x += n.x >= 0.0 ? -t : t;
n.y += n.y >= 0.0 ? -t : t;
return normalize( n );
}


void main()
{
vec4 AORoughnessMetalnessData = subpassLoad(tAOMetalRoughnessTexture);
vec4 tBaseColor = subpassLoad(tAlbedoSampler);
vec4 tPosition = subpassLoad(tPositionSampler);
vec3 n = subpassLoad(tNormalTexture).xyz;
vec3 n = Decode(subpassLoad(tNormalTexture).xy);

const vec3 f90 = vec3(1.0);

Expand Down
18 changes: 16 additions & 2 deletions shaders/primitive.frag
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ layout(set = 3, binding = 0) uniform PL_DYNAMIC_DATA
//-----------------------------------------------------------------------------

layout(location = 0) out vec4 outAlbedo;
layout(location = 1) out vec4 outNormal;
layout(location = 1) out vec2 outNormal;
layout(location = 2) out vec4 outPosition;
layout(location = 3) out vec4 outAOMetalnessRoughness;

Expand Down Expand Up @@ -218,6 +218,20 @@ MaterialInfo getMetallicRoughnessInfo(MaterialInfo info, float u_MetallicFactor,
return info;
}

vec2 OctWrap( vec2 v ) {
vec2 w = 1.0 - abs( v.yx );
if (v.x < 0.0) w.x = -w.x;
if (v.y < 0.0) w.y = -w.y;
return w;
}

vec2 Encode( vec3 n ) {
n /= ( abs( n.x ) + abs( n.y ) + abs( n.z ) );
n.xy = n.z > 0.0 ? n.xy : OctWrap( n.xy );
n.xy = n.xy * 0.5 + 0.5;
return n.xy;
}

//-----------------------------------------------------------------------------
// [SECTION] entry
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -257,7 +271,7 @@ void main()

// fill g-buffer
outAlbedo = tBaseColor;
outNormal = vec4(tNormalInfo.n, 1.0);
outNormal = Encode(tNormalInfo.n);
outPosition = vec4(tShaderIn.tPosition, 1.0);
outAOMetalnessRoughness = vec4(ao, materialInfo.metallic, materialInfo.perceptualRoughness, material.u_MipCount);
}
Expand Down

0 comments on commit f9f8d2a

Please sign in to comment.