Skip to content

Commit

Permalink
gi: minor shader generation code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pablode committed May 26, 2024
1 parent d6ac382 commit c11c6a0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 116 deletions.
74 changes: 27 additions & 47 deletions src/gi/src/Gi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -888,14 +888,25 @@ namespace gtl
bool hasPipelineClosestHitShader = false;
bool hasPipelineAnyHitShader = false;

uint32_t diskLightCount = scene->diskLights.elementCount();
uint32_t distantLightCount = scene->distantLights.elementCount();
uint32_t sphereLightCount = scene->sphereLights.elementCount();
uint32_t rectLightCount = scene->rectLights.elementCount();
uint32_t diskLightCount = scene->diskLights.elementCount();
uint32_t totalLightCount = sphereLightCount + distantLightCount + rectLightCount + diskLightCount;
uint32_t sphereLightCount = scene->sphereLights.elementCount();
uint32_t totalLightCount = diskLightCount + distantLightCount + rectLightCount + sphereLightCount;

bool nextEventEstimation = (params.nextEventEstimation && totalLightCount > 0);

GiGlslShaderGen::CommonShaderParams commonParams = {
.aovId = (int) params.aovId,
.diskLightCount = diskLightCount,
.distantLightCount = distantLightCount,
.mediumStackSize = params.mediumStackSize,
.rectLightCount = rectLightCount,
.sphereLightCount = sphereLightCount,
.texCount2d = texCount2d,
.texCount3d = texCount3d
};

// Create per-material closest-hit shaders.
//
// This is done in multiple phases: first, GLSL is generated from MDL, and
Expand Down Expand Up @@ -1004,27 +1015,19 @@ namespace gtl
// Closest hit
{
GiGlslShaderGen::ClosestHitShaderParams hitParams = {
.aovId = (int) params.aovId,
.baseFileName = "rp_main.chit",
.commonParams = commonParams,
.directionalBias = material->directionalBias,
.enableSceneTransforms = material->requiresSceneTransforms,
.hasCutoutTransparency = material->hasCutoutTransparency,
.hasVolumeAbsorptionCoeff = material->hasVolumeAbsorptionCoeff,
.hasVolumeScatteringCoeff = material->hasVolumeScatteringCoeff,
.isEmissive = material->isEmissive,
.hasCutoutTransparency = material->hasCutoutTransparency,
.isThinWalled = material->isThinWalled,
.enableSceneTransforms = material->requiresSceneTransforms,
.nextEventEstimation = nextEventEstimation,
.shadingGlsl = compInfo.closestHitInfo.genInfo.glslSource,
.distantLightCount = distantLightCount,
.sphereLightCount = sphereLightCount,
.rectLightCount = rectLightCount,
.diskLightCount = diskLightCount,
.textureIndexOffset2d = compInfo.closestHitInfo.texOffset2d,
.textureIndexOffset3d = compInfo.closestHitInfo.texOffset3d,
.texCount2d = texCount2d,
.texCount3d = texCount3d,
.mediumStackSize = params.mediumStackSize,
.maxVolumeWalkLength = params.maxVolumeWalkLength,
.directionalBias = material->directionalBias
.textureIndexOffset3d = compInfo.closestHitInfo.texOffset3d
};

if (!s_shaderGen->generateClosestHitSpirv(hitParams, compInfo.closestHitInfo.spv))
Expand All @@ -1038,20 +1041,12 @@ namespace gtl
if (compInfo.anyHitInfo)
{
GiGlslShaderGen::AnyHitShaderParams hitParams = {
.aovId = (int) params.aovId,
.enableSceneTransforms = material->requiresSceneTransforms,
.baseFileName = "rp_main.ahit",
.commonParams = commonParams,
.enableSceneTransforms = material->requiresSceneTransforms,
.opacityEvalGlsl = compInfo.anyHitInfo->genInfo.glslSource,
.distantLightCount = distantLightCount,
.sphereLightCount = sphereLightCount,
.rectLightCount = rectLightCount,
.diskLightCount = diskLightCount,
.textureIndexOffset2d = compInfo.anyHitInfo->texOffset2d,
.textureIndexOffset3d = compInfo.anyHitInfo->texOffset3d,
.texCount2d = texCount2d,
.texCount3d = texCount3d,
.mediumStackSize = params.mediumStackSize,
.maxVolumeWalkLength = params.maxVolumeWalkLength
.textureIndexOffset3d = compInfo.anyHitInfo->texOffset3d
};

hitParams.shadowTest = false;
Expand Down Expand Up @@ -1153,22 +1148,15 @@ namespace gtl
// Create ray generation shader.
{
GiGlslShaderGen::RaygenShaderParams rgenParams = {
.aovId = (int) params.aovId,
.commonParams = commonParams,
.depthOfField = params.depthOfField,
.filterImportanceSampling = params.filterImportanceSampling,
.materialCount = params.materialCount,
.maxVolumeWalkLength = params.maxVolumeWalkLength,
.nextEventEstimation = nextEventEstimation,
.progressiveAccumulation = params.progressiveAccumulation,
.reorderInvocations = s_deviceFeatures.rayTracingInvocationReorder,
.shaderClockExts = clockCyclesAov,
.distantLightCount = distantLightCount,
.sphereLightCount = sphereLightCount,
.rectLightCount = rectLightCount,
.diskLightCount = diskLightCount,
.texCount2d = texCount2d,
.texCount3d = texCount3d,
.mediumStackSize = params.mediumStackSize,
.maxVolumeWalkLength = params.maxVolumeWalkLength
.shaderClockExts = clockCyclesAov
};

std::vector<uint8_t> spv;
Expand All @@ -1190,16 +1178,8 @@ namespace gtl
// Create miss shaders.
{
GiGlslShaderGen::MissShaderParams missParams = {
.aovId = (int) params.aovId,
.domeLightCameraVisible = params.domeLightCameraVisible,
.distantLightCount = distantLightCount,
.sphereLightCount = sphereLightCount,
.rectLightCount = rectLightCount,
.diskLightCount = diskLightCount,
.texCount2d = texCount2d,
.texCount3d = texCount3d,
.mediumStackSize = params.mediumStackSize,
.maxVolumeWalkLength = params.maxVolumeWalkLength
.commonParams = commonParams,
.domeLightCameraVisible = params.domeLightCameraVisible
};

// regular miss shader
Expand Down
45 changes: 16 additions & 29 deletions src/gi/src/GlslShaderGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,24 @@ namespace gtl
GiGlslShaderCompiler::deinit();
}

void _sgGenerateCommonDefines(GiGlslStitcher& stitcher, uint32_t texCount2d, uint32_t texCount3d, uint32_t sphereLightCount,
uint32_t distantLightCount, uint32_t rectLightCount, uint32_t diskLightCount, uint32_t mediumStackSize,
uint32_t maxVolumeWalkLength)
void _sgGenerateCommonDefines(GiGlslStitcher& stitcher, const GiGlslShaderGen::CommonShaderParams& params)
{
#if defined(NDEBUG) || defined(__APPLE__)
stitcher.appendDefine("NDEBUG");
#endif

uint32_t totalLightCount = sphereLightCount + distantLightCount + rectLightCount + diskLightCount;
uint32_t totalLightCount = params.diskLightCount + params.distantLightCount +
params.rectLightCount + params.sphereLightCount;

stitcher.appendDefine("TEXTURE_COUNT_2D", (int32_t) texCount2d);
stitcher.appendDefine("TEXTURE_COUNT_3D", (int32_t) texCount3d);
stitcher.appendDefine("SPHERE_LIGHT_COUNT", (int32_t) sphereLightCount);
stitcher.appendDefine("DISTANT_LIGHT_COUNT", (int32_t) distantLightCount);
stitcher.appendDefine("RECT_LIGHT_COUNT", (int32_t) rectLightCount);
stitcher.appendDefine("DISK_LIGHT_COUNT", (int32_t) diskLightCount);
stitcher.appendDefine("AOV_ID", params.aovId);
stitcher.appendDefine("TEXTURE_COUNT_2D", (int32_t) params.texCount2d);
stitcher.appendDefine("TEXTURE_COUNT_3D", (int32_t) params.texCount3d);
stitcher.appendDefine("SPHERE_LIGHT_COUNT", (int32_t) params.sphereLightCount);
stitcher.appendDefine("DISTANT_LIGHT_COUNT", (int32_t) params.distantLightCount);
stitcher.appendDefine("RECT_LIGHT_COUNT", (int32_t) params.rectLightCount);
stitcher.appendDefine("DISK_LIGHT_COUNT", (int32_t) params.diskLightCount);
stitcher.appendDefine("TOTAL_LIGHT_COUNT", (int32_t) totalLightCount);
stitcher.appendDefine("MEDIUM_STACK_SIZE", (int32_t) mediumStackSize);
stitcher.appendDefine("MAX_VOLUME_WALK_LENGTH", (int32_t) maxVolumeWalkLength);
stitcher.appendDefine("MEDIUM_STACK_SIZE", (int32_t) params.mediumStackSize);
}

bool GiGlslShaderGen::generateRgenSpirv(std::string_view fileName, const RaygenShaderParams& params, std::vector<uint8_t>& spv)
Expand Down Expand Up @@ -108,9 +107,7 @@ namespace gtl
stitcher.appendDefine("REORDER_HINT_BIT_COUNT", reorderHintBitCount);
}

_sgGenerateCommonDefines(stitcher, params.texCount2d, params.texCount3d, params.sphereLightCount,
params.distantLightCount, params.rectLightCount, params.diskLightCount,
params.mediumStackSize, params.maxVolumeWalkLength);
_sgGenerateCommonDefines(stitcher, params.commonParams);

if (params.depthOfField)
{
Expand All @@ -129,7 +126,7 @@ namespace gtl
stitcher.appendDefine("PROGRESSIVE_ACCUMULATION");
}

stitcher.appendDefine("AOV_ID", params.aovId);
stitcher.appendDefine("MAX_VOLUME_WALK_LENGTH", (int32_t) params.maxVolumeWalkLength);

fs::path filePath = m_shaderPath / fileName;
if (!stitcher.appendSourceFile(filePath))
Expand All @@ -146,17 +143,13 @@ namespace gtl
GiGlslStitcher stitcher;
stitcher.appendVersion();

_sgGenerateCommonDefines(stitcher, params.texCount2d, params.texCount3d, params.sphereLightCount,
params.distantLightCount, params.rectLightCount, params.diskLightCount,
params.mediumStackSize, params.maxVolumeWalkLength);
_sgGenerateCommonDefines(stitcher, params.commonParams);

if (params.domeLightCameraVisible)
{
stitcher.appendDefine("DOME_LIGHT_CAMERA_VISIBLE");
}

stitcher.appendDefine("AOV_ID", params.aovId);

fs::path filePath = m_shaderPath / fileName;
if (!stitcher.appendSourceFile(filePath))
{
Expand Down Expand Up @@ -234,11 +227,8 @@ namespace gtl
GiGlslStitcher stitcher;
stitcher.appendVersion();

_sgGenerateCommonDefines(stitcher, params.texCount2d, params.texCount3d, params.sphereLightCount,
params.distantLightCount, params.rectLightCount, params.diskLightCount,
params.mediumStackSize, params.maxVolumeWalkLength);
_sgGenerateCommonDefines(stitcher, params.commonParams);

stitcher.appendDefine("AOV_ID", params.aovId);
stitcher.appendDefine("TEXTURE_INDEX_OFFSET_2D", (int32_t) params.textureIndexOffset2d);
stitcher.appendDefine("TEXTURE_INDEX_OFFSET_3D", (int32_t) params.textureIndexOffset3d);
stitcher.appendDefine("MEDIUM_DIRECTIONAL_BIAS", params.directionalBias);
Expand Down Expand Up @@ -289,11 +279,8 @@ namespace gtl
GiGlslStitcher stitcher;
stitcher.appendVersion();

_sgGenerateCommonDefines(stitcher, params.texCount2d, params.texCount3d, params.sphereLightCount,
params.distantLightCount, params.rectLightCount, params.diskLightCount,
params.mediumStackSize, params.maxVolumeWalkLength);
_sgGenerateCommonDefines(stitcher, params.commonParams);

stitcher.appendDefine("AOV_ID", params.aovId);
stitcher.appendDefine("TEXTURE_INDEX_OFFSET_2D", (int32_t) params.textureIndexOffset2d);
stitcher.appendDefine("TEXTURE_INDEX_OFFSET_3D", (int32_t) params.textureIndexOffset3d);
if (params.shadowTest)
Expand Down
61 changes: 21 additions & 40 deletions src/gi/src/GlslShaderGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,82 +53,63 @@ namespace gtl
bool generateMaterialOpacityGenInfo(const McMaterial& material, MaterialGenInfo& genInfo);

public:
struct RaygenShaderParams
struct CommonShaderParams
{
int32_t aovId;
uint32_t diskLightCount;
uint32_t distantLightCount;
uint32_t mediumStackSize;
uint32_t rectLightCount;
uint32_t sphereLightCount;
uint32_t texCount2d;
uint32_t texCount3d;
};

struct RaygenShaderParams
{
bool depthOfField;
CommonShaderParams commonParams;
bool filterImportanceSampling;
uint32_t materialCount;
uint32_t maxVolumeWalkLength;
bool nextEventEstimation;
bool progressiveAccumulation;
bool reorderInvocations;
bool shaderClockExts;
uint32_t distantLightCount;
uint32_t sphereLightCount;
uint32_t rectLightCount;
uint32_t diskLightCount;
uint32_t texCount2d;
uint32_t texCount3d;
uint32_t mediumStackSize;
uint32_t maxVolumeWalkLength;
};

struct MissShaderParams
{
int32_t aovId;
CommonShaderParams commonParams;
bool domeLightCameraVisible;
uint32_t distantLightCount;
uint32_t sphereLightCount;
uint32_t rectLightCount;
uint32_t diskLightCount;
uint32_t texCount2d;
uint32_t texCount3d;
uint32_t mediumStackSize;
uint32_t maxVolumeWalkLength;
};

struct ClosestHitShaderParams
{
int32_t aovId;
std::string_view baseFileName;
CommonShaderParams commonParams;
float directionalBias;
bool enableSceneTransforms;
bool hasCutoutTransparency;
bool hasVolumeAbsorptionCoeff;
bool hasVolumeScatteringCoeff;
bool isEmissive;
bool hasCutoutTransparency;
bool isThinWalled;
bool enableSceneTransforms;
bool nextEventEstimation;
std::string_view shadingGlsl;
uint32_t distantLightCount;
uint32_t sphereLightCount;
uint32_t rectLightCount;
uint32_t diskLightCount;
uint32_t textureIndexOffset2d;
uint32_t textureIndexOffset3d;
uint32_t texCount2d;
uint32_t texCount3d;
uint32_t mediumStackSize;
uint32_t maxVolumeWalkLength;
float directionalBias;
};

struct AnyHitShaderParams
{
int32_t aovId;
bool enableSceneTransforms;
std::string_view baseFileName;
CommonShaderParams commonParams;
bool enableSceneTransforms;
std::string_view opacityEvalGlsl;
bool shadowTest;
uint32_t distantLightCount;
uint32_t sphereLightCount;
uint32_t rectLightCount;
uint32_t diskLightCount;
uint32_t textureIndexOffset2d;
uint32_t textureIndexOffset3d;
uint32_t texCount2d;
uint32_t texCount3d;
uint32_t mediumStackSize;
uint32_t maxVolumeWalkLength;
};

bool generateRgenSpirv(std::string_view fileName, const RaygenShaderParams& params, std::vector<uint8_t>& spv);
Expand Down

0 comments on commit c11c6a0

Please sign in to comment.