mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-03 16:35:36 +00:00
gpu/internal/driver: use strings for generated DXIL assembly
Literal strings are a more compact than literal byte slices. A future change will switch to go:embed to save even more space. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -20,21 +20,21 @@ type FXC struct {
|
||||
func NewFXC() *FXC { return &FXC{Bin: "fxc.exe"} }
|
||||
|
||||
// Compile compiles the input shader.
|
||||
func (fxc *FXC) Compile(path, variant string, input []byte, entryPoint string, profileVersion string) ([]byte, error) {
|
||||
func (fxc *FXC) Compile(path, variant string, input []byte, entryPoint string, profileVersion string) (string, error) {
|
||||
base := fxc.WorkDir.Path(filepath.Base(path), variant, profileVersion)
|
||||
pathin := base + ".in"
|
||||
pathout := base + ".out"
|
||||
result := pathout
|
||||
|
||||
if err := fxc.WorkDir.WriteFile(pathin, input); err != nil {
|
||||
return nil, fmt.Errorf("unable to write shader to disk: %w", err)
|
||||
return "", fmt.Errorf("unable to write shader to disk: %w", err)
|
||||
}
|
||||
|
||||
cmd := exec.Command(fxc.Bin)
|
||||
if runtime.GOOS != "windows" {
|
||||
cmd = exec.Command("wine", fxc.Bin)
|
||||
if err := winepath(&pathin, &pathout); err != nil {
|
||||
return nil, err
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ func (fxc *FXC) Compile(path, variant string, input []byte, entryPoint string, p
|
||||
case ".vert":
|
||||
profile = "vs_" + profileVersion
|
||||
default:
|
||||
return nil, fmt.Errorf("unrecognized shader type %s", path)
|
||||
return "", fmt.Errorf("unrecognized shader type %s", path)
|
||||
}
|
||||
|
||||
cmd.Args = append(cmd.Args,
|
||||
@@ -61,15 +61,15 @@ func (fxc *FXC) Compile(path, variant string, input []byte, entryPoint string, p
|
||||
if runtime.GOOS != "windows" {
|
||||
info = "If the fxc tool cannot be found, set WINEPATH to the Windows path for the Windows SDK.\n"
|
||||
}
|
||||
return nil, fmt.Errorf("%s\n%sfailed to run %v: %w", output, info, cmd.Args, err)
|
||||
return "", fmt.Errorf("%s\n%sfailed to run %v: %w", output, info, cmd.Args, err)
|
||||
}
|
||||
|
||||
compiled, err := ioutil.ReadFile(result)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to read output %q: %w", pathout, err)
|
||||
return "", fmt.Errorf("unable to read output %q: %w", pathout, err)
|
||||
}
|
||||
|
||||
return compiled, nil
|
||||
return string(compiled), nil
|
||||
}
|
||||
|
||||
// DXC is hlsl compiler that targets ShaderModel 6.0 and newer.
|
||||
@@ -81,21 +81,21 @@ type DXC struct {
|
||||
func NewDXC() *DXC { return &DXC{Bin: "dxc.exe"} }
|
||||
|
||||
// Compile compiles the input shader.
|
||||
func (dxc *DXC) Compile(path, variant string, input []byte, entryPoint string, profile string) ([]byte, error) {
|
||||
func (dxc *DXC) Compile(path, variant string, input []byte, entryPoint string, profile string) (string, error) {
|
||||
base := dxc.WorkDir.Path(filepath.Base(path), variant, profile)
|
||||
pathin := base + ".in"
|
||||
pathout := base + ".out"
|
||||
result := pathout
|
||||
|
||||
if err := dxc.WorkDir.WriteFile(pathin, input); err != nil {
|
||||
return nil, fmt.Errorf("unable to write shader to disk: %w", err)
|
||||
return "", fmt.Errorf("unable to write shader to disk: %w", err)
|
||||
}
|
||||
|
||||
cmd := exec.Command(dxc.Bin)
|
||||
if runtime.GOOS != "windows" {
|
||||
cmd = exec.Command("wine", dxc.Bin)
|
||||
if err := winepath(&pathin, &pathout); err != nil {
|
||||
return nil, err
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,15 +113,15 @@ func (dxc *DXC) Compile(path, variant string, input []byte, entryPoint string, p
|
||||
if runtime.GOOS != "windows" {
|
||||
info = "If the dxc tool cannot be found, set WINEPATH to the Windows path for the Windows SDK.\n"
|
||||
}
|
||||
return nil, fmt.Errorf("%s\n%sfailed to run %v: %w", output, info, cmd.Args, err)
|
||||
return "", fmt.Errorf("%s\n%sfailed to run %v: %w", output, info, cmd.Args, err)
|
||||
}
|
||||
|
||||
compiled, err := ioutil.ReadFile(result)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to read output %q: %w", pathout, err)
|
||||
return "", fmt.Errorf("unable to read output %q: %w", pathout, err)
|
||||
}
|
||||
|
||||
return compiled, nil
|
||||
return string(compiled), nil
|
||||
}
|
||||
|
||||
// winepath uses the winepath tool to convert a paths to Windows format.
|
||||
|
||||
@@ -233,7 +233,7 @@ func (conv *Converter) Run(out io.Writer) error {
|
||||
fmt.Fprintf(out, "GLSL150: `%s`,\n", src.GLSL150)
|
||||
}
|
||||
if len(src.HLSL) > 0 {
|
||||
fmt.Fprintf(out, "HLSL: %#v,\n", src.HLSL)
|
||||
fmt.Fprintf(out, "HLSL: %q,\n", src.HLSL)
|
||||
}
|
||||
fmt.Fprintf(out, "}")
|
||||
if multiVariant {
|
||||
|
||||
@@ -345,7 +345,7 @@ func (b *Backend) NewInputLayout(vertexShader driver.ShaderSources, layout []dri
|
||||
AlignedByteOffset: uint32(l.Offset),
|
||||
}
|
||||
}
|
||||
l, err := b.dev.CreateInputLayout(descs, vertexShader.HLSL)
|
||||
l, err := b.dev.CreateInputLayout(descs, []byte(vertexShader.HLSL))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -398,11 +398,11 @@ func (b *Backend) NewComputeProgram(shader driver.ShaderSources) (driver.Program
|
||||
}
|
||||
|
||||
func (b *Backend) NewProgram(vertexShader, fragmentShader driver.ShaderSources) (driver.Program, error) {
|
||||
vs, err := b.dev.CreateVertexShader(vertexShader.HLSL)
|
||||
vs, err := b.dev.CreateVertexShader([]byte(vertexShader.HLSL))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ps, err := b.dev.CreatePixelShader(fragmentShader.HLSL)
|
||||
ps, err := b.dev.CreatePixelShader([]byte(fragmentShader.HLSL))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ type ShaderSources struct {
|
||||
GLSL310ES string
|
||||
GLSL130 string
|
||||
GLSL150 string
|
||||
HLSL []byte
|
||||
HLSL string
|
||||
Uniforms UniformsReflection
|
||||
Inputs []InputLocation
|
||||
Textures []TextureBinding
|
||||
|
||||
Reference in New Issue
Block a user