Skip to content

Commit 3b0b920

Browse files
authored
Bug fix for optix SBT access (shader-slang#1922)
* optix SBT record data can now be accessed using uniform parameters on ray tracing entry points * Update slang-emit.cpp * fixing a bug where SBT instruction was missing a location at which to insert. Switching back to emitFieldExtract and accounting for changes in instruction emission location
1 parent 858c7c5 commit 3b0b920

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

source/slang/slang-emit-cuda.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -813,9 +813,9 @@ bool CUDASourceEmitter::tryEmitInstExprImpl(IRInst* inst, const EmitOpInfo& inOu
813813
}
814814
case kIROp_GetOptiXSbtDataPtr:
815815
{
816-
m_writer->emit("(*((");
816+
m_writer->emit("((");
817817
emitType(inst->getDataType());
818-
m_writer->emit(")optixGetSbtDataPointer()))");
818+
m_writer->emit(")optixGetSbtDataPointer())");
819819
return true;
820820
}
821821
default: break;

source/slang/slang-ir-optix-entry-point-uniforms.cpp

+9-7
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,16 @@ struct CollectOptixEntryPointUniformParams : PerEntryPointPass {
234234
//
235235
IRInst* fieldVal = nullptr;
236236

237-
// Note: for an optix SBT pointer, we can't dereference
238-
// optixGetSbtDataPointer() like builder->emitFieldAddress requires.
239-
// (thus, this code differs from slang-ir-entry-point-uniforms.cpp)
240-
// Instead, we always use emitFieldExtract, and then the SBT instruction
241-
// emits a C-style cast to the appropriate struct type.
242-
fieldVal = builder->emitFieldExtract(
243-
paramType,
237+
// A constant buffer behaves like a pointer
238+
// at the IR level, so we first do a pointer
239+
// offset operation to compute what amounts
240+
// to `&cb->field`, and then load from that address.
241+
//
242+
auto fieldAddress = builder->emitFieldAddress(
243+
builder->getPtrType(paramType),
244244
collectedParam,
245245
paramFieldKey);
246+
fieldVal = builder->emitLoad(fieldAddress);
246247

247248
// We replace the value used at this use site, which
248249
// will have a side effect of making `use` no longer
@@ -270,6 +271,7 @@ struct CollectOptixEntryPointUniformParams : PerEntryPointPass {
270271

271272
// Now, replace the collected parameter with OptiX SBT accesses.
272273
auto paramType = collectedParam->getFullType();
274+
builder->setInsertBefore(entryPointFunc->getFirstBlock()->getFirstOrdinaryInst());
273275
IRInst* getAttr = builder->emitIntrinsicInst(paramType, kIROp_GetOptiXSbtDataPtr, 0, nullptr);
274276
collectedParam->replaceUsesWith(getAttr);
275277
collectedParam->removeAndDeallocate();

0 commit comments

Comments
 (0)