shaderprecomp: remove ShaderSource and ShaderSourceID

This change simplifies the APIs to avoid some confusions around IDs.

Updates #2861
Closes #2999
This commit is contained in:
Hajime Hoshi 2024-05-26 21:40:11 +09:00
parent 3279688dd6
commit 8be3bb41d5
9 changed files with 53 additions and 91 deletions

View File

@ -20,8 +20,10 @@
package main
import (
"encoding/hex"
"errors"
"fmt"
"hash/fnv"
"os"
"os/exec"
"path/filepath"
@ -57,11 +59,7 @@ func run() error {
srcs := shaderprecomp.AppendBuildinShaderSources(nil)
defaultSrcBytes, err := os.ReadFile(filepath.Join("..", "defaultshader.go"))
if err != nil {
return err
}
defaultSrc, err := shaderprecomp.NewShaderSource(defaultSrcBytes)
defaultSrc, err := os.ReadFile(filepath.Join("..", "defaultshader.go"))
if err != nil {
return err
}
@ -77,9 +75,7 @@ func run() error {
return nil
}
func generateHSLSFiles(source *shaderprecomp.ShaderSource, tmpdir string) (vs, ps string, err error) {
id := source.ID().String()
func generateHSLSFiles(source []byte, id string, tmpdir string) (vs, ps string, err error) {
vsHLSLFilePath := filepath.Join(tmpdir, id+"_vs.hlsl")
vsf, err := os.Create(vsHLSLFilePath)
if err != nil {
@ -101,16 +97,18 @@ func generateHSLSFiles(source *shaderprecomp.ShaderSource, tmpdir string) (vs, p
return vsHLSLFilePath, psHLSLFilePath, nil
}
func compile(source *shaderprecomp.ShaderSource, tmpdir string) error {
func compile(kageSource []byte, tmpdir string) error {
h := fnv.New32()
_, _ = h.Write(kageSource)
id := hex.EncodeToString(h.Sum(nil))
// Generate HLSL files. Make sure this process doesn't have any handlers of the files.
// Without closing the files, fxc.exe cannot access the files.
vsHLSLFilePath, psHLSLFilePath, err := generateHSLSFiles(source, tmpdir)
vsHLSLFilePath, psHLSLFilePath, err := generateHSLSFiles(kageSource, id, tmpdir)
if err != nil {
return err
}
id := source.ID().String()
vsFXCFilePath := id + "_vs.fxc"
cmd := exec.Command("fxc.exe", "/nologo", "/O3", "/T", shaderprecomp.HLSLVertexShaderProfile, "/E", shaderprecomp.HLSLVertexShaderEntryPoint, "/Fo", vsFXCFilePath, vsHLSLFilePath)
cmd.Stdout = os.Stdout

View File

@ -20,6 +20,8 @@
package main
import (
"encoding/hex"
"hash/fnv"
"os"
"os/exec"
"path/filepath"
@ -42,11 +44,7 @@ func run() error {
srcs := shaderprecomp.AppendBuildinShaderSources(nil)
defaultSrcBytes, err := os.ReadFile(filepath.Join("..", "defaultshader.go"))
if err != nil {
return err
}
defaultSrc, err := shaderprecomp.NewShaderSource(defaultSrcBytes)
defaultSrc, err := os.ReadFile(filepath.Join("..", "defaultshader.go"))
if err != nil {
return err
}
@ -62,8 +60,10 @@ func run() error {
return nil
}
func compile(source *shaderprecomp.ShaderSource, tmpdir string) error {
id := source.ID().String()
func compile(kageSource []byte, tmpdir string) error {
h := fnv.New32()
_, _ = h.Write(kageSource)
id := hex.EncodeToString(h.Sum(nil))
metalFilePath := filepath.Join(tmpdir, id+".metal")
@ -73,7 +73,7 @@ func compile(source *shaderprecomp.ShaderSource, tmpdir string) error {
}
defer f.Close()
if err := shaderprecomp.CompileToMSL(f, source); err != nil {
if err := shaderprecomp.CompileToMSL(f, kageSource); err != nil {
return err
}
if err := f.Sync(); err != nil {

View File

@ -16,8 +16,10 @@ package main
import (
"embed"
"encoding/hex"
"errors"
"fmt"
"hash/fnv"
"io/fs"
"os"
@ -29,14 +31,16 @@ var metallibs embed.FS
func registerPrecompiledShaders() error {
srcs := shaderprecomp.AppendBuildinShaderSources(nil)
defaultShaderSource, err := shaderprecomp.NewShaderSource(defaultShaderSourceBytes)
if err != nil {
return err
}
srcs = append(srcs, defaultShaderSource)
srcs = append(srcs, defaultShaderSourceBytes)
for _, src := range srcs {
name := src.ID().String() + ".metallib"
// Calculate the hash of the source code to identify the Metal library.
// FNV is used as it is fast and the hash does not need to be secure.
h := fnv.New32()
_, _ = h.Write(src)
id := hex.EncodeToString(h.Sum(nil))
name := id + ".metallib"
lib, err := metallibs.ReadFile("metallib/" + name)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {

View File

@ -16,8 +16,10 @@ package main
import (
"embed"
"encoding/hex"
"errors"
"fmt"
"hash/fnv"
"io/fs"
"os"
@ -31,14 +33,16 @@ var fxcs embed.FS
func registerPrecompiledShaders() error {
srcs := shaderprecomp.AppendBuildinShaderSources(nil)
defaultShaderSource, err := shaderprecomp.NewShaderSource(defaultShaderSourceBytes)
if err != nil {
return err
}
srcs = append(srcs, defaultShaderSource)
srcs = append(srcs, defaultShaderSourceBytes)
for _, src := range srcs {
vsname := src.ID().String() + "_vs.fxc"
// Calculate the hash of the source code to identify the Metal library.
// FNV is used as it is fast and the hash does not need to be secure.
h := fnv.New32()
_, _ = h.Write(src)
id := hex.EncodeToString(h.Sum(nil))
vsname := id + "_vs.fxc"
vs, err := fxcs.ReadFile("fxc/" + vsname)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
@ -48,7 +52,7 @@ func registerPrecompiledShaders() error {
return err
}
psname := src.ID().String() + "_ps.fxc"
psname := id + "_ps.fxc"
ps, err := fxcs.ReadFile("fxc/" + psname)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {

View File

@ -70,8 +70,8 @@ func (c *precompiledFXCs) get(hash shaderir.SourceHash) ([]byte, []byte) {
var thePrecompiledFXCs precompiledFXCs
func RegisterPrecompiledFXCs(hash shaderir.SourceHash, vertex, pixel []byte) {
thePrecompiledFXCs.put(hash, vertex, pixel)
func RegisterPrecompiledFXCs(kageSource []byte, vertex, pixel []byte) {
thePrecompiledFXCs.put(shaderir.CalcSourceHash(kageSource), vertex, pixel)
}
var vertexShaderCache = map[string]*_ID3DBlob{}

View File

@ -51,8 +51,8 @@ func (c *precompiledLibraries) get(hash shaderir.SourceHash) []byte {
var thePrecompiledLibraries precompiledLibraries
func RegisterPrecompiledLibrary(hash shaderir.SourceHash, bin []byte) {
thePrecompiledLibraries.put(hash, bin)
func RegisterPrecompiledLibrary(kageSource []byte, bin []byte) {
thePrecompiledLibraries.put(shaderir.CalcSourceHash(kageSource), bin)
}
type shaderRpsKey struct {

View File

@ -16,8 +16,6 @@ package shaderprecomp
import (
"github.com/hajimehoshi/ebiten/v2/internal/builtinshader"
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
)
// AppendBuildinShaderSources appends all the built-in shader sources to the given slice.
@ -25,46 +23,6 @@ import (
// Do not modify the content of the shader source.
//
// AppendBuildinShaderSources is concurrent-safe.
func AppendBuildinShaderSources(sources []*ShaderSource) []*ShaderSource {
for _, s := range builtinshader.AppendShaderSources(nil) {
src, err := NewShaderSource(s)
if err != nil {
panic(err)
}
sources = append(sources, src)
}
return sources
}
// ShaderSource is an object encapsulating a shader source code.
type ShaderSource struct {
source []byte
id ShaderSourceID
}
// NewShaderSource creates a new ShaderSource object from the given source code.
func NewShaderSource(source []byte) (*ShaderSource, error) {
hash, err := graphics.CalcSourceHash(source)
if err != nil {
return nil, err
}
return &ShaderSource{
source: source,
id: ShaderSourceID(hash),
}, nil
}
// ID returns a unique identifier for the shader source.
// The ShaderSourceID value must be the same for the same shader source and the same Ebitengine version.
// There is no guarantee that the ShaderSourceID value is the same between different Ebitengine versions.
func (s *ShaderSource) ID() ShaderSourceID {
return s.id
}
// ShaderSourceID is a uniuqe identifier for a shader source.
type ShaderSourceID [16]byte
// String returns a string representation of the shader source ID.
func (s ShaderSourceID) String() string {
return shaderir.SourceHash(s).String()
func AppendBuildinShaderSources(sources [][]byte) [][]byte {
return builtinshader.AppendShaderSources(sources)
}

View File

@ -21,15 +21,14 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir/msl"
)
// CompileToMSL compiles the shader source to Metal Shader Language, and writes the result to w.
//
// CompileToMSL is concurrent-safe.
func CompileToMSL(w io.Writer, source *ShaderSource) error {
ir, err := graphics.CompileShader(source.source)
func CompileToMSL(w io.Writer, kageSource []byte) error {
ir, err := graphics.CompileShader(kageSource)
if err != nil {
return err
}
@ -44,6 +43,6 @@ func CompileToMSL(w io.Writer, source *ShaderSource) error {
// For more details, see https://developer.apple.com/documentation/metal/shader_libraries/building_a_shader_library_by_precompiling_source_files.
//
// RegisterMetalLibrary is concurrent-safe.
func RegisterMetalLibrary(source *ShaderSource, library []byte) {
metal.RegisterPrecompiledLibrary(shaderir.SourceHash(source.ID()), library)
func RegisterMetalLibrary(kageSource []byte, library []byte) {
metal.RegisterPrecompiledLibrary(kageSource, library)
}

View File

@ -21,7 +21,6 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/directx"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir/hlsl"
)
@ -42,8 +41,8 @@ const (
// CompileToHLSL compiles the shader source to High-Level Shader Language to writers.
//
// CompileToHLSL is concurrent-safe.
func CompileToHLSL(vertexWriter, pixelWriter io.Writer, source *ShaderSource) error {
ir, err := graphics.CompileShader(source.source)
func CompileToHLSL(vertexWriter, pixelWriter io.Writer, kageSource []byte) error {
ir, err := graphics.CompileShader(kageSource)
if err != nil {
return err
}
@ -62,6 +61,6 @@ func CompileToHLSL(vertexWriter, pixelWriter io.Writer, source *ShaderSource) er
// For more details, see https://learn.microsoft.com/en-us/windows/win32/direct3dtools/dx-graphics-tools-fxc-using.
//
// RegisterFXCs is concurrent-safe.
func RegisterFXCs(source *ShaderSource, vertexFXC, pixelFXC []byte) {
directx.RegisterPrecompiledFXCs(shaderir.SourceHash(source.ID()), vertexFXC, pixelFXC)
func RegisterFXCs(kageSource []byte, vertexFXC, pixelFXC []byte) {
directx.RegisterPrecompiledFXCs(kageSource, vertexFXC, pixelFXC)
}