Skip to content

Commit

Permalink
kram - hlslparser update
Browse files Browse the repository at this point in the history
Use brew ninja build script for now.   It's fast.  Don't know why Xcode uses it's own sanitized path, instead of my own with brew and vulkan sdk in it.  Turn on -line to jump back to errors in original source files from IDE.  Turn on warnings as errors, just to keep the code clean.   This makes output hard to read, so recommend disabling it when developing the conversions.  VS supports same gcc/clang error/warning report, so use that.
  • Loading branch information
alecazam committed Mar 20, 2023
1 parent 476cf7e commit ea74a57
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 24 deletions.
58 changes: 45 additions & 13 deletions hlslparser/build.ninja
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand All @@ -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
Expand All @@ -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}
Expand All @@ -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
Expand All @@ -78,21 +105,23 @@ 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
command = $dxc $flagsDXC $flagsPS -E ${entryPoint}PS -Fo $out $in
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

Expand All @@ -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


8 changes: 4 additions & 4 deletions hlslparser/shaders/Sample.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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)
Expand Down
9 changes: 3 additions & 6 deletions hlslparser/src/HLSLTokenizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion hlslparser/testshaders.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
70CAA48C29BE6A62004B7E7B /* Compute.hlsl */ = {isa = PBXFileReference; lastKnownFileType = text; path = Compute.hlsl; sourceTree = "<group>"; };
70CAA48D29BE6A62004B7E7B /* Compute.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = Compute.metal; sourceTree = "<group>"; };
70CAA48E29BEB04C004B7E7B /* DepthTest.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = DepthTest.metal; sourceTree = "<group>"; };
70CAA48F29C63A46004B7E7B /* build.ninja */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = build.ninja; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXGroup section */
707D37A529B9787400B08D22 = {
isa = PBXGroup;
children = (
70CAA48F29C63A46004B7E7B /* build.ninja */,
707D37DA29B97A0900B08D22 /* buildShaders.sh */,
707D37D829B979EB00B08D22 /* shaders */,
707D37CA29B9797A00B08D22 /* outshaders */,
Expand Down Expand Up @@ -77,7 +79,7 @@
buildConfigurationList = 70CAA48629BAE9F5004B7E7B /* Build configuration list for PBXLegacyTarget "testshaders" */;
buildPhases = (
);
buildToolPath = ./buildShaders.sh;
buildToolPath = /opt/homebrew/bin/ninja;
buildWorkingDirectory = "";
dependencies = (
);
Expand Down

0 comments on commit ea74a57

Please sign in to comment.