diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2020-01-24 15:06:08 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-24 15:06:08 -0500 |
| commit | b8f294445b998eadb9b09e2b91eb462b881eaf2e (patch) | |
| tree | 8607e5d2f6c2c2b4b7545a721d6d58e6e557e5c0 /source/slang | |
| parent | 394983d61efa2bf99ba96aa68a47df8927a8a634 (diff) | |
Texture Sample available in CUDA (#1176)
* WIP: Trying to figure out how texturing will work with CUDA.
* WIP: Fixes for CUDA layout. Initial CUDA texture test.
* WIP: Outputs something compilable by CUDA for TextureND.Sample
* 2d texture working with CUDA.
* Fix how binding for SamplerState occurs in CUDA.
* Small tidy up of comments.
Diffstat (limited to 'source/slang')
| -rw-r--r-- | source/slang/core.meta.slang | 19 | ||||
| -rw-r--r-- | source/slang/core.meta.slang.h | 21 | ||||
| -rw-r--r-- | source/slang/slang-emit-c-like.cpp | 4 | ||||
| -rw-r--r-- | source/slang/slang-emit-cpp.cpp | 35 | ||||
| -rw-r--r-- | source/slang/slang-emit-cuda.cpp | 11 | ||||
| -rw-r--r-- | source/slang/slang-type-layout.cpp | 22 |
6 files changed, 75 insertions, 37 deletions
diff --git a/source/slang/core.meta.slang b/source/slang/core.meta.slang index 80ff09ea8..58e6e287c 100644 --- a/source/slang/core.meta.slang +++ b/source/slang/core.meta.slang @@ -770,6 +770,7 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) sb << "$1"; } sb << ")$z\")\n"; + } sb << "T Load("; sb << "int" << loadCoordCount << " location"; @@ -887,6 +888,24 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) // `Sample()` sb << "__target_intrinsic(glsl, \"$ctexture($p, $2)$z\")\n"; + + if( baseShape != TextureFlavor::Shape::ShapeCube ) + { + sb << "__target_intrinsic(cuda, \"tex" << kBaseTextureTypes[tt].coordCount << "D<$S0>($0"; + if (kBaseTextureTypes[tt].coordCount == 1) + { + sb << ", $2"; + } + else + { + for (int i = 0; i < kBaseTextureTypes[tt].coordCount; ++i) + { + sb << ", ($2)." << char(i + 'x'); + } + } + sb << ")\")\n"; + } + sb << "T Sample(SamplerState s, "; sb << "float" << kBaseTextureTypes[tt].coordCount + isArray << " location);\n"; diff --git a/source/slang/core.meta.slang.h b/source/slang/core.meta.slang.h index 27811b588..201429700 100644 --- a/source/slang/core.meta.slang.h +++ b/source/slang/core.meta.slang.h @@ -791,6 +791,7 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) sb << "$1"; } sb << ")$z\")\n"; + } sb << "T Load("; sb << "int" << loadCoordCount << " location"; @@ -908,6 +909,24 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) // `Sample()` sb << "__target_intrinsic(glsl, \"$ctexture($p, $2)$z\")\n"; + + if( baseShape != TextureFlavor::Shape::ShapeCube ) + { + sb << "__target_intrinsic(cuda, \"tex" << kBaseTextureTypes[tt].coordCount << "D<$S0>($0"; + if (kBaseTextureTypes[tt].coordCount == 1) + { + sb << ", $2"; + } + else + { + for (int i = 0; i < kBaseTextureTypes[tt].coordCount; ++i) + { + sb << ", ($2)." << char(i + 'x'); + } + } + sb << ")\")\n"; + } + sb << "T Sample(SamplerState s, "; sb << "float" << kBaseTextureTypes[tt].coordCount + isArray << " location);\n"; @@ -1258,7 +1277,7 @@ for (auto op : binaryOps) sb << "__intrinsic_op(" << int(op.opCode) << ") matrix<" << resultType << ",N,M> operator" << op.opName << "(" << leftQual << "matrix<" << leftType << ",N,M> left, " << rightType << " right);\n"; } } -SLANG_RAW("#line 1240 \"core.meta.slang\"") +SLANG_RAW("#line 1259 \"core.meta.slang\"") SLANG_RAW("\n") SLANG_RAW("\n") SLANG_RAW("// Specialized function\n") diff --git a/source/slang/slang-emit-c-like.cpp b/source/slang/slang-emit-c-like.cpp index 10790567e..8e0c64d1a 100644 --- a/source/slang/slang-emit-c-like.cpp +++ b/source/slang/slang-emit-c-like.cpp @@ -1250,6 +1250,10 @@ void CLikeSourceEmitter::emitIntrinsicCallExprImpl( SLANG_RELEASE_ASSERT(argCount > argIndex); IRType* type = args[argIndex].get()->getDataType(); + if (auto baseTextureType = as<IRTextureType>(type)) + { + type = baseTextureType->getElementType(); + } IRBasicType* underlyingType = nullptr; if (auto basicType = as<IRBasicType>(type)) diff --git a/source/slang/slang-emit-cpp.cpp b/source/slang/slang-emit-cpp.cpp index 5ea3c72a2..759f980d3 100644 --- a/source/slang/slang-emit-cpp.cpp +++ b/source/slang/slang-emit-cpp.cpp @@ -1912,48 +1912,23 @@ void CPPSourceEmitter::emitIntrinsicCallExprImpl( return; } - auto prec = getInfo(EmitOp::Postfix); - needClose = maybeEmitParens(outerPrec, prec); - - if (name[0] == '.') - { - // Looks like a member function call - emitOperand(args[0].get(), leftSide(outerPrec, prec)); - m_writer->emit("."); - - name = UnownedStringSlice(name.begin() + 1, name.end()); - - args++; - argCount--; - } - else { Op op = m_opLookup->getOpByName(name); if (op != Op::Invalid) { - + // Work out the intrinsic used HLSLIntrinsic intrinsic; m_intrinsicSet.calcIntrinsic(op, inst->getDataType(), args, argCount, intrinsic); HLSLIntrinsic* specOp = m_intrinsicSet.add(intrinsic); - + emitCall(specOp, inst, args, int(argCount), inOuterPrec); return; } } - - m_writer->emit(name); - m_writer->emit("("); - for (Index i = 0; i < argCount; ++i) - { - if (i != 0) - { - m_writer->emit(", "); - } - emitOperand(args[i].get(), getInfo(EmitOp::General)); - } - m_writer->emit(")"); - maybeCloseParens(needClose); + + // Use default impl (which will do intrinsic special macro expansion as necessary) + return Super::emitIntrinsicCallExprImpl(inst, targetIntrinsic, inOuterPrec); } bool CPPSourceEmitter::_tryEmitInstExprAsIntrinsic(IRInst* inst, const EmitOpInfo& inOuterPrec) diff --git a/source/slang/slang-emit-cuda.cpp b/source/slang/slang-emit-cuda.cpp index 93508813b..26d6eada0 100644 --- a/source/slang/slang-emit-cuda.cpp +++ b/source/slang/slang-emit-cuda.cpp @@ -89,6 +89,9 @@ SlangResult CUDASourceEmitter::_calcCUDATextureTypeName(IRTextureTypeBase* texTy return SLANG_FAIL; } + outName << "CUtexObject"; + +#if 0 outName << "texture<"; outName << _getTypeName(texType->getElementType()); outName << ", "; @@ -124,6 +127,7 @@ SlangResult CUDASourceEmitter::_calcCUDATextureTypeName(IRTextureTypeBase* texTy } outName << ">"; +#endif return SLANG_OK; } @@ -312,6 +316,13 @@ SlangResult CUDASourceEmitter::calcTypeName(IRType* type, CodeGenTarget target, } } + switch (type->op) + { + case kIROp_SamplerStateType: out << "SamplerState"; return SLANG_OK; + case kIROp_SamplerComparisonStateType: out << "SamplerComparisonState"; return SLANG_OK; + default: break; + } + break; } } diff --git a/source/slang/slang-type-layout.cpp b/source/slang/slang-type-layout.cpp index 644f54a95..cf793b52d 100644 --- a/source/slang/slang-type-layout.cpp +++ b/source/slang/slang-type-layout.cpp @@ -735,25 +735,30 @@ struct CUDAObjectLayoutRulesImpl : CPUObjectLayoutRulesImpl { typedef CPUObjectLayoutRulesImpl Super; + // cuda.h defines a variety of handle types. We don't want to have to include cuda.h though - as it may not be available + // on a build target. So for we define this handle type, that matches cuda.h and is used for types that use this kind + // of opaque handle (as opposed to a pointer) such as CUsurfObject, CUtexObject + typedef unsigned long long ObjectHandle; + virtual SimpleLayoutInfo GetObjectLayout(ShaderParameterKind kind) override { switch (kind) { case ShaderParameterKind::ConstantBuffer: // It's a pointer to the actual uniform data - return SimpleLayoutInfo(LayoutResourceKind::Uniform, sizeof(void*), sizeof(void*)); + return SimpleLayoutInfo(LayoutResourceKind::Uniform, sizeof(void*), SLANG_ALIGN_OF(void*)); case ShaderParameterKind::MutableTexture: case ShaderParameterKind::TextureUniformBuffer: case ShaderParameterKind::Texture: // It's a pointer to a texture interface - return SimpleLayoutInfo(LayoutResourceKind::Uniform, sizeof(void*), sizeof(void*)); + return SimpleLayoutInfo(LayoutResourceKind::Uniform, sizeof(ObjectHandle), SLANG_ALIGN_OF(ObjectHandle)); case ShaderParameterKind::StructuredBuffer: case ShaderParameterKind::MutableStructuredBuffer: // TODO(JS): We are just storing as a pointer for now // It's a ptr and a size of the amount of elements - return SimpleLayoutInfo(LayoutResourceKind::Uniform, sizeof(void*), sizeof(void*)); + return SimpleLayoutInfo(LayoutResourceKind::Uniform, sizeof(void*), SLANG_ALIGN_OF(void*)); case ShaderParameterKind::RawBuffer: case ShaderParameterKind::Buffer: @@ -763,11 +768,16 @@ struct CUDAObjectLayoutRulesImpl : CPUObjectLayoutRulesImpl // TODO(JS): We are storing as a pointer for now // It's a pointer and a size in bytes - return SimpleLayoutInfo(LayoutResourceKind::Uniform, sizeof(void*), sizeof(void*)); + return SimpleLayoutInfo(LayoutResourceKind::Uniform, sizeof(void*), SLANG_ALIGN_OF(void*)); case ShaderParameterKind::SamplerState: - // It's a pointer - return SimpleLayoutInfo(LayoutResourceKind::Uniform, sizeof(void*), sizeof(void*)); + // In CUDA it seems that sampler states are combined into texture objects. + // So it's a binding issue to combine a sampler with a texture - and sampler are ignored + // For simplicity here though - we do create a variable and that variable takes up + // uniform binding space. + // TODO(JS): If we wanted to remove these variables we'd want to do it as a pass. The pass + // would presumably have to remove use of variables of this kind throughout IR. + return SimpleLayoutInfo(LayoutResourceKind::Uniform, sizeof(void*), SLANG_ALIGN_OF(void*)); case ShaderParameterKind::TextureSampler: case ShaderParameterKind::MutableTextureSampler: |
