Skip to content

Commit ef9a937

Browse files
committed
Refactor code as per review comments
1 parent 029ecc1 commit ef9a937

File tree

1 file changed

+60
-78
lines changed

1 file changed

+60
-78
lines changed

source/slang/slang-ir-glsl-legalize.cpp

+60-78
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,6 @@ struct GLSLLegalizationContext
329329

330330
IRBuilder* builder;
331331
IRBuilder* getBuilder() { return builder; }
332-
333-
// For ray tracing shaders, we need to consolidate all parameters into a single structure
334-
Dictionary<IRFunc*, IRInst*> rayTracingConsolidatedVars;
335-
Dictionary<IRFunc*, List<IRParam*>> rayTracingProcessedParams;
336332
};
337333

338334
// This examines the passed type and determines the GLSL mesh shader indices
@@ -2306,7 +2302,7 @@ IRInst* materializeValue(IRBuilder* builder, ScalarizedVal const& val)
23062302
}
23072303
}
23082304

2309-
static void handleSingleParam(
2305+
void handleSingleParam(
23102306
GLSLLegalizationContext* context,
23112307
IRFunc* func,
23122308
IRParam* pp,
@@ -2360,7 +2356,6 @@ static void handleSingleParam(
23602356

23612357
static void consolidateParameters(
23622358
GLSLLegalizationContext* context,
2363-
IRFunc* func,
23642359
List<IRParam*>& params)
23652360
{
23662361
auto builder = context->getBuilder();
@@ -2381,7 +2376,8 @@ static void consolidateParameters(
23812376
valueType = inOutType->getValueType();
23822377

23832378
auto key = builder->createStructKey();
2384-
builder->addNameHintDecoration(key, UnownedStringSlice("field"));
2379+
if (auto nameDecor = _param->findDecoration<IRNameHintDecoration>())
2380+
builder->addNameHintDecoration(key, nameDecor->getName());
23852381
auto field = builder->createStructField(structType, key, valueType);
23862382
field->removeFromParent();
23872383
field->insertAtEnd(structType);
@@ -2396,9 +2392,6 @@ static void consolidateParameters(
23962392
// Add the ray payload decoration and assign location 0.
23972393
builder->addVulkanRayPayloadDecoration(consolidatedVar, 0);
23982394

2399-
// Store the consolidated variable for this function
2400-
context->rayTracingConsolidatedVars[func] = consolidatedVar;
2401-
24022395
// Replace each parameter with a field in the consolidated struct
24032396
for (Index i = 0; i < params.getCount(); ++i)
24042397
{
@@ -2441,64 +2434,54 @@ static void consolidateParameters(
24412434
}
24422435
}
24432436

2444-
static void handleMultipleParams(GLSLLegalizationContext* context, IRFunc* func, IRParam* pp)
2437+
// Consolidate ray tracing parameters for an entry point function
2438+
void consolidateRayTracingParameters(
2439+
GLSLLegalizationContext* context,
2440+
IRFunc* func)
24452441
{
2442+
auto builder = context->getBuilder();
24462443
auto firstBlock = func->getFirstBlock();
2444+
if (!firstBlock)
2445+
return;
24472446

2448-
// Now we run the consolidation step, but if we've already
2449-
// processed this parameter, skip it.
2450-
List<IRParam*>* processedParams = nullptr;
2451-
if (auto foundList = context->rayTracingProcessedParams.tryGetValue(func))
2452-
{
2453-
processedParams = foundList;
2454-
if (processedParams->contains(pp))
2455-
return;
2456-
}
2457-
else
2458-
{
2459-
context->rayTracingProcessedParams[func] = List<IRParam*>();
2460-
processedParams = &context->rayTracingProcessedParams[func];
2461-
}
2462-
2463-
// Collect all parameters that need to be consolidated
2464-
List<IRParam*> params;
2465-
List<IRVarLayout*> paramLayouts;
2447+
// Collect all out/inout parameters that need to be consolidated
2448+
List<IRParam*> outParams;
2449+
List<IRParam*> otherParams;
24662450

2467-
for (auto _param = firstBlock->getFirstParam(); _param; _param = _param->getNextParam())
2451+
for (auto param = firstBlock->getFirstParam(); param; param = param->getNextParam())
24682452
{
2469-
auto pLayoutDecoration = _param->findDecoration<IRLayoutDecoration>();
2470-
SLANG_ASSERT(pLayoutDecoration);
2471-
auto pLayout = as<IRVarLayout>(pLayoutDecoration->getLayout());
2472-
SLANG_ASSERT(pLayout);
2473-
2474-
// Only include parameters that haven't been processed yet
2475-
auto _paramType = _param->getDataType();
2476-
bool needsConsolidation = (as<IROutType>(_paramType) || as<IRInOutType>(_paramType));
2477-
if (!processedParams->contains(_param) && needsConsolidation)
2453+
builder->setInsertBefore(firstBlock->getFirstOrdinaryInst());
2454+
if (as<IROutTypeBase>(param->getDataType()))
2455+
{
2456+
outParams.add(param);
2457+
}
2458+
else
24782459
{
2479-
params.add(_param);
2480-
paramLayouts.add(pLayout);
2481-
processedParams->add(_param);
2460+
otherParams.add(param);
24822461
}
24832462
}
24842463

2485-
consolidateParameters(context, func, params);
2486-
}
2487-
2488-
void legalizeRayTracingEntryPointParameterForGLSL(
2489-
GLSLLegalizationContext* context,
2490-
IRFunc* func,
2491-
IRParam* pp,
2492-
IRVarLayout* paramLayout,
2493-
bool hasSingleOutOrInOutParam)
2494-
{
2495-
if (hasSingleOutOrInOutParam)
2464+
// We don't need consolidation here.
2465+
if (outParams.getCount() <= 1)
24962466
{
2497-
handleSingleParam(context, func, pp, paramLayout);
2467+
// We have one out/inout param, so add it as part of otherParams so we can
2468+
// just do one pass of handleSingleParam().
2469+
if (outParams.getCount() == 1)
2470+
{
2471+
otherParams.add(outParams[0]);
2472+
}
2473+
for (auto param : otherParams)
2474+
{
2475+
auto paramLayoutDecoration = param->findDecoration<IRLayoutDecoration>();
2476+
SLANG_ASSERT(paramLayoutDecoration);
2477+
auto paramLayout = as<IRVarLayout>(paramLayoutDecoration->getLayout());
2478+
handleSingleParam(context, func, param, paramLayout);
2479+
}
24982480
return;
24992481
}
25002482

2501-
handleMultipleParams(context, func, pp);
2483+
// Consolidate the parameters
2484+
consolidateParameters(context, outParams);
25022485
}
25032486

25042487
static void legalizeMeshPayloadInputParam(
@@ -3344,28 +3327,6 @@ void legalizeEntryPointParameterForGLSL(
33443327
case Stage::Intersection:
33453328
case Stage::Miss:
33463329
case Stage::RayGeneration:
3347-
{
3348-
// Count the number of inout or out parameters
3349-
int inoutOrOutParamCount = 0;
3350-
auto firstBlock = func->getFirstBlock();
3351-
for (auto _param = firstBlock->getFirstParam(); _param; _param = _param->getNextParam())
3352-
{
3353-
auto _paramType = _param->getDataType();
3354-
if (as<IROutType>(_paramType) || as<IRInOutType>(_paramType))
3355-
{
3356-
inoutOrOutParamCount++;
3357-
}
3358-
}
3359-
3360-
// If we have just one inout or out param, we don't need consolidation.
3361-
bool hasSingleOutOrInOutParam = inoutOrOutParamCount <= 1;
3362-
legalizeRayTracingEntryPointParameterForGLSL(
3363-
context,
3364-
func,
3365-
pp,
3366-
paramLayout,
3367-
hasSingleOutOrInOutParam);
3368-
}
33693330
return;
33703331
}
33713332

@@ -3941,12 +3902,33 @@ void legalizeEntryPointForGLSL(
39413902
invokePathConstantFuncInHullShader(&context, codeGenContext, scalarizedGlobalOutput);
39423903
}
39433904

3905+
// Special handling for ray tracing shaders
3906+
bool isRayTracingShader = false;
3907+
switch (stage)
3908+
{
3909+
case Stage::AnyHit:
3910+
case Stage::Callable:
3911+
case Stage::ClosestHit:
3912+
case Stage::Intersection:
3913+
case Stage::Miss:
3914+
case Stage::RayGeneration:
3915+
isRayTracingShader = true;
3916+
consolidateRayTracingParameters(&context, func);
3917+
break;
3918+
default:
3919+
break;
3920+
}
3921+
39443922
// Next we will walk through any parameters of the entry-point function,
39453923
// and turn them into global variables.
39463924
if (auto firstBlock = func->getFirstBlock())
39473925
{
39483926
for (auto pp = firstBlock->getFirstParam(); pp; pp = pp->getNextParam())
39493927
{
3928+
if (isRayTracingShader)
3929+
{
3930+
continue;
3931+
}
39503932
// Any initialization code we insert for parameters needs
39513933
// to be at the start of the "ordinary" instructions in the block:
39523934
builder.setInsertBefore(firstBlock->getFirstOrdinaryInst());
@@ -3962,7 +3944,7 @@ void legalizeEntryPointForGLSL(
39623944

39633945
legalizeEntryPointParameterForGLSL(&context, codeGenContext, func, pp, paramLayout);
39643946
}
3965-
3947+
39663948
// At this point we should have eliminated all uses of the
39673949
// parameters of the entry block. Also, our control-flow
39683950
// rules mean that the entry block cannot be the target

0 commit comments

Comments
 (0)