Skip to content

Commit e67af5b

Browse files
authored
Various Fixes to gfx, reflection and emit. (shader-slang#1867)
* Various Fixes to gfx, reflection and emit. - Fix GLSL emit to properly output `*bitsTo*` functions for `IRBitCast` insts. - Add line directive mode setting for `ISession`. - Extend `TypeLayout::getElementStride` to handle `VectorType` case. - Fix `IDevice::readBufferResource` 's D3D12 implementation to copy only the requested bytes out. - Fix `render-test` to use the `ISession` from `gfx` instead of creating its own `ISession` to make sure `gfx` and `render-test` agree on WitnessTable and RTTI IDs. - Extend `render-test` to support filling vector and matrix values in the new `set x = ...` TEST_INPUT syntax. - Add a `dynamic-dispatch-15` test case to make sure packing / unpacking works correctly across all targets, and to make sure render-test's RTTI/WitnessTable ID filling logic is correct for non-trivial cases. * Remove default-major test * Fix cyclic reference in `ExtendedTypeLayout`. * Move `lineDirectiveMode` setting to `TargetDesc`. Add `structureSize` to `TargetDesc` and `SessionDesc` for future binary compatibility. * Cleanup. Co-authored-by: Yong He <yhe@nvidia.com>
1 parent 8e57166 commit e67af5b

20 files changed

+260
-55
lines changed

slang-gfx.h

+1
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,7 @@ class IDevice: public ISlangUnknown
11401140
SlangFloatingPointMode floatingPointMode = SLANG_FLOATING_POINT_MODE_DEFAULT;
11411141
SlangOptimizationLevel optimizationLevel = SLANG_OPTIMIZATION_LEVEL_DEFAULT;
11421142
SlangTargetFlags targetFlags = 0;
1143+
SlangLineDirectiveMode lineDirectiveMode = SLANG_LINE_DIRECTIVE_MODE_DEFAULT;
11431144
};
11441145

11451146
struct Desc

slang.h

+25-2
Original file line numberDiff line numberDiff line change
@@ -1317,10 +1317,17 @@ extern "C"
13171317
SlangCompileRequest* request,
13181318
const char* prefix);
13191319

1320-
/*! @see slang::ICompileRequest::setLineDirectiveMode */
1320+
/*! DEPRECATED: use `spSetTargetLineDirectiveMode` instead.
1321+
@see slang::ICompileRequest::setLineDirectiveMode */
13211322
SLANG_API void spSetLineDirectiveMode(
13221323
SlangCompileRequest* request,
13231324
SlangLineDirectiveMode mode);
1325+
1326+
/*! @see slang::ICompileRequest::setTargetLineDirectiveMode */
1327+
SLANG_API void spSetTargetLineDirectiveMode(
1328+
SlangCompileRequest* request,
1329+
int targetIndex,
1330+
SlangLineDirectiveMode mode);
13241331

13251332
/*! @see slang::ICompileRequest::setCodeGenTarget */
13261333
SLANG_API void spSetCodeGenTarget(
@@ -3784,6 +3791,11 @@ namespace slang
37843791
virtual SLANG_NO_THROW SlangResult SLANG_MCALL getProgramWithEntryPoints(
37853792
slang::IComponentType** outProgram) = 0;
37863793

3794+
/** Set the line directive mode for a target.
3795+
*/
3796+
virtual SLANG_NO_THROW void SLANG_MCALL setTargetLineDirectiveMode(
3797+
SlangInt targetIndex,
3798+
SlangLineDirectiveMode mode) = 0;
37873799
};
37883800

37893801
#define SLANG_UUID_ICompileRequest ICompileRequest::getTypeGuid()
@@ -3792,6 +3804,10 @@ namespace slang
37923804
*/
37933805
struct TargetDesc
37943806
{
3807+
/** The size of this structure, in bytes.
3808+
*/
3809+
size_t structureSize = sizeof(TargetDesc);
3810+
37953811
/** The target format to generate code for (e.g., SPIR-V, DXIL, etc.)
37963812
*/
37973813
SlangCompileTarget format = SLANG_TARGET_UNKNOWN;
@@ -3810,6 +3826,10 @@ namespace slang
38103826
/** Optimization level to use for the target.
38113827
*/
38123828
SlangOptimizationLevel optimizationLevel = SLANG_OPTIMIZATION_LEVEL_DEFAULT;
3829+
3830+
/** The line directive mode for output source code.
3831+
*/
3832+
SlangLineDirectiveMode lineDirectiveMode = SLANG_LINE_DIRECTIVE_MODE_DEFAULT;
38133833
};
38143834

38153835
typedef uint32_t SessionFlags;
@@ -3836,6 +3856,10 @@ namespace slang
38363856

38373857
struct SessionDesc
38383858
{
3859+
/** The size of this structure, in bytes.
3860+
*/
3861+
size_t structureSize = sizeof(SessionDesc);
3862+
38393863
/** Code generation targets to include in the session.
38403864
*/
38413865
TargetDesc const* targets = nullptr;
@@ -3856,7 +3880,6 @@ namespace slang
38563880

38573881
PreprocessorMacroDesc const* preprocessorMacros = nullptr;
38583882
SlangInt preprocessorMacroCount = 0;
3859-
38603883
};
38613884

38623885
enum class ContainerType

source/slang/slang-api.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,15 @@ SLANG_API void spSetLineDirectiveMode(
272272
request->setLineDirectiveMode(mode);
273273
}
274274

275+
SLANG_API void spSetTargetLineDirectiveMode(
276+
slang::ICompileRequest* request,
277+
int targetIndex,
278+
SlangLineDirectiveMode mode)
279+
{
280+
SLANG_ASSERT(request);
281+
request->setTargetLineDirectiveMode(targetIndex, mode);
282+
}
283+
275284
SLANG_API void spSetCommandLineCompilerMode(
276285
slang::ICompileRequest* request)
277286
{

source/slang/slang-compiler.cpp

100755100644
-1
Original file line numberDiff line numberDiff line change
@@ -1961,7 +1961,6 @@ namespace Slang
19611961
m_program->getLinkage(),
19621962
sink,
19631963
m_program);
1964-
19651964
backEndRequest->shouldDumpIR =
19661965
(m_targetReq->getTargetFlags() & SLANG_TARGET_FLAG_DUMP_IR) != 0;
19671966

source/slang/slang-compiler.h

+19-7
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,10 @@ namespace Slang
11781178
{
11791179
floatingPointMode = mode;
11801180
}
1181+
void setLineDirectiveMode(LineDirectiveMode mode)
1182+
{
1183+
lineDirectiveMode = mode;
1184+
}
11811185
void addCapability(CapabilityAtom capability);
11821186

11831187

@@ -1190,6 +1194,7 @@ namespace Slang
11901194
CodeGenTarget getTarget() { return format; }
11911195
Profile getTargetProfile() { return targetProfile; }
11921196
FloatingPointMode getFloatingPointMode() { return floatingPointMode; }
1197+
LineDirectiveMode getLineDirectiveMode() { return lineDirectiveMode; }
11931198
SlangTargetFlags getTargetFlags() { return targetFlags; }
11941199
CapabilitySet getTargetCaps();
11951200

@@ -1211,6 +1216,7 @@ namespace Slang
12111216
FloatingPointMode floatingPointMode = FloatingPointMode::Default;
12121217
List<CapabilityAtom> rawCapabilities;
12131218
CapabilitySet cookedCapabilities;
1219+
LineDirectiveMode lineDirectiveMode = LineDirectiveMode::Default;
12141220
};
12151221

12161222
/// Are we generating code for a D3D API?
@@ -1867,11 +1873,6 @@ namespace Slang
18671873
// Should we dump intermediate results along the way, for debugging?
18681874
bool shouldDumpIntermediates = false;
18691875

1870-
// How should `#line` directives be emitted (if at all)?
1871-
LineDirectiveMode lineDirectiveMode = LineDirectiveMode::Default;
1872-
1873-
LineDirectiveMode getLineDirectiveMode() { return lineDirectiveMode; }
1874-
18751876
ComponentType* getProgram() { return m_program; }
18761877
void setProgram(ComponentType* program) { m_program = program; }
18771878

@@ -1976,7 +1977,9 @@ namespace Slang
19761977
virtual SLANG_NO_THROW void SLANG_MCALL setCommandLineCompilerMode() SLANG_OVERRIDE;
19771978
virtual SLANG_NO_THROW SlangResult SLANG_MCALL addTargetCapability(SlangInt targetIndex, SlangCapabilityID capability) SLANG_OVERRIDE;
19781979
virtual SLANG_NO_THROW SlangResult SLANG_MCALL getProgramWithEntryPoints(slang::IComponentType** outProgram) SLANG_OVERRIDE;
1979-
1980+
virtual SLANG_NO_THROW void SLANG_MCALL setTargetLineDirectiveMode(
1981+
SlangInt targetIndex,
1982+
SlangLineDirectiveMode mode) SLANG_OVERRIDE;
19801983

19811984
EndToEndCompileRequest(
19821985
Session* session);
@@ -2018,6 +2021,11 @@ namespace Slang
20182021
/// A blob holding the diagnostic output
20192022
ComPtr<ISlangBlob> m_diagnosticOutputBlob;
20202023

2024+
/// Line directive mode for new targets to be added to this request.
2025+
/// This is needed to support the legacy `setLineDirectiveMode` API.
2026+
/// We can remove this field if we move to `setTargetLineDirectiveMode`.
2027+
LineDirectiveMode m_lineDirectiveMode = LineDirectiveMode::Default;
2028+
20212029
/// Per-entry-point information not tracked by other compile requests
20222030
class EntryPointInfo : public RefObject
20232031
{
@@ -2083,7 +2091,11 @@ namespace Slang
20832091
{
20842092
return m_specializedEntryPoints[index];
20852093
}
2086-
2094+
~EndToEndCompileRequest()
2095+
{
2096+
m_linkage = nullptr;
2097+
m_frontEndReq = nullptr;
2098+
}
20872099
private:
20882100

20892101
ISlangUnknown* getInterface(const Guid& guid);

source/slang/slang-emit-c-like.h

-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ class CLikeSourceEmitter: public RefObject
195195

196196
/// Get the diagnostic sink
197197
DiagnosticSink* getSink() { return m_compileRequest->getSink();}
198-
LineDirectiveMode getLineDirectiveMode() { return m_compileRequest->getLineDirectiveMode(); }
199198

200199
/// Get the code gen target
201200
CodeGenTarget getTarget() { return m_target; }

source/slang/slang-emit-glsl.cpp

+29-2
Original file line numberDiff line numberDiff line change
@@ -1404,21 +1404,48 @@ bool GLSLSourceEmitter::tryEmitInstExprImpl(IRInst* inst, const EmitOpInfo& inOu
14041404
case kIROp_BitCast:
14051405
{
14061406
auto toType = extractBaseType(inst->getDataType());
1407+
auto fromType = extractBaseType(inst->getOperand(0)->getDataType());
14071408
switch (toType)
14081409
{
14091410
default:
14101411
diagnoseUnhandledInst(inst);
14111412
break;
14121413

14131414
case BaseType::UInt:
1415+
if (fromType == BaseType::Float)
1416+
{
1417+
m_writer->emit("floatBitsToUint");
1418+
}
1419+
else
1420+
{
1421+
emitType(inst->getDataType());
1422+
}
14141423
break;
14151424

14161425
case BaseType::Int:
1417-
emitType(inst->getDataType());
1426+
if (fromType == BaseType::Float)
1427+
{
1428+
m_writer->emit("floatBitsToInt");
1429+
}
1430+
else
1431+
{
1432+
emitType(inst->getDataType());
1433+
}
14181434
break;
14191435

14201436
case BaseType::Float:
1421-
m_writer->emit("uintBitsToFloat");
1437+
switch (fromType)
1438+
{
1439+
case BaseType::Int:
1440+
m_writer->emit("intBitsToFloat");
1441+
break;
1442+
case BaseType::UInt:
1443+
m_writer->emit("uintBitsToFloat");
1444+
break;
1445+
default:
1446+
emitType(inst->getDataType());
1447+
break;
1448+
}
14221449
break;
14231450
}
14241451

source/slang/slang-emit.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ SlangResult emitEntryPointsSourceFromIR(
729729
auto sink = compileRequest->getSink();
730730
auto program = compileRequest->getProgram();
731731

732-
auto lineDirectiveMode = compileRequest->getLineDirectiveMode();
732+
auto lineDirectiveMode = targetRequest->getLineDirectiveMode();
733733
// To try to make the default behavior reasonable, we will
734734
// always use C-style line directives (to give the user
735735
// good source locations on error messages from downstream

source/slang/slang-reflection-api.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,12 @@ SLANG_API size_t spReflectionTypeLayout_GetElementStride(SlangReflectionTypeLayo
946946
return 0;
947947
}
948948
}
949+
else if (auto vectorTypeLayout = as<VectorTypeLayout>(typeLayout))
950+
{
951+
auto resInfo = vectorTypeLayout->elementTypeLayout->FindResourceInfo(LayoutResourceKind::Uniform);
952+
if (!resInfo) return 0;
953+
return resInfo->count.getFiniteValue();
954+
}
949955

950956
return 0;
951957
}
@@ -971,7 +977,14 @@ SLANG_API SlangReflectionTypeLayout* spReflectionTypeLayout_GetElementTypeLayout
971977
{
972978
return convert(specializedTypeLayout->baseTypeLayout.Ptr());
973979
}
974-
980+
else if (auto vectorTypeLayout = as<VectorTypeLayout>(typeLayout))
981+
{
982+
return convert(vectorTypeLayout->elementTypeLayout);
983+
}
984+
else if (auto matrixTypeLayout = as<MatrixTypeLayout>(typeLayout))
985+
{
986+
return convert(matrixTypeLayout->elementTypeLayout);
987+
}
975988
return nullptr;
976989
}
977990

source/slang/slang-repro.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,6 @@ static String _scrubName(const String& in)
363363

364364
dst->compileFlags = request->getFrontEndReq()->compileFlags;
365365
dst->shouldDumpIntermediates = request->getBackEndReq()->shouldDumpIntermediates;
366-
dst->lineDirectiveMode = request->getBackEndReq()->lineDirectiveMode;
367366

368367
dst->debugInfoLevel = linkage->debugInfoLevel;
369368
dst->optimizationLevel = linkage->optimizationLevel;

source/slang/slang.cpp

+34-6
Original file line numberDiff line numberDiff line change
@@ -445,9 +445,16 @@ SLANG_NO_THROW SlangResult SLANG_MCALL Session::createSession(
445445
RefPtr<Linkage> linkage = new Linkage(this, astBuilder, getBuiltinLinkage());
446446

447447
Int targetCount = desc.targetCount;
448+
const uint8_t* targetDescPtr = reinterpret_cast<const uint8_t*>(desc.targets);
448449
for(Int ii = 0; ii < targetCount; ++ii)
449450
{
450-
linkage->addTarget(desc.targets[ii]);
451+
slang::TargetDesc targetDesc;
452+
// Copy the size field first.
453+
memcpy(&targetDesc.structureSize, targetDescPtr, sizeof(size_t));
454+
// Copy the entire desc structure.
455+
memcpy(&targetDesc, targetDescPtr, targetDesc.structureSize);
456+
linkage->addTarget(targetDesc);
457+
targetDescPtr += targetDesc.structureSize;
451458
}
452459

453460
if(desc.flags & slang::kSessionFlag_FalcorCustomSharedKeywordSemantics)
@@ -793,6 +800,7 @@ void Linkage::addTarget(
793800
target->setFloatingPointMode(FloatingPointMode(desc.floatingPointMode));
794801
target->addTargetFlags(desc.flags);
795802
target->setTargetProfile(Profile(desc.profile));
803+
target->setLineDirectiveMode(LineDirectiveMode(desc.lineDirectiveMode));
796804
}
797805

798806
#if 0
@@ -1034,6 +1042,7 @@ SLANG_NO_THROW SlangResult SLANG_MCALL Linkage::createCompileRequest(
10341042
SlangCompileRequest** outCompileRequest)
10351043
{
10361044
auto compileRequest = new EndToEndCompileRequest(this);
1045+
compileRequest->addRef();
10371046
*outCompileRequest = asExternal(compileRequest);
10381047
return SLANG_OK;
10391048
}
@@ -1060,7 +1069,6 @@ SlangResult Linkage::setMatrixLayoutMode(
10601069
return SLANG_OK;
10611070
}
10621071

1063-
10641072
//
10651073
// TargetRequest
10661074
//
@@ -2009,7 +2017,9 @@ BackEndCompileRequest::BackEndCompileRequest(
20092017
: CompileRequestBase(linkage, sink)
20102018
, m_program(program)
20112019
, m_dumpIntermediatePrefix("slang-dump-")
2012-
{}
2020+
2021+
{
2022+
}
20132023

20142024
EndToEndCompileRequest::EndToEndCompileRequest(
20152025
Session* session)
@@ -3840,8 +3850,16 @@ void EndToEndCompileRequest::setDumpIntermediatePrefix(const char* prefix)
38403850

38413851
void EndToEndCompileRequest::setLineDirectiveMode(SlangLineDirectiveMode mode)
38423852
{
3843-
// TODO: validation
3844-
getBackEndReq()->lineDirectiveMode = LineDirectiveMode(mode);
3853+
// This method is deprecated and user should call `setTargetLineDirectiveMode` instead.
3854+
// We provide the implementation here for backward compatibility.
3855+
// Targets added later will use `m_lineDirectiveMode`, so we update it to the new `mode`
3856+
// set by the user.
3857+
m_lineDirectiveMode = LineDirectiveMode(mode);
3858+
3859+
// Change all existing targets to use the new mode.
3860+
auto linkage = getLinkage();
3861+
for (auto& target : linkage->targets)
3862+
target->setLineDirectiveMode(m_lineDirectiveMode);
38453863
}
38463864

38473865
void EndToEndCompileRequest::setCommandLineCompilerMode()
@@ -3854,11 +3872,14 @@ void EndToEndCompileRequest::setCodeGenTarget(SlangCompileTarget target)
38543872
auto linkage = getLinkage();
38553873
linkage->targets.clear();
38563874
linkage->addTarget(CodeGenTarget(target));
3875+
linkage->targets[0]->setLineDirectiveMode(m_lineDirectiveMode);
38573876
}
38583877

38593878
int EndToEndCompileRequest::addCodeGenTarget(SlangCompileTarget target)
38603879
{
3861-
return (int)getLinkage()->addTarget(CodeGenTarget(target));
3880+
int targetIndex = (int)getLinkage()->addTarget(CodeGenTarget(target));
3881+
getLinkage()->targets[targetIndex]->setLineDirectiveMode(m_lineDirectiveMode);
3882+
return targetIndex;
38623883
}
38633884

38643885
void EndToEndCompileRequest::setTargetProfile(int targetIndex, SlangProfileID profile)
@@ -3887,6 +3908,13 @@ void EndToEndCompileRequest::setTargetMatrixLayoutMode(int targetIndex, SlangMat
38873908
setMatrixLayoutMode(mode);
38883909
}
38893910

3911+
void EndToEndCompileRequest::setTargetLineDirectiveMode(
3912+
SlangInt targetIndex,
3913+
SlangLineDirectiveMode mode)
3914+
{
3915+
getLinkage()->targets[targetIndex]->setLineDirectiveMode(LineDirectiveMode(mode));
3916+
}
3917+
38903918
SlangResult EndToEndCompileRequest::addTargetCapability(SlangInt targetIndex, SlangCapabilityID capability)
38913919
{
38923920
auto& targets = getLinkage()->targets;

0 commit comments

Comments
 (0)