From f14c151883435c95b04de060917fc6b0b93b8958 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Tue, 27 Jul 2021 14:21:43 +0200 Subject: [PATCH] gpu,gpu/internal: generate hashes of compute programs The CPU fallback for the compute renderer is contained in a separate module for space reasons, but the CPU binaries must exactly match the compute programs. However, there is no way to express that constraint in go.mod. This change generates hashes of every compute program so that a following change can verify the CPU binaries match the programs. Signed-off-by: Elias Naur --- gpu/internal/convertshaders/main.go | 8 ++++++++ gpu/internal/driver/driver.go | 1 + gpu/shaders.go | 7 +++++++ 3 files changed, 16 insertions(+) diff --git a/gpu/internal/convertshaders/main.go b/gpu/internal/convertshaders/main.go index 40183554..b97b055f 100644 --- a/gpu/internal/convertshaders/main.go +++ b/gpu/internal/convertshaders/main.go @@ -4,6 +4,8 @@ package main import ( "bytes" + "crypto/sha256" + "encoding/hex" "errors" "flag" "fmt" @@ -232,6 +234,9 @@ func (conv *Converter) Run(out io.Writer) error { if len(src.HLSL) > 0 { fmt.Fprintf(out, "HLSL: %q,\n", src.HLSL) } + if len(src.Hash) > 0 { + fmt.Fprintf(out, "Hash: %q,\n", src.Hash) + } fmt.Fprintf(out, "}") if multiVariant { fmt.Fprintf(out, ",") @@ -366,6 +371,9 @@ func (conv *Converter) ComputeShader(shaderPath string) ([]driver.ShaderSources, var sources driver.ShaderSources sources.Name = filepath.Base(shaderPath) + sum := sha256.Sum256(shader) + sources.Hash = hex.EncodeToString(sum[:]) + sources.GLSL310ES, err = conv.spirv.Convert(shaderPath, "", spirv, "es", "310") if err != nil { return nil, fmt.Errorf("failed to convert es compute shader %q: %w", shaderPath, err) diff --git a/gpu/internal/driver/driver.go b/gpu/internal/driver/driver.go index 982f8715..c3332812 100644 --- a/gpu/internal/driver/driver.go +++ b/gpu/internal/driver/driver.go @@ -64,6 +64,7 @@ type ShaderSources struct { Uniforms UniformsReflection Inputs []InputLocation Textures []TextureBinding + Hash string } type UniformsReflection struct { diff --git a/gpu/shaders.go b/gpu/shaders.go index 68c300ea..1edb6dfb 100644 --- a/gpu/shaders.go +++ b/gpu/shaders.go @@ -250,6 +250,7 @@ void main() } `, + Hash: "6862eaf623d89da635e9d5bc981c77aae4e39aa44047ab47c62d243cf5fe7e73", } shader_binning_comp = driver.ShaderSources{ Name: "binning.comp", @@ -573,6 +574,7 @@ void main() } `, + Hash: "84177b6dfb90309a6c054ab9fea42293bd49033c221651c756eb40188f4d6ce8", } shader_blit_frag = [...]driver.ShaderSources{ { @@ -2172,6 +2174,7 @@ void main() } `, + Hash: "e7ef250c08701490aed979a889cca73943b988bdb5e4ca4b02735aebcf5e5505", } shader_copy_frag = driver.ShaderSources{ Name: "copy.frag", @@ -4204,6 +4207,7 @@ void main() } `, + Hash: "0f18de15866045b36217068789c9c8715a63e0f9f120c53ea2d4d76f53e443c3", } shader_intersect_frag = driver.ShaderSources{ Name: "intersect.frag", @@ -5066,6 +5070,7 @@ void main() } `, + Hash: "e72e482d7f40b2949426017cb5aeb4d391fcfc0e9d42e73bffb5a1e5576e906d", } shader_material_frag = driver.ShaderSources{ Name: "material.frag", @@ -5950,6 +5955,7 @@ void main() } `, + Hash: "ed67e14c880cf92bdd7a9d520610e8c8b139907ff8b55df20464d353a7f58e79", } shader_stencil_frag = driver.ShaderSources{ Name: "stencil.frag", @@ -6632,5 +6638,6 @@ void main() } `, + Hash: "364b3cf559d02a86c751292bedc571d5ceef2df899de39ad483b4176294e9857", } )