diff --git a/hlslparser/build.ninja b/hlslparser/build.ninja index bd118bb9..17d6e513 100755 --- a/hlslparser/build.ninja +++ b/hlslparser/build.ninja @@ -10,6 +10,7 @@ hlslparser = ${appBuildDir}hlslparser metalCompile = xcrun -sdk macosx metal metalLib = xcrun -sdk macosx metallib +metalLibStrip = xcrun -sdk macosx metal-dsymutil # src files srcDir = ${projectDir}shaders/ @@ -20,11 +21,21 @@ intDir = ${projectDir}outshaders/ # compiled shader per platform dstDir = ${projectDir}out/mac/ +# turn on file/line directives to jump back to original sources +# and turn on comments in generated sources +flagsParser = -g -line -flagsParser = -g +# this makes metallib 3x bigger, but not optimized +#flagsMSLDebug = -g -# this is setup for .air file and metallibdsym creation -flagsMSL = -g -frecord-sources=flat -std=macos-metal2.3 +# this allows Xcode to load the sym file +flagsMSLDebug = -frecord-sources + +# target MSL2.3 for now on macOS +flagsMSL = -std=macos-metal2.3 -Werror + +# this seems to gen weird warnings +# # for iOS # -std=ios-metal2.3 @@ -35,16 +46,19 @@ rule genMSL # compile to .air rule compileMSL - command = $metalCompile $flagsMSL -c $in -o $out + command = $metalCompile $flagsMSLDebug $flagsMSL -c $in -o $out # https://developer.apple.com/documentation/metal/shader_libraries/compiling_shader_code_into_a_library_with_metal_s_command-line_tools?language=objc # linker to metallib rule linkMSL command = $metalLib -o $out $in +# strip metallib and gen metlalibsym +rule stripMSL + command = $metalLibStrip -flat -remove-source $in + dependsMSL = ${intDir}/ShaderMSL.h -# need correct dir # gen air build ${intDir}Skinning.metal: genMSL ${srcDir}Skinning.hlsl | ${dependsMSL} build ${intDir}Sample.metal: genMSL ${srcDir}Sample.hlsl | ${dependsMSL} @@ -54,14 +68,27 @@ build ${dstDir}Skinning.air: compileMSL ${intDir}Skinning.metal build ${dstDir}Sample.air: compileMSL ${intDir}Sample.metal build ${dstDir}Compute.air: compileMSL ${intDir}Compute.metal -# can ninja take file list? -build ${dstDir}Game.metallib: linkMSL ${dstDir}Skinning.air ${dstDir}Sample.air ${dstDir}Compute.air +# gen metallib (87K) +build ${dstDir}GameShaders.metallib: linkMSL ${dstDir}Skinning.air ${dstDir}Sample.air ${dstDir}Compute.air + +# this is getting run every time since it strips the metallib, how to prevent that +# also can compile all metal files into both metallib and dsycm +# this also doesn't make metallib any smaller, but is supposed to strip it +# goes from 299K down to 273K, but seems like it should go smaller +# with no debug flags, 87K and sym is 80K w/o debug, prob should skip gen of sym file +# if not in debug + +# gen metallibsym +build ${dstDir}GameShaders.metallibsym: stripMSL ${dstDir}GameShaders.metallib #------- dxc = ${vulkanSDK}dxc -flagsDXC = -nologo -Zi -Zpc -enable-16bit-types -HV 2021 -fspv-extension=SPV_KHR_shader_draw_parameters -spirv -fspv-target-env=vulkan1.2 +# flagsDXDebug = -Zi +flagsDXDebug = + +flagsDXC = -nologo -Zpc -enable-16bit-types -HV 2021 -Werror -fspv-extension=SPV_KHR_shader_draw_parameters -spirv -fspv-target-env=vulkan1.2 flagsVS = -T vs_6_2 flagsPS = -T ps_6_2 @@ -78,8 +105,7 @@ rule genHLSL # this will get shadow replaced entryPoint = Foo -# compile to dxil or spriv -# TODO: fix src/dstDir +# compile to spirv rule compileVS command = $dxc $flagsDXC $flagsVS -E ${entryPoint}VS -Fo $out $in rule compilePS @@ -87,12 +113,15 @@ rule compilePS rule compileCS command = $dxc $flagsDXC $flagsCS -E ${entryPoint}CS -Fo $out $in +rule archiveSpriv + command = zip -r $out $in + # gen hlsl build ${intDir}Skinning.hlsl: genHLSL ${srcDir}Skinning.hlsl | ${dependsHLSL} build ${intDir}Sample.hlsl: genHLSL ${srcDir}Sample.hlsl | ${dependsHLSL} build ${intDir}Compute.hlsl: genHLSL ${srcDir}Compute.hlsl | ${dependsHLSL} -# compile hlsl +# gen spv build ${dstDirHLSL}Skinning.vert.spv: compileVS ${intDir}Skinning.hlsl entryPoint = Skinning @@ -105,9 +134,12 @@ build ${dstDirHLSL}Sample.vert.spv: compileVS ${intDir}Sample.hlsl build ${dstDirHLSL}Sample.frag.spv: compilePS ${intDir}Sample.hlsl entryPoint = Sample -# could zip up files - build ${dstDirHLSL}Compute.comp.spv: compileCS ${intDir}Compute.hlsl entryPoint = Compute +# TODO: use strip command to gen pdb for each file, may only apply to DXIL + +# TODO: could zip spv to single archive (6k) +build ${dstDirHLSL}GameShaders.zip: archiveSpriv ${dstDirHLSL}Skinning.vert.spv ${dstDirHLSL}Skinning.frag.spv ${dstDirHLSL}Sample.vert.spv ${dstDirHLSL}Sample.frag.spv ${dstDirHLSL}Compute.comp.spv + diff --git a/hlslparser/shaders/Sample.hlsl b/hlslparser/shaders/Sample.hlsl index ea6ba57f..046091e6 100644 --- a/hlslparser/shaders/Sample.hlsl +++ b/hlslparser/shaders/Sample.hlsl @@ -107,10 +107,10 @@ half4 CalcLightingColor(float3 vLightPos, float3 vLightDir, half4 vLightColor, f half fDistFalloff = (half)saturate((vFalloffs.x - fDist) / vFalloffs.y); // Normalize from here on. - half3 vLightToPixelNormalized = normalize(vLightToPixelUnNormalized); + half3 vLightToPixelNormalized = (half3)normalize(vLightToPixelUnNormalized); // Angle falloff = 0 at vFalloffs.z, 1 at vFalloffs.z - vFalloffs.w - half3 lightDir = normalize(vLightDir); + half3 lightDir = (half3)normalize(vLightDir); half fCosAngle = dot(vLightToPixelNormalized, lightDir); half fAngleFalloff = saturate((fCosAngle - (half)vFalloffs.z) / (half)vFalloffs.w); @@ -179,13 +179,13 @@ OutputVS SampleVS(InputVS input) float4 SamplePS(InputPS input) : SV_Target0 { half4 diffuseColor = SampleH(diffuseMap, sampleWrap, input.uv); - half3 pixelNormal = CalcPerPixelNormal(input.uv, input.normal, input.tangent); + half3 pixelNormal = CalcPerPixelNormal(input.uv, (half3)input.normal, (half3)input.tangent); half4 totalLight = (half4)scene.ambientColor; for (int i = 0; i < NUM_LIGHTS; i++) { LightState light = scene.lights[i]; - half4 lightPass = CalcLightingColor(light.position, light.direction, light.color, light.falloff, input.worldpos.xyz, pixelNormal); + half4 lightPass = CalcLightingColor(light.position, light.direction, (half4)light.color, light.falloff, input.worldpos.xyz, pixelNormal); // only single light shadow map if (scene.sampleShadowMap && i == 0) diff --git a/hlslparser/src/HLSLTokenizer.cpp b/hlslparser/src/HLSLTokenizer.cpp index 1837394e..a332400f 100644 --- a/hlslparser/src/HLSLTokenizer.cpp +++ b/hlslparser/src/HLSLTokenizer.cpp @@ -651,13 +651,10 @@ void HLSLTokenizer::Error(const char* format, ...) // can log error/warning/info messages bool isError = true; -#if _MSC_VER - // VS convention - Log_Error("%s(%d): %s: %s\n", m_fileName, m_lineNumber, isError ? "error" : "warning", buffer); -#else - // Xcode convention (must be absolute filename for clickthrough) + + // Gcc/lcang convention (must be absolute filename for clickthrough) + // Visual Stuidio can pick up on this formatting too Log_Error("%s:%d: %s: %s\n", m_fileName, m_lineNumber, isError ? "error" : "warning", buffer); -#endif } void HLSLTokenizer::GetTokenName(char buffer[s_maxIdentifier]) const diff --git a/hlslparser/testshaders.xcodeproj/project.pbxproj b/hlslparser/testshaders.xcodeproj/project.pbxproj index f58f621b..cc7a2822 100644 --- a/hlslparser/testshaders.xcodeproj/project.pbxproj +++ b/hlslparser/testshaders.xcodeproj/project.pbxproj @@ -21,12 +21,14 @@ 70CAA48C29BE6A62004B7E7B /* Compute.hlsl */ = {isa = PBXFileReference; lastKnownFileType = text; path = Compute.hlsl; sourceTree = ""; }; 70CAA48D29BE6A62004B7E7B /* Compute.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = Compute.metal; sourceTree = ""; }; 70CAA48E29BEB04C004B7E7B /* DepthTest.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = DepthTest.metal; sourceTree = ""; }; + 70CAA48F29C63A46004B7E7B /* build.ninja */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = build.ninja; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXGroup section */ 707D37A529B9787400B08D22 = { isa = PBXGroup; children = ( + 70CAA48F29C63A46004B7E7B /* build.ninja */, 707D37DA29B97A0900B08D22 /* buildShaders.sh */, 707D37D829B979EB00B08D22 /* shaders */, 707D37CA29B9797A00B08D22 /* outshaders */, @@ -77,7 +79,7 @@ buildConfigurationList = 70CAA48629BAE9F5004B7E7B /* Build configuration list for PBXLegacyTarget "testshaders" */; buildPhases = ( ); - buildToolPath = ./buildShaders.sh; + buildToolPath = /opt/homebrew/bin/ninja; buildWorkingDirectory = ""; dependencies = ( );