121 lines
4.4 KiB
Go
121 lines
4.4 KiB
Go
//go:build ignore
|
|||
|
|
|
||
|
|
// Gen compiles shader.hlsl and classes.hlsl to DXBC with the system's d3dcompiler_47.dll
|
||
|
|
// and writes shaders.go, so that apps need no shader compiler. It runs on
|
||
|
|
// Windows: go generate ./internal/gpu/d3d11
|
||
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"fmt"
|
||
|
|
"go/format"
|
||
|
|
"log"
|
||
|
|
"os"
|
||
|
|
"syscall"
|
||
|
|
"unsafe"
|
||
|
|
|
||
|
|
"github.com/egoist/mygo/internal/gpu"
|
||
|
|
)
|
||
|
|
|
||
|
|
// classSets are the classes that get shaders of their own, from those holding
|
||
|
|
// least: each class, what small batches join most (shadows with the fills
|
||
|
|
// casting them, fills with labels), and everything, for the rest.
|
||
|
|
var classSets = []uint32{
|
||
|
|
set(gpu.ClassPlain),
|
||
|
|
set(gpu.ClassImage),
|
||
|
|
set(gpu.ClassShadow),
|
||
|
|
set(gpu.ClassPlain, gpu.ClassShadow),
|
||
|
|
set(gpu.ClassGlyph),
|
||
|
|
set(gpu.ClassSubpixel),
|
||
|
|
set(gpu.ClassPlain, gpu.ClassGlyph),
|
||
|
|
set(gpu.ClassPlain, gpu.ClassSubpixel),
|
||
|
|
set(gpu.ClassFill),
|
||
|
|
set(gpu.ClassPlain, gpu.ClassFill, gpu.ClassShadow, gpu.ClassGlyph, gpu.ClassSubpixel, gpu.ClassImage),
|
||
|
|
}
|
||
|
|
|
||
|
|
func set(classes ...gpu.Class) (s uint32) {
|
||
|
|
for _, c := range classes {
|
||
|
|
s |= 1 << c
|
||
|
|
}
|
||
|
|
return s
|
||
|
|
}
|
||
|
|
|
||
|
|
// classDefines is that of shader.go, which this program cannot import, as it
|
||
|
|
// makes a file of its package.
|
||
|
|
func classDefines(cl uint32) string {
|
||
|
|
s := fmt.Sprintf("#define CLASSES %d\n", cl)
|
||
|
|
for _, c := range []struct {
|
||
|
|
name string
|
||
|
|
class gpu.Class
|
||
|
|
}{{"PLAIN", gpu.ClassPlain}, {"FILL", gpu.ClassFill}, {"SHADOW", gpu.ClassShadow}, {"GLYPH", gpu.ClassGlyph}, {"SUBPIXEL", gpu.ClassSubpixel}, {"IMAGE", gpu.ClassImage}} {
|
||
|
|
s += fmt.Sprintf("#define %s %d\n", c.name, cl>>c.class&1)
|
||
|
|
}
|
||
|
|
return s
|
||
|
|
}
|
||
|
|
|
||
|
|
func main() {
|
||
|
|
src, err := os.ReadFile("shader.hlsl")
|
||
|
|
if err != nil {
|
||
|
|
log.Fatal(err)
|
||
|
|
}
|
||
|
|
classes, err := os.ReadFile("classes.hlsl")
|
||
|
|
if err != nil {
|
||
|
|
log.Fatal(err)
|
||
|
|
}
|
||
|
|
compiler := syscall.NewLazyDLL("d3dcompiler_47.dll").NewProc("D3DCompile")
|
||
|
|
compile := func(src []byte, entry, target string) []byte {
|
||
|
|
var blob, errs uintptr
|
||
|
|
name, _ := syscall.BytePtrFromString("shader.hlsl")
|
||
|
|
e, _ := syscall.BytePtrFromString(entry)
|
||
|
|
t, _ := syscall.BytePtrFromString(target)
|
||
|
|
// IEEE strictness avoids the native optimizer's stack overflow on
|
||
|
|
// transformed coverage sampling while retaining level 3 optimization.
|
||
|
|
const optimize3IEEE = 1<<15 | 1<<13
|
||
|
|
hr, _, _ := compiler.Call(uintptr(unsafe.Pointer(&src[0])), uintptr(len(src)), uintptr(unsafe.Pointer(name)),
|
||
|
|
0, 0, uintptr(unsafe.Pointer(e)), uintptr(unsafe.Pointer(t)), optimize3IEEE, 0,
|
||
|
|
uintptr(unsafe.Pointer(&blob)), uintptr(unsafe.Pointer(&errs)))
|
||
|
|
if int32(hr) < 0 {
|
||
|
|
msg := "unknown error"
|
||
|
|
if errs != 0 {
|
||
|
|
msg = string(blobBytes(errs))
|
||
|
|
}
|
||
|
|
log.Fatalf("%s: %s", entry, msg)
|
||
|
|
}
|
||
|
|
return append([]byte(nil), blobBytes(blob)...)
|
||
|
|
}
|
||
|
|
var b bytes.Buffer
|
||
|
|
fmt.Fprintf(&b, "// Code generated by gen.go from shader.hlsl and classes.hlsl; DO NOT EDIT.\n\n//go:build windows\n\npackage d3d11\n\n")
|
||
|
|
fmt.Fprintf(&b, "// shaderSum is the SHA-256 of the shader.hlsl and the classes.hlsl\n// the shaders were compiled from.\nconst shaderSum = %q\n\n", gpu.SourceSum(string(src)+string(classes)))
|
||
|
|
for _, s := range []struct{ name, entry, target string }{{"vertexShader", "vs", "vs_4_0"}, {"pixelShader", "ps", "ps_4_0"},
|
||
|
|
{"passVertexShader", "passvs", "vs_4_0"}, {"downShader", "downps", "ps_4_0"}, {"blurShader", "blurps", "ps_4_0"}} {
|
||
|
|
b.WriteString(gpu.GoBytes(s.name, compile(src, s.entry, s.target)) + "\n")
|
||
|
|
}
|
||
|
|
b.WriteString("// classShaders are the shaders of classes.hlsl, from those holding least: a\n// batch draws with the first that holds its classes.\nvar classShaders = []classShader{\n")
|
||
|
|
var vars bytes.Buffer
|
||
|
|
for _, cl := range classSets {
|
||
|
|
full := append([]byte(classDefines(cl)+string(src)+"\n"), classes...)
|
||
|
|
fmt.Fprintf(&vars, "%s\n%s\n", gpu.GoBytes(fmt.Sprintf("classVS%d", cl), compile(full, "classvs", "vs_4_0")), gpu.GoBytes(fmt.Sprintf("classPS%d", cl), compile(full, "classps", "ps_4_0")))
|
||
|
|
fmt.Fprintf(&b, "\t{%d, classVS%d, classPS%d},\n", cl, cl, cl)
|
||
|
|
}
|
||
|
|
b.WriteString("}\n\n")
|
||
|
|
b.Write(vars.Bytes())
|
||
|
|
out, err := format.Source(b.Bytes())
|
||
|
|
if err != nil {
|
||
|
|
log.Fatal(err)
|
||
|
|
}
|
||
|
|
if err := os.WriteFile("shaders.go", out, 0o644); err != nil {
|
||
|
|
log.Fatal(err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// blobBytes returns the contents of an ID3DBlob.
|
||
|
|
func blobBytes(blob uintptr) []byte {
|
||
|
|
call := func(i int) uintptr {
|
||
|
|
vtbl := *(*uintptr)(unsafe.Pointer(blob))
|
||
|
|
fn := *(*uintptr)(unsafe.Pointer(vtbl + uintptr(i)*unsafe.Sizeof(uintptr(0))))
|
||
|
|
r, _, _ := syscall.SyscallN(fn, blob)
|
||
|
|
return r
|
||
|
|
}
|
||
|
|
p, n := call(3), call(4)
|
||
|
|
return unsafe.Slice((*byte)(unsafe.Pointer(p)), n)
|
||
|
|
}
|