From 047daae9300c8a94d28383cf992ce00e3ad2da1e Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Mon, 9 Sep 2019 13:54:31 -0400 Subject: CPU compute testing on non windows targets (#1045) * WIP: Refactor of CPUCompute and stand alone cpu-render-test * Fix compilation on CygWin. * Make CPU compute tests run on non windows targets. * Check that C/C++ compiler is available for CPU compute. * Fix some tabbing issues. * Add -fPIC on gfx * Use dxcompiler_47.dll from slang-binaries on windows. * make https for git module slang-binaries * Fix comment in premake5.lua around d3dcompiler_47.dll * Add resources to the CPUComputeUtil::Context to keep in scope. * Fixes problem compiling on cygwin where dx12 is included in build of gfx lib. --- tools/render-test/slang-support.cpp | 89 ++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 2 deletions(-) (limited to 'tools/render-test/slang-support.cpp') diff --git a/tools/render-test/slang-support.cpp b/tools/render-test/slang-support.cpp index 230f78453..df7e91df8 100644 --- a/tools/render-test/slang-support.cpp +++ b/tools/render-test/slang-support.cpp @@ -10,6 +10,12 @@ #include namespace renderer_test { +using namespace Slang; + +// Entry point name to use for vertex/fragment shader +static const char vertexEntryPointName[] = "vertexMain"; +static const char fragmentEntryPointName[] = "fragmentMain"; +static const char computeEntryPointName[] = "computeMain"; /* static */ SlangResult ShaderCompilerUtil::compileProgram(SlangSession* session, const Input& input, const ShaderCompileRequest& request, Output& out) { @@ -138,7 +144,9 @@ namespace renderer_test { { fprintf(stderr, "%s", diagnostics); } - if (SLANG_SUCCEEDED(res)) + + SLANG_RETURN_ON_FAIL(res); + { size_t codeSize = 0; char const* code = (char const*) spGetEntryPointCode(slangRequest, computeEntryPoint, &codeSize); @@ -166,7 +174,9 @@ namespace renderer_test { // OutputDebugStringA(diagnostics); fprintf(stderr, "%s", diagnostics); } - if (SLANG_SUCCEEDED(res)) + + SLANG_RETURN_ON_FAIL(res); + { size_t vertexCodeSize = 0; char const* vertexCode = (char const*) spGetEntryPointCode(slangRequest, vertexEntryPoint, &vertexCodeSize); @@ -193,4 +203,79 @@ namespace renderer_test { return SLANG_OK; } +/* static */SlangResult ShaderCompilerUtil::readSource(const String& inSourcePath, List& outSourceText) +{ + // Read in the source code + FILE* sourceFile = fopen(inSourcePath.getBuffer(), "rb"); + if (!sourceFile) + { + fprintf(stderr, "error: failed to open '%s' for reading\n", inSourcePath.getBuffer()); + return SLANG_FAIL; + } + fseek(sourceFile, 0, SEEK_END); + size_t sourceSize = ftell(sourceFile); + fseek(sourceFile, 0, SEEK_SET); + + outSourceText.setCount(sourceSize + 1); + fread(outSourceText.getBuffer(), sourceSize, 1, sourceFile); + fclose(sourceFile); + outSourceText[sourceSize] = 0; + + return SLANG_OK; +} + +/* static */SlangResult ShaderCompilerUtil::compileWithLayout(SlangSession* session, const String& sourcePath, Options::ShaderProgramType shaderType, const ShaderCompilerUtil::Input& input, OutputAndLayout& output) +{ + List sourceText; + SLANG_RETURN_ON_FAIL(readSource(sourcePath, sourceText)); + + output.sourcePath = sourcePath; + + auto& layout = output.layout; + + // Default the amount of renderTargets based on shader type + switch (shaderType) + { + default: + layout.numRenderTargets = 1; + break; + + case Options::ShaderProgramType::Compute: + layout.numRenderTargets = 0; + break; + } + + // Parse the layout + layout.parse(sourceText.getBuffer()); + layout.updateForTarget(input.target); + + // Setup SourceInfo + ShaderCompileRequest::SourceInfo sourceInfo; + sourceInfo.path = sourcePath.getBuffer(); + sourceInfo.dataBegin = sourceText.getBuffer(); + // Subtract 1 because it's zero terminated + sourceInfo.dataEnd = sourceText.getBuffer() + sourceText.getCount() - 1; + + ShaderCompileRequest compileRequest; + compileRequest.source = sourceInfo; + if (shaderType == Options::ShaderProgramType::Graphics || shaderType == Options::ShaderProgramType::GraphicsCompute) + { + compileRequest.vertexShader.source = sourceInfo; + compileRequest.vertexShader.name = vertexEntryPointName; + compileRequest.fragmentShader.source = sourceInfo; + compileRequest.fragmentShader.name = fragmentEntryPointName; + } + else + { + compileRequest.computeShader.source = sourceInfo; + compileRequest.computeShader.name = computeEntryPointName; + } + compileRequest.globalGenericTypeArguments = layout.globalGenericTypeArguments; + compileRequest.entryPointGenericTypeArguments = layout.entryPointGenericTypeArguments; + compileRequest.globalExistentialTypeArguments = layout.globalExistentialTypeArguments; + compileRequest.entryPointExistentialTypeArguments = layout.entryPointExistentialTypeArguments; + + return ShaderCompilerUtil::compileProgram(session, input, compileRequest, output.output); +} + } // renderer_test -- cgit v1.2.3