Skip to content

Commit 7d9d130

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

File tree

1 file changed

+60
-70
lines changed

1 file changed

+60
-70
lines changed

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

+60-70
Original file line numberDiff line numberDiff line change
@@ -2306,7 +2306,7 @@ IRInst* materializeValue(IRBuilder* builder, ScalarizedVal const& val)
23062306
}
23072307
}
23082308

2309-
static void handleSingleParam(
2309+
void handleSingleParam(
23102310
GLSLLegalizationContext* context,
23112311
IRFunc* func,
23122312
IRParam* pp,
@@ -2381,7 +2381,8 @@ static void consolidateParameters(
23812381
valueType = inOutType->getValueType();
23822382

23832383
auto key = builder->createStructKey();
2384-
builder->addNameHintDecoration(key, UnownedStringSlice("field"));
2384+
if (auto nameDecor = _param->findDecoration<IRNameHintDecoration>())
2385+
builder->addNameHintDecoration(key, nameDecor->getName());
23852386
auto field = builder->createStructField(structType, key, valueType);
23862387
field->removeFromParent();
23872388
field->insertAtEnd(structType);
@@ -2441,64 +2442,54 @@ static void consolidateParameters(
24412442
}
24422443
}
24432444

2444-
static void handleMultipleParams(GLSLLegalizationContext* context, IRFunc* func, IRParam* pp)
2445+
// Consolidate ray tracing parameters for an entry point function
2446+
void consolidateRayTracingParameters(
2447+
GLSLLegalizationContext* context,
2448+
IRFunc* func)
24452449
{
2450+
auto builder = context->getBuilder();
24462451
auto firstBlock = func->getFirstBlock();
2452+
if (!firstBlock)
2453+
return;
24472454

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;
2455+
// Collect all out/inout parameters that need to be consolidated
2456+
List<IRParam*> outParams;
2457+
List<IRParam*> otherParams;
24662458

2467-
for (auto _param = firstBlock->getFirstParam(); _param; _param = _param->getNextParam())
2459+
for (auto param = firstBlock->getFirstParam(); param; param = param->getNextParam())
24682460
{
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)
2461+
builder->setInsertBefore(firstBlock->getFirstOrdinaryInst());
2462+
if (as<IROutTypeBase>(param->getDataType()))
2463+
{
2464+
outParams.add(param);
2465+
}
2466+
else
24782467
{
2479-
params.add(_param);
2480-
paramLayouts.add(pLayout);
2481-
processedParams->add(_param);
2468+
otherParams.add(param);
24822469
}
24832470
}
24842471

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)
2472+
// We don't need consolidation here.
2473+
if (outParams.getCount() <= 1)
24962474
{
2497-
handleSingleParam(context, func, pp, paramLayout);
2475+
// We have one out/inout param, so add it as part of otherParams so we can
2476+
// just do one pass of handleSingleParam().
2477+
if (outParams.getCount() == 1)
2478+
{
2479+
otherParams.add(outParams[0]);
2480+
}
2481+
for (auto param : otherParams)
2482+
{
2483+
auto paramLayoutDecoration = param->findDecoration<IRLayoutDecoration>();
2484+
SLANG_ASSERT(paramLayoutDecoration);
2485+
auto paramLayout = as<IRVarLayout>(paramLayoutDecoration->getLayout());
2486+
handleSingleParam(context, func, param, paramLayout);
2487+
}
24982488
return;
24992489
}
25002490

2501-
handleMultipleParams(context, func, pp);
2491+
// Consolidate the parameters
2492+
consolidateParameters(context, func, outParams);
25022493
}
25032494

25042495
static void legalizeMeshPayloadInputParam(
@@ -3344,28 +3335,6 @@ void legalizeEntryPointParameterForGLSL(
33443335
case Stage::Intersection:
33453336
case Stage::Miss:
33463337
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-
}
33693338
return;
33703339
}
33713340

@@ -3941,12 +3910,33 @@ void legalizeEntryPointForGLSL(
39413910
invokePathConstantFuncInHullShader(&context, codeGenContext, scalarizedGlobalOutput);
39423911
}
39433912

3913+
// Special handling for ray tracing shaders
3914+
bool isRayTracingShader = false;
3915+
switch (stage)
3916+
{
3917+
case Stage::AnyHit:
3918+
case Stage::Callable:
3919+
case Stage::ClosestHit:
3920+
case Stage::Intersection:
3921+
case Stage::Miss:
3922+
case Stage::RayGeneration:
3923+
isRayTracingShader = true;
3924+
consolidateRayTracingParameters(&context, func);
3925+
break;
3926+
default:
3927+
break;
3928+
}
3929+
39443930
// Next we will walk through any parameters of the entry-point function,
39453931
// and turn them into global variables.
39463932
if (auto firstBlock = func->getFirstBlock())
39473933
{
39483934
for (auto pp = firstBlock->getFirstParam(); pp; pp = pp->getNextParam())
39493935
{
3936+
if (isRayTracingShader)
3937+
{
3938+
continue;
3939+
}
39503940
// Any initialization code we insert for parameters needs
39513941
// to be at the start of the "ordinary" instructions in the block:
39523942
builder.setInsertBefore(firstBlock->getFirstOrdinaryInst());
@@ -3962,7 +3952,7 @@ void legalizeEntryPointForGLSL(
39623952

39633953
legalizeEntryPointParameterForGLSL(&context, codeGenContext, func, pp, paramLayout);
39643954
}
3965-
3955+
39663956
// At this point we should have eliminated all uses of the
39673957
// parameters of the entry block. Also, our control-flow
39683958
// rules mean that the entry block cannot be the target

0 commit comments

Comments
 (0)