diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2020-01-10 15:00:13 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-10 15:00:13 -0500 |
| commit | ef41dfc605f7868c0ccc7dde05982232b7d49589 (patch) | |
| tree | a2abe79250c234c65f968976db2578341ec77437 /source/slang/slang-emit-cpp.cpp | |
| parent | f2a123d727316d8203820a332da1348f78ad9ad6 (diff) | |
WIP: CPU like CUDA binding (#1164)
* CUDA generated first test compiles.
* WIP on enabling CUDA in render-test.
* Detect CUDA_PATH environmental variable to build build cuda support into render-test.
Added WIP cuda-compute-util.cpp/h
Added CUDA as a renderer type.
* Fix libraries needed for cuda in premake.
* Added -enable-cuda premake option. Defaults to false.
* Creates CUDA device, loads PTX and finds entry point.
* Fix some erroneous cruft from slang-cuda-prelude.h
* Made CUDA use C++ like ABI for generated code.
Fix small bug in C++ output semantics.
Diffstat (limited to 'source/slang/slang-emit-cpp.cpp')
| -rw-r--r-- | source/slang/slang-emit-cpp.cpp | 147 |
1 files changed, 78 insertions, 69 deletions
diff --git a/source/slang/slang-emit-cpp.cpp b/source/slang/slang-emit-cpp.cpp index f28c79a86..848ebd6e0 100644 --- a/source/slang/slang-emit-cpp.cpp +++ b/source/slang/slang-emit-cpp.cpp @@ -2085,7 +2085,7 @@ void CPPSourceEmitter::emitOperandImpl(IRInst* inst, EmitOpInfo const& outerPre } } -static bool _isVariable(IROp op) +/* static */bool CPPSourceEmitter::_isVariable(IROp op) { switch (op) { @@ -2104,18 +2104,6 @@ static bool _isFunction(IROp op) return op == kIROp_Func; } -struct GlobalParamInfo -{ - typedef GlobalParamInfo ThisType; - bool operator<(const ThisType& rhs) const { return offset < rhs.offset; } - bool operator==(const ThisType& rhs) const { return offset == rhs.offset; } - bool operator!=(const ThisType& rhs) const { return !(*this == rhs); } - - IRInst* inst; - UInt offset; - UInt size; -}; - void CPPSourceEmitter::_emitEntryPointDefinitionStart(IRFunc* func, IRGlobalParam* entryPointGlobalParams, const String& funcName, const UnownedStringSlice& varyingTypeName) { auto resultType = func->getResultType(); @@ -2129,8 +2117,9 @@ void CPPSourceEmitter::_emitEntryPointDefinitionStart(IRFunc* func, IRGlobalPara m_writer->emit("("); m_writer->emit(varyingTypeName); - m_writer->emit("* varyingInput, UniformEntryPointParams* params, UniformState* uniformState)\n{\n"); + m_writer->emit("* varyingInput, UniformEntryPointParams* params, UniformState* uniformState)"); emitSemantics(func); + m_writer->emit("\n{\n"); m_writer->indent(); // Initialize when constructing so that globals are zeroed @@ -2324,16 +2313,8 @@ void CPPSourceEmitter::_emitInitAxisValues(const Int sizeAlongAxis[kThreadGroupA m_writer->emit("};\n"); } -void CPPSourceEmitter::emitModuleImpl(IRModule* module) +void CPPSourceEmitter::_emitForwardDeclarations(const List<EmitAction>& actions) { - // Setup all built in types used in the module - m_typeSet.addAllBuiltinTypes(module); - // If any matrix types are used, then we need appropriate vector types too. - m_typeSet.addVectorForMatrixTypes(); - - List<EmitAction> actions; - computeEmitActions(module, actions); - // Emit forward declarations. Don't emit variables that need to be grouped or function definitions (which will ref those types) for (auto action : actions) { @@ -2355,67 +2336,95 @@ void CPPSourceEmitter::emitModuleImpl(IRModule* module) break; } } +} - IRGlobalParam* entryPointGlobalParams = nullptr; +void CPPSourceEmitter::_calcGlobalParams(const List<EmitAction>& actions, List<GlobalParamInfo>& outParams, IRGlobalParam** outEntryPointGlobalParams) +{ + outParams.clear(); + *outEntryPointGlobalParams = nullptr; - // Output the global parameters in a 'UniformState' structure + IRGlobalParam* entryPointGlobalParams = nullptr; + for (auto action : actions) { - m_writer->emit("struct UniformState\n{\n"); - m_writer->indent(); - - List<GlobalParamInfo> params; - - for (auto action : actions) + if (action.level == EmitAction::Level::Definition && action.inst->op == kIROp_GlobalParam) { - if (action.level == EmitAction::Level::Definition && action.inst->op == kIROp_GlobalParam) - { - auto inst = action.inst; + auto inst = action.inst; - if (inst->findDecorationImpl(kIROp_EntryPointParamDecoration)) - { - // Should only be one instruction marked this way - SLANG_ASSERT(entryPointGlobalParams == nullptr); - entryPointGlobalParams = as<IRGlobalParam>(inst); - continue; - } + if (inst->findDecorationImpl(kIROp_EntryPointParamDecoration)) + { + // Should only be one instruction marked this way + SLANG_ASSERT(entryPointGlobalParams == nullptr); + entryPointGlobalParams = as<IRGlobalParam>(inst); + continue; + } - IRVarLayout* varLayout = CLikeSourceEmitter::getVarLayout(action.inst); - SLANG_ASSERT(varLayout); + IRVarLayout* varLayout = CLikeSourceEmitter::getVarLayout(action.inst); + SLANG_ASSERT(varLayout); - IRVarOffsetAttr* offsetAttr = varLayout->findOffsetAttr(LayoutResourceKind::Uniform); - IRTypeLayout* typeLayout = varLayout->getTypeLayout(); - IRTypeSizeAttr* sizeAttr = typeLayout->findSizeAttr(LayoutResourceKind::Uniform); + IRVarOffsetAttr* offsetAttr = varLayout->findOffsetAttr(LayoutResourceKind::Uniform); + IRTypeLayout* typeLayout = varLayout->getTypeLayout(); + IRTypeSizeAttr* sizeAttr = typeLayout->findSizeAttr(LayoutResourceKind::Uniform); - GlobalParamInfo paramInfo; - paramInfo.inst = action.inst; - // Index is the byte offset for uniform - paramInfo.offset = offsetAttr ? offsetAttr->getOffset() : 0; - paramInfo.size = sizeAttr ? sizeAttr->getFiniteSize() : 0; + GlobalParamInfo paramInfo; + paramInfo.inst = action.inst; + // Index is the byte offset for uniform + paramInfo.offset = offsetAttr ? offsetAttr->getOffset() : 0; + paramInfo.size = sizeAttr ? sizeAttr->getFiniteSize() : 0; - params.add(paramInfo); - } + outParams.add(paramInfo); } + } - // We want to sort by layout offset, and insert suitable padding - params.sort(); + // We want to sort by layout offset, and insert suitable padding + outParams.sort(); - int padIndex = 0; - size_t offset = 0; - for (const auto& paramInfo : params) - { - if (offset < paramInfo.offset) - { - // We want to output some padding - StringBuilder builder; - builder << "uint8_t _pad" << (padIndex++) << "[" << (paramInfo.offset - offset) << "];\n"; - } + *outEntryPointGlobalParams = entryPointGlobalParams; +} + +void CPPSourceEmitter::_emitUniformStateMembers(const List<EmitAction>& actions, IRGlobalParam** outEntryPointGlobalParams) +{ + List<GlobalParamInfo> params; + _calcGlobalParams(actions, params, outEntryPointGlobalParams); - emitGlobalInst(paramInfo.inst); - // Set offset after this - offset = paramInfo.offset + paramInfo.size; + int padIndex = 0; + size_t offset = 0; + for (const auto& paramInfo : params) + { + if (offset < paramInfo.offset) + { + // We want to output some padding + StringBuilder builder; + builder << "uint8_t _pad" << (padIndex++) << "[" << (paramInfo.offset - offset) << "];\n"; } - m_writer->emit("\n"); + emitGlobalInst(paramInfo.inst); + // Set offset after this + offset = paramInfo.offset + paramInfo.size; + } + m_writer->emit("\n"); +} + +void CPPSourceEmitter::emitModuleImpl(IRModule* module) +{ + // Setup all built in types used in the module + m_typeSet.addAllBuiltinTypes(module); + // If any matrix types are used, then we need appropriate vector types too. + m_typeSet.addVectorForMatrixTypes(); + + List<EmitAction> actions; + computeEmitActions(module, actions); + + _emitForwardDeclarations(actions); + + IRGlobalParam* entryPointGlobalParams = nullptr; + + // Output the global parameters in a 'UniformState' structure + { + m_writer->emit("struct UniformState\n{\n"); + m_writer->indent(); + + _emitUniformStateMembers(actions, &entryPointGlobalParams); + m_writer->dedent(); m_writer->emit("\n};\n\n"); } |
