Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffstadt committed Jan 22, 2025
1 parent ad7b705 commit 61541a3
Show file tree
Hide file tree
Showing 10 changed files with 577 additions and 599 deletions.
7 changes: 4 additions & 3 deletions extensions/pl_ecs_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,10 @@ enum _plHumanoidBone

enum _plEnvironmentProbeFlags
{
PL_ENVIRONMENT_PROBE_FLAGS_NONE = 0,
PL_ENVIRONMENT_PROBE_FLAGS_DIRTY = 1 << 0,
PL_ENVIRONMENT_PROBE_FLAGS_REALTIME = 1 << 1
PL_ENVIRONMENT_PROBE_FLAGS_NONE = 0,
PL_ENVIRONMENT_PROBE_FLAGS_DIRTY = 1 << 0,
PL_ENVIRONMENT_PROBE_FLAGS_REALTIME = 1 << 1,
PL_ENVIRONMENT_PROBE_FLAGS_INCLUDE_SKY = 1 << 2
};

//-----------------------------------------------------------------------------
Expand Down
398 changes: 152 additions & 246 deletions extensions/pl_renderer_ext.c

Large diffs are not rendered by default.

546 changes: 258 additions & 288 deletions extensions/pl_renderer_internal.c

Large diffs are not rendered by default.

69 changes: 49 additions & 20 deletions extensions/pl_renderer_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ Index of this file:
// [SECTION] internal structs
//-----------------------------------------------------------------------------

typedef int plDrawableFlags;

enum _plDrawableFlags
{
PL_DRAWABLE_FLAG_NONE = 0,
PL_DRAWABLE_FLAG_FORWARD = 1 << 0,
PL_DRAWABLE_FLAG_DEFERRED = 1 << 1,
PL_DRAWABLE_FLAG_PROBE = 1 << 2
};

typedef struct _plShaderVariant
{
plGraphicsState tGraphicsState;
Expand All @@ -128,20 +138,36 @@ typedef struct _plSkinData

typedef struct _plDrawable
{
plEntity tEntity;
uint32_t uDataOffset;
uint32_t uVertexOffset;
uint32_t uVertexCount;
uint32_t uIndexOffset;
uint32_t uIndexCount;
uint32_t uMaterialIndex;
plShaderHandle tShader;
plShaderHandle tEnvShader;
plShaderHandle tShadowShader;
uint32_t uSkinIndex;
bool bCulled;
plDrawableFlags tFlags;
plEntity tEntity;
uint32_t uDataOffset;
uint32_t uVertexOffset;
uint32_t uVertexCount;
uint32_t uIndexOffset;
uint32_t uIndexCount;
uint32_t uMaterialIndex;
plShaderHandle tShader;
plShaderHandle tEnvShader;
plShaderHandle tShadowShader;
uint32_t uSkinIndex;
uint32_t uTriangleCount;
plBufferHandle tIndexBuffer;
uint32_t uStaticVertexOffset;
uint32_t uDynamicVertexOffset;
bool bCulled;
} plDrawable;

typedef struct _plGPUProbeData
{
plVec3 tPosition;
float fRangeSqr;

uint32_t uLambertianEnvSampler;
uint32_t uGGXEnvSampler;
uint32_t uGGXLUT;
int _unused0[1];
} plGPUProbeData;

typedef struct _plGPUMaterial
{
// Metallic Roughness
Expand Down Expand Up @@ -323,8 +349,9 @@ typedef struct _plRefView
plBufferHandle atGlobalBuffers[PL_MAX_FRAMES_IN_FLIGHT];

// submitted drawables
plDrawable* sbtVisibleOpaqueDrawables;
plDrawable* sbtVisibleTransparentDrawables;
uint32_t* sbtVisibleDrawables;
uint32_t* sbtVisibleOpaqueDrawables;
uint32_t* sbtVisibleTransparentDrawables;

// drawing api
plDrawList3D* pt3DGizmoDrawList;
Expand Down Expand Up @@ -387,11 +414,11 @@ typedef struct _plRefScene

// drawables (per scene, will be culled by views)

plDrawable* sbtNewDeferredDrawables; // unprocessed
plDrawable* sbtNewForwardDrawables; // unprocessed
plDrawable* sbtNewDrawables; // unprocessed

plDrawable* sbtDrawables; // regular rendering

plDrawable* sbtDeferredDrawables; // regular rendering
plDrawable* sbtForwardDrawables; // regular rendering
uint32_t* sbuProbeDrawables;

uint32_t* sbuShadowDeferredDrawables; // shadow rendering (index into regular drawables)
uint32_t* sbuShadowForwardDrawables; // shadow rendering (index into regular drawables)
Expand All @@ -401,8 +428,7 @@ typedef struct _plRefScene
plShaderHandle* sbtOutlineDrawablesOldEnvShaders;

// entity to drawable hashmaps
plHashMap* ptDeferredHashmap;
plHashMap* ptForwardHashmap;
plHashMap* ptDrawableHashmap;

// bindless texture system
uint32_t uTextureIndexCount;
Expand All @@ -426,6 +452,8 @@ typedef struct _plRefScene

// environment probes
plEnvironmentProbeData* sbtProbeData;
plGPUProbeData* sbtGPUProbeData;
plBufferHandle atGPUProbeDataBuffers[PL_MAX_FRAMES_IN_FLIGHT];
plBufferHandle atFilterWorkingBuffers[7];
} plRefScene;

Expand Down Expand Up @@ -524,6 +552,7 @@ typedef struct _plRefRendererData
plHashMap* ptTextureHashmap;

// graphics options
bool bShowProbes;
bool bWireframe;
bool bReloadSwapchain;
bool bReloadMSAA;
Expand Down
1 change: 1 addition & 0 deletions extensions/pl_ui_widgets.c
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,7 @@ pl_radio_button(const char* pcText, int* piValue, int iButtonValue)

plRect tBoundingBox = pl_rect_expand_vec2(&tTextBounding, (plVec2){0.5f * (gptCtx->tStyle.tFramePadding.x + gptCtx->tStyle.tInnerSpacing.x + tWidgetSize.y), 0.0f});
tBoundingBox = pl_rect_move_start_x(&tBoundingBox, tStartPos.x + gptCtx->tStyle.tFramePadding.x);
tBoundingBox.tMax.y = tStartPos.y + tWidgetSize.y;
const plRect* ptClipRect = gptDraw->get_clip_rect(gptCtx->ptDrawlist);
tBoundingBox = pl_rect_clip_full(&tBoundingBox, ptClipRect);
bool bHovered = false;
Expand Down
29 changes: 18 additions & 11 deletions sandbox/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,27 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plEditorData* ptEditorData)
plTransformComponent* ptPLightTransform = gptEcs->add_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_TRANSFORM, tPointLight);
ptPLightTransform->tTranslation = (plVec3){0.0f, 1.497f, 2.0f};

plEntity tSpotLight = gptEcs->create_spot_light(ptMainComponentLibrary, "spot light", (plVec3){0.0f, 2.0f, 0.0f}, (plVec3){0.0, -1.0f, 0.0f}, &ptLight);
ptLight->uShadowResolution = 1024;
ptLight->fRange = 5.0f;
ptLight->fRadius = 0.025f;
ptLight->fIntensity = 20.0f;
ptLight->tFlags |= PL_LIGHT_FLAG_CAST_SHADOW | PL_LIGHT_FLAG_VISUALIZER;
plTransformComponent* ptSLightTransform = gptEcs->add_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_TRANSFORM, tSpotLight);
ptSLightTransform->tTranslation = (plVec3){0.0f, 4.0f, 0.0f};
// plEntity tSpotLight = gptEcs->create_spot_light(ptMainComponentLibrary, "spot light", (plVec3){0.0f, 2.0f, 0.0f}, (plVec3){0.0, -1.0f, 0.0f}, &ptLight);
// ptLight->uShadowResolution = 1024;
// ptLight->fRange = 5.0f;
// ptLight->fRadius = 0.025f;
// ptLight->fIntensity = 20.0f;
// ptLight->tFlags |= PL_LIGHT_FLAG_CAST_SHADOW | PL_LIGHT_FLAG_VISUALIZER;
// plTransformComponent* ptSLightTransform = gptEcs->add_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_TRANSFORM, tSpotLight);
// ptSLightTransform->tTranslation = (plVec3){0.0f, 4.0f, 0.0f};

plEnvironmentProbeComponent* ptProbe = NULL;
gptEcs->create_environment_probe(ptMainComponentLibrary, "Main Probe", (plVec3){0.0f, 2.0f, 2.0f}, &ptProbe);
ptProbe->fRange = 30.0f;
gptEcs->create_environment_probe(ptMainComponentLibrary, "Main Probe", (plVec3){0.0f, 2.0f, 0.0f}, &ptProbe);
ptProbe->fRange = 10.0f;
ptProbe->uResolution = 128;
ptProbe->tFlags = PL_ENVIRONMENT_PROBE_FLAGS_REALTIME;
ptProbe->tFlags = PL_ENVIRONMENT_PROBE_FLAGS_REALTIME | PL_ENVIRONMENT_PROBE_FLAGS_INCLUDE_SKY;

gptEcs->create_environment_probe(ptMainComponentLibrary, "Probe0", (plVec3){9.0f, 2.0f, 3.0f}, &ptProbe); ptProbe->fRange = 7.0f;
gptEcs->create_environment_probe(ptMainComponentLibrary, "Probe1", (plVec3){-1.274f, 2.0f, 3.0f}, &ptProbe); ptProbe->fRange = 7.0f;
gptEcs->create_environment_probe(ptMainComponentLibrary, "Probe2", (plVec3){-9.1f, 2.0f, 3.0f}, &ptProbe); ptProbe->fRange = 7.0f;
gptEcs->create_environment_probe(ptMainComponentLibrary, "Probe3", (plVec3){8.0f, 2.0f, -3.0f}, &ptProbe); ptProbe->fRange = 7.0f;
gptEcs->create_environment_probe(ptMainComponentLibrary, "Probe4", (plVec3){-1.0f, 2.0f, -3.0f}, &ptProbe); ptProbe->fRange = 7.0f;
gptEcs->create_environment_probe(ptMainComponentLibrary, "Probe5", (plVec3){-9.1f, 2.0f, -3.0f}, &ptProbe); ptProbe->fRange = 7.0f;

return ptEditorData;
}
Expand Down
10 changes: 10 additions & 0 deletions sandbox/pl_ecs_tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ pl_show_ecs_window(plEntity* ptSelectedEntity, plComponentLibrary* ptLibrary, bo
else
ptProbeComp->tFlags &= ~PL_ENVIRONMENT_PROBE_FLAGS_REALTIME;
}

bool bIncludeSky = ptProbeComp->tFlags & PL_ENVIRONMENT_PROBE_FLAGS_INCLUDE_SKY;
if(gptUi->checkbox("Include Sky", &bIncludeSky))
{
if(bIncludeSky)
ptProbeComp->tFlags |= PL_ENVIRONMENT_PROBE_FLAGS_INCLUDE_SKY;
else
ptProbeComp->tFlags &= ~PL_ENVIRONMENT_PROBE_FLAGS_INCLUDE_SKY;
}

if(gptUi->button("Update"))
{
ptProbeComp->tFlags |= PL_ENVIRONMENT_PROBE_FLAGS_DIRTY;
Expand Down
54 changes: 38 additions & 16 deletions shaders/lighting.frag
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ layout(constant_id = 0) const int iRenderingFlags = 0;
layout(constant_id = 1) const int iDirectionLightCount = 0;
layout(constant_id = 2) const int iPointLightCount = 0;
layout(constant_id = 3) const int iSpotLightCount = 0;
layout(constant_id = 4) const int iProbeCount = 0;

//-----------------------------------------------------------------------------
// [SECTION] bind group 0
Expand Down Expand Up @@ -92,7 +93,12 @@ layout(set = 2, binding = 6) readonly buffer plSShadowData
plLightShadowData atData[];
} tSShadowData;

layout(set = 2, binding = 7) uniform sampler tShadowSampler;
layout(set = 2, binding = 7) readonly buffer plProbeData
{
plEnvironmentProbeData atData[];
} tProbeData;

layout(set = 2, binding = 8) uniform sampler tShadowSampler;

//-----------------------------------------------------------------------------
// [SECTION] dynamic bind group
Expand Down Expand Up @@ -185,33 +191,33 @@ vec3 BRDF_specularGGX(vec3 f0, vec3 f90, float alphaRoughness, float specularWei
return specularWeight * F * Vis * D;
}

vec3 getDiffuseLight(vec3 n)
vec3 getDiffuseLight(vec3 n, int iProbeIndex)
{
// n.z = -n.z; // uncomment if not reverse z
// return texture(samplerCube(u_LambertianEnvSampler, tEnvSampler), n).rgb;
return texture(samplerCube(atCubeTextures[nonuniformEXT(tObjectInfo.uLambertianEnvSampler)], tEnvSampler), n).rgb;
return texture(samplerCube(atCubeTextures[nonuniformEXT(tProbeData.atData[iProbeIndex].uLambertianEnvSampler)], tEnvSampler), n).rgb;
}


vec4 getSpecularSample(vec3 reflection, float lod)
vec4 getSpecularSample(vec3 reflection, float lod, int iProbeIndex)
{
// reflection.z = -reflection.z; // uncomment if not reverse z
// reflection.x = -reflection.x; // uncomment if not reverse z
// return textureLod(samplerCube(u_GGXEnvSampler, tEnvSampler), reflection, lod);
return textureLod(samplerCube(atCubeTextures[nonuniformEXT(tObjectInfo.uGGXEnvSampler)], tEnvSampler), reflection, lod);
return textureLod(samplerCube(atCubeTextures[nonuniformEXT(tProbeData.atData[iProbeIndex].uGGXEnvSampler)], tEnvSampler), reflection, lod);
}

vec3 getIBLRadianceGGX(vec3 n, vec3 v, float roughness, vec3 F0, float specularWeight, int u_MipCount)
vec3 getIBLRadianceGGX(vec3 n, vec3 v, float roughness, vec3 F0, float specularWeight, int u_MipCount, int iProbeIndex)
{

float lod = roughness * float(u_MipCount - 1);
vec3 reflection = normalize(reflect(-v, n));
vec4 specularSample = getSpecularSample(reflection, lod);
vec4 specularSample = getSpecularSample(reflection, lod, iProbeIndex);

float NdotV = clampedDot(n, v);
vec2 brdfSamplePoint = clamp(vec2(NdotV, roughness), vec2(0.0, 0.0), vec2(1.0, 1.0));
// vec2 f_ab = texture(sampler2D(u_GGXLUT, tEnvSampler), brdfSamplePoint).rg;
vec2 f_ab = texture(sampler2D(at2DTextures[nonuniformEXT(tObjectInfo.uGGXLUT)], tEnvSampler), brdfSamplePoint).rg;
vec2 f_ab = texture(sampler2D(at2DTextures[nonuniformEXT(tProbeData.atData[iProbeIndex].uGGXLUT)], tEnvSampler), brdfSamplePoint).rg;

vec3 specularLight = specularSample.rgb;

Expand All @@ -223,14 +229,14 @@ vec3 getIBLRadianceGGX(vec3 n, vec3 v, float roughness, vec3 F0, float specularW
}

// specularWeight is introduced with KHR_materials_specular
vec3 getIBLRadianceLambertian(vec3 n, vec3 v, float roughness, vec3 diffuseColor, vec3 F0, float specularWeight)
vec3 getIBLRadianceLambertian(vec3 n, vec3 v, float roughness, vec3 diffuseColor, vec3 F0, float specularWeight, int iProbeIndex)
{
vec3 irradiance = getDiffuseLight(n);
vec3 irradiance = getDiffuseLight(n, iProbeIndex);

float NdotV = clampedDot(n, v);
vec2 brdfSamplePoint = clamp(vec2(NdotV, roughness), vec2(0.0, 0.0), vec2(1.0, 1.0));
// vec2 f_ab = texture(sampler2D(u_GGXLUT, tEnvSampler), brdfSamplePoint).rg;
vec2 f_ab = texture(sampler2D(at2DTextures[nonuniformEXT(tObjectInfo.uGGXLUT)], tEnvSampler), brdfSamplePoint).rg;
vec2 f_ab = texture(sampler2D(at2DTextures[nonuniformEXT(tProbeData.atData[iProbeIndex].uGGXLUT)], tEnvSampler), brdfSamplePoint).rg;

// see https://bruop.github.io/ibl/#single_scattering_results at Single Scattering Results
// Roughness dependent fresnel, from Fdez-Aguera
Expand Down Expand Up @@ -433,14 +439,30 @@ void main()
const float fPerceptualRoughness = AORoughnessMetalnessData.b;
float specularWeight = 1.0;
vec3 v = normalize(tGlobalInfo.data[tObjectInfo.uGlobalIndex].tCameraPos.xyz - tWorldPosition.xyz);
// int iMips = int(AORoughnessMetalnessData.a);
int iMips = textureQueryLevels(samplerCube(atCubeTextures[nonuniformEXT(tObjectInfo.uGGXEnvSampler)], tEnvSampler));

// Calculate lighting contribution from image based lighting source (IBL)
if(bool(iRenderingFlags & PL_RENDERING_FLAG_USE_IBL))
if(bool(iRenderingFlags & PL_RENDERING_FLAG_USE_IBL) && iProbeCount > 0)
{
f_specular += getIBLRadianceGGX(n, v, fPerceptualRoughness, f0, specularWeight, iMips);
f_diffuse += getIBLRadianceLambertian(n, v, fPerceptualRoughness, c_diff, f0, specularWeight);
int iProbeIndex = 0;
float fCurrentDistance = 10000.0;
for(int i = iProbeCount - 1; i > -1; i--)
{
vec3 tDist = tProbeData.atData[i].tPosition - tWorldPosition.xyz;
tDist = tDist * tDist;
float fDistSqr = tDist.x + tDist.y + tDist.z;
if(fDistSqr <= tProbeData.atData[i].fRangeSqr && fDistSqr < fCurrentDistance)
{
iProbeIndex = i;
fCurrentDistance = fDistSqr;
}
}

if(iProbeIndex > -1)
{
int iMips = textureQueryLevels(samplerCube(atCubeTextures[nonuniformEXT(tProbeData.atData[iProbeIndex].uGGXEnvSampler)], tEnvSampler));
f_specular += getIBLRadianceGGX(n, v, fPerceptualRoughness, f0, specularWeight, iMips, iProbeIndex);
f_diffuse += getIBLRadianceLambertian(n, v, fPerceptualRoughness, c_diff, f0, specularWeight, iProbeIndex);
}
}

// punctual stuff
Expand Down
9 changes: 9 additions & 0 deletions shaders/lights.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ struct plLightShadowData
float fYOffset;
};

struct plEnvironmentProbeData
{
vec3 tPosition;
float fRangeSqr;
uint uLambertianEnvSampler;
uint uGGXEnvSampler;
uint uGGXLUT;
};

float
getRangeAttenuation(float range2, float dist2)
{
Expand Down
Loading

0 comments on commit 61541a3

Please sign in to comment.