~eliasnaur/gio

ref: 284659d3eac9 gio/internal/cmd/convertshaders/main.go -rw-r--r-- 14.3 KiB
284659d3Elias Naur gpu/shaders: [compute] remove unused command from kernel4 a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// SPDX-License-Identifier: Unlicense OR MIT

package main

import (
	"bytes"
	"encoding/json"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"sort"
	"strings"
	"text/template"

	"gioui.org/gpu/backend"
)

// This program generates shader variants for
// multiple GPU backends (OpenGL ES, Direct3D 11...)
// from a single source.

var (
	packageName   = flag.String("package", "", "specify Go package name")
	shadersDir    = flag.String("dir", "shaders", "specify shader directory")
	absShadersDir string
)

type shaderArgs struct {
	FetchColorExpr string
	Header         string
}

func main() {
	flag.Parse()
	if err := generate(); err != nil {
		fmt.Fprintf(os.Stderr, "generate: %v\n", err)
		os.Exit(1)
	}
}

func generate() error {
	tmp, err := ioutil.TempDir("", "shader-convert")
	if err != nil {
		return err
	}
	defer os.RemoveAll(tmp)
	glslcc, err := exec.LookPath("glslcc")
	if err != nil {
		return err
	}
	absShadersDir, err = filepath.Abs(*shadersDir)
	if err != nil {
		return err
	}
	shaders, err := filepath.Glob(filepath.Join(absShadersDir, "*"))
	if err != nil {
		return err
	}
	var out bytes.Buffer
	out.WriteString("// Code generated by build.go. DO NOT EDIT.\n\n")
	fmt.Fprintf(&out, "package %s\n\n", *packageName)
	fmt.Fprintf(&out, "import %q\n\n", "gioui.org/gpu/backend")

	out.WriteString("var (\n")

	for _, shader := range shaders {
		var err error
		switch filepath.Ext(shader) {
		case ".vert", ".frag":
			err = generateShader(glslcc, tmp, &out, shader)
		case ".comp":
			err = generateComputeShader(tmp, &out, shader)
		default:
			continue
		}
		if err != nil {
			return err
		}
	}
	out.WriteString(")")
	if err := ioutil.WriteFile("shaders.go", out.Bytes(), 0644); err != nil {
		return err
	}
	cmd := exec.Command("gofmt", "-s", "-w", "shaders.go")
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	return cmd.Run()
}

func generateComputeShader(tmp string, out io.Writer, shader string) error {
	glslangValidator, err := exec.LookPath("glslangValidator")
	if err != nil {
		return err
	}
	spirv_cross, err := exec.LookPath("spirv-cross")
	if err != nil {
		return err
	}
	var src backend.ShaderSources
	glsl310es, err := convertComputeShader(tmp, glslangValidator, spirv_cross, shader)
	if err != nil {
		return err
	}
	src.Name = filepath.Base(shader)
	src.GLSL310ES = glsl310es
	name := filepath.Base(shader)
	name = strings.ReplaceAll(name, ".", "_")
	fmt.Fprintf(out, "\tshader_%s = ", name)
	fmt.Fprintf(out, "backend.ShaderSources{\n")
	fmt.Fprintf(out, "Name: %#v,\n", src.Name)
	fmt.Fprintf(out, "GLSL310ES: %#v,\n", src.GLSL310ES)
	fmt.Fprintf(out, "}")
	fmt.Fprintf(out, "\n")
	return nil
}

func generateShader(glslcc, tmp string, out io.Writer, shader string) error {
	const nvariants = 3
	var variants [nvariants]struct {
		backend.ShaderSources
		hlslSrc string
	}
	args := [nvariants]shaderArgs{
		{
			FetchColorExpr: `_color`,
			Header:         `layout(binding=0) uniform Color { vec4 _color; };`,
		},
		{
			FetchColorExpr: `mix(_color1, _color2, clamp(vUV.x, 0.0, 1.0))`,
			Header:         `layout(binding=0) uniform Gradient { vec4 _color1; vec4 _color2; };`,
		},
		{
			FetchColorExpr: `texture(tex, vUV)`,
			Header:         `layout(binding=0) uniform sampler2D tex;`,
		},
	}
	for i := range args {
		// Ignore error; some shaders are not meant to run in GLSL 1.00.
		glsl100es, _, _ := convertShader(tmp, glslcc, shader, "gles", "100", &args[i], false)
		glsl300es, reflect, err := convertShader(tmp, glslcc, shader, "gles", "300", &args[i], false)
		if err != nil {
			return err
		}
		glsl130, _, err := convertShader(tmp, glslcc, shader, "glsl", "130", &args[i], false)
		if err != nil {
			return err
		}
		hlsl, _, err := convertShader(tmp, glslcc, shader, "hlsl", "40", &args[i], false)
		if err != nil {
			return err
		}
		if err := parseReflection(reflect, &variants[i].ShaderSources); err != nil {
			return err
		}
		var hlslProf string
		switch filepath.Ext(shader) {
		case ".frag":
			hlslProf = "ps"
		case ".vert":
			hlslProf = "vs"
		default:
			return fmt.Errorf("unrecognized shader type %s", shader)
		}
		var hlslc []byte
		hlslc, err = compileHLSL(tmp, shader, hlsl, "main", hlslProf+"_4_0_level_9_1")
		if err != nil {
			// Attempt shader model 4.0. Only the app/headless
			// test shaders use features not supported by level
			// 9.1.
			hlslc, err = compileHLSL(tmp, shader, hlsl, "main", hlslProf+"_4_0")
			if err != nil {
				return err
			}
		}
		// OpenGL 3.2 Core only accepts GLSL version 1.50, but is
		// otherwise compatible with version 1.30.
		glsl150 := strings.Replace(glsl130, "#version 130", "#version 150", 1)
		variants[i].Name = filepath.Base(shader)
		variants[i].GLSL100ES = glsl100es
		variants[i].GLSL300ES = glsl300es
		variants[i].GLSL130 = glsl130
		variants[i].GLSL150 = glsl150
		variants[i].hlslSrc = hlsl
		variants[i].HLSL = hlslc
	}
	name := filepath.Base(shader)
	name = strings.ReplaceAll(name, ".", "_")
	fmt.Fprintf(out, "\tshader_%s = ", name)
	// If the shader don't use the variant arguments, output
	// only a single version.
	multiVariant := variants[0].GLSL100ES != variants[1].GLSL100ES
	if multiVariant {
		fmt.Fprintf(out, "[...]backend.ShaderSources{\n")
	}
	for _, src := range variants {
		fmt.Fprintf(out, "backend.ShaderSources{\n")
		fmt.Fprintf(out, "Name: %#v,\n", src.Name)
		if len(src.Inputs) > 0 {
			fmt.Fprintf(out, "Inputs: %#v,\n", src.Inputs)
		}
		if u := src.Uniforms; len(u.Blocks) > 0 {
			fmt.Fprintf(out, "Uniforms: backend.UniformsReflection{\n")
			fmt.Fprintf(out, "Blocks: %#v,\n", u.Blocks)
			fmt.Fprintf(out, "Locations: %#v,\n", u.Locations)
			fmt.Fprintf(out, "Size: %d,\n", u.Size)
			fmt.Fprintf(out, "},\n")
		}
		if len(src.Textures) > 0 {
			fmt.Fprintf(out, "Textures: %#v,\n", src.Textures)
		}
		fmt.Fprintf(out, "GLSL100ES: %#v,\n", src.GLSL100ES)
		fmt.Fprintf(out, "GLSL300ES: %#v,\n", src.GLSL300ES)
		fmt.Fprintf(out, "GLSL130: %#v,\n", src.GLSL130)
		fmt.Fprintf(out, "GLSL150: %#v,\n", src.GLSL150)
		fmt.Fprintf(out, "/*\n%s\n*/\n", src.hlslSrc)
		fmt.Fprintf(out, "HLSL: %#v,\n", src.HLSL)
		fmt.Fprintf(out, "}")
		if multiVariant {
			fmt.Fprintf(out, ",")
		}
		fmt.Fprintf(out, "\n")
		if !multiVariant {
			break
		}
	}
	if multiVariant {
		fmt.Fprintf(out, "}\n")
	}
	return nil
}

func parseReflection(jsonData []byte, info *backend.ShaderSources) error {
	type InputReflection struct {
		ID            int    `json:"id"`
		Name          string `json:"name"`
		Location      int    `json:"location"`
		Semantic      string `json:"semantic"`
		SemanticIndex int    `json:"semantic_index"`
		Type          string `json:"type"`
	}
	type UniformMemberReflection struct {
		Name   string `json:"name"`
		Type   string `json:"type"`
		Offset int    `json:"offset"`
		Size   int    `json:"size"`
	}
	type UniformBufferReflection struct {
		ID      int                       `json:"id"`
		Name    string                    `json:"name"`
		Set     int                       `json:"set"`
		Binding int                       `json:"binding"`
		Size    int                       `json:"block_size"`
		Members []UniformMemberReflection `json:"members"`
	}
	type TextureReflection struct {
		ID        int    `json:"id"`
		Name      string `json:"name"`
		Set       int    `json:"set"`
		Binding   int    `json:"binding"`
		Dimension string `json:"dimension"`
		Format    string `json:"format"`
	}
	type StorageBufferReflection struct {
		ID        int    `json:"id"`
		Name      string `json:"name"`
		Set       int    `json:"set"`
		Binding   int    `json:"binding"`
		BlockSize int    `json:"block_size"`
	}
	type shaderReflection struct {
		Inputs         []InputReflection         `json:"inputs"`
		UniformBuffers []UniformBufferReflection `json:"uniform_buffers"`
		Textures       []TextureReflection       `json:"textures"`
		StorageBuffers []StorageBufferReflection `json:"storage_buffers"`
	}
	type shaderMetadata struct {
		VS shaderReflection `json:"vs"`
		FS shaderReflection `json:"fs"`
		CS shaderReflection `json:"cs"`
	}
	var reflect shaderMetadata
	if err := json.Unmarshal(jsonData, &reflect); err != nil {
		return fmt.Errorf("parseReflection: %v", err)
	}
	inputRef := reflect.VS.Inputs
	for _, input := range inputRef {
		dataType, dataSize, err := parseDataType(input.Type)
		if err != nil {
			return fmt.Errorf("parseReflection: %v", err)
		}
		info.Inputs = append(info.Inputs, backend.InputLocation{
			Name:          input.Name,
			Location:      input.Location,
			Semantic:      input.Semantic,
			SemanticIndex: input.SemanticIndex,
			Type:          dataType,
			Size:          dataSize,
		})
	}
	sort.Slice(info.Inputs, func(i, j int) bool {
		return info.Inputs[i].Location < info.Inputs[j].Location
	})
	shaderBlocks := reflect.VS.UniformBuffers
	if len(shaderBlocks) == 0 {
		shaderBlocks = reflect.FS.UniformBuffers
	}
	blockOffset := 0
	for _, block := range shaderBlocks {
		info.Uniforms.Blocks = append(info.Uniforms.Blocks, backend.UniformBlock{
			Name:    block.Name,
			Binding: block.Binding,
		})
		for _, member := range block.Members {
			dataType, size, err := parseDataType(member.Type)
			if err != nil {
				return fmt.Errorf("parseReflection: %v", err)
			}
			info.Uniforms.Locations = append(info.Uniforms.Locations, backend.UniformLocation{
				// Synthetic name generated by glslcc.
				Name:   fmt.Sprintf("_%d.%s", block.ID, member.Name),
				Type:   dataType,
				Size:   size,
				Offset: blockOffset + member.Offset,
			})
		}
		blockOffset += block.Size
	}
	info.Uniforms.Size = blockOffset
	textures := reflect.VS.Textures
	if len(textures) == 0 {
		textures = reflect.FS.Textures
	}
	for _, texture := range textures {
		info.Textures = append(info.Textures, backend.TextureBinding{
			Name:    texture.Name,
			Binding: texture.Binding,
		})
	}
	for _, sb := range reflect.CS.StorageBuffers {
		info.StorageBuffers = append(info.StorageBuffers, backend.StorageBufferBinding{
			Binding:   sb.Binding,
			BlockSize: sb.BlockSize,
		})
	}
	return nil
}

func parseDataType(t string) (backend.DataType, int, error) {
	switch t {
	case "float":
		return backend.DataTypeFloat, 1, nil
	case "float2":
		return backend.DataTypeFloat, 2, nil
	case "float3":
		return backend.DataTypeFloat, 3, nil
	case "float4":
		return backend.DataTypeFloat, 4, nil
	case "int":
		return backend.DataTypeInt, 1, nil
	case "int2":
		return backend.DataTypeInt, 2, nil
	case "int3":
		return backend.DataTypeInt, 3, nil
	case "int4":
		return backend.DataTypeInt, 4, nil
	default:
		return 0, 0, fmt.Errorf("unsupported input data type: %s", t)
	}
}

func compileHLSL(tmp, path, src, entry, profile string) ([]byte, error) {
	base := filepath.Base(path)
	tmppath := filepath.Join(tmp, base)
	defer os.Remove(tmppath)
	if err := ioutil.WriteFile(tmppath, []byte(src), 0644); err != nil {
		return nil, err
	}
	outfile := filepath.Join(tmp, base+".obj")
	defer os.Remove(outfile)
	fxcInput := tmppath
	fxcOutput := outfile
	var fxc *exec.Cmd
	if runtime.GOOS == "windows" {
		fxc = exec.Command("fxc.exe")
	} else {
		// Convert paths to wine Windows format.
		var err error
		fxcInput, err = windowsPath(fxcInput)
		if err != nil {
			return nil, err
		}
		fxcOutput, err = windowsPath(fxcOutput)
		if err != nil {
			return nil, err
		}
		fxc = exec.Command("wine", "fxc.exe")
	}
	var stdout, stderr bytes.Buffer
	fxc.Stderr = &stderr
	fxc.Stdout = &stdout
	fxc.Args = append(fxc.Args, "/Fo", fxcOutput, "/T", profile, "/E", entry, fxcInput)
	if err := fxc.Run(); err != nil {
		info := ""
		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%s\n%s%s: %v", stderr.Bytes(), stdout.Bytes(), info, fxc, err)
	}
	return ioutil.ReadFile(outfile)
}

// windowsPath uses the winepath tool to convert a path to Windows format. The returned
// path can be used as arguments for Windows command line tools.
func windowsPath(path string) (string, error) {
	var out bytes.Buffer
	winepath := exec.Command("winepath", "--windows", path)
	winepath.Stdout = &out
	if err := winepath.Run(); err != nil {
		return "", err
	}
	return strings.TrimSpace(out.String()), nil
}

func convertShader(tmp, glslcc, path, lang, profile string, args *shaderArgs, flattenUBOs bool) (string, []byte, error) {
	shaderTmpl, err := template.ParseFiles(path)
	if err != nil {
		return "", nil, err
	}
	var buf bytes.Buffer
	if err := shaderTmpl.Execute(&buf, args); err != nil {
		return "", nil, err
	}
	tmppath := filepath.Join(tmp, filepath.Base(path))
	defer os.Remove(tmppath)
	if err := ioutil.WriteFile(tmppath, buf.Bytes(), 0644); err != nil {
		return "", nil, err
	}
	var progFlag string
	var progSuffix string
	switch filepath.Ext(path) {
	case ".vert":
		progFlag = "--vert"
		progSuffix = "vs"
	case ".frag":
		progFlag = "--frag"
		progSuffix = "fs"
	case ".comp":
		progFlag = "--compute"
		progSuffix = "cs"
	default:
		return "", nil, fmt.Errorf("unrecognized shader type: %s", path)
	}
	cmd := exec.Command(glslcc,
		"--silent",
		"--optimize",
		"--include-dirs", absShadersDir,
		"--reflect",
		"--output", filepath.Join(tmp, "shader"),
		"--lang", lang,
		"--profile", profile,
		progFlag, tmppath,
	)
	if lang == "hlsl" {
		cmd.Args = append(cmd.Args, "--defines=HLSL")
	}
	if flattenUBOs {
		cmd.Args = append(cmd.Args, "--flatten-ubos")
	}
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	if err := cmd.Run(); err != nil {
		return "", nil, fmt.Errorf("%s: %v", strings.Join(cmd.Args, " "), err)
	}
	f, err := os.Open(filepath.Join(tmp, "shader_"+progSuffix))
	if err != nil {
		return "", nil, err
	}
	defer f.Close()
	defer os.Remove(f.Name())
	src, err := ioutil.ReadAll(f)
	if err != nil {
		return "", nil, err
	}
	reflect, err := ioutil.ReadFile(filepath.Join(tmp, "shader_"+progSuffix+".json"))
	if err != nil {
		return "", nil, err
	}
	return string(src), reflect, nil
}

func convertComputeShader(tmp, glslangValidator, spirv_cross, path string) (string, error) {
	spvFile := filepath.Join(tmp, "shader.spv")
	cmd := exec.Command(glslangValidator,
		"-G100", // OpenGL ES 3.1.
		"-w",    // Suppress warnings.
		"-o", spvFile,
		path,
	)
	defer os.Remove(spvFile)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	if err := cmd.Run(); err != nil {
		return "", fmt.Errorf("%s: %v", strings.Join(cmd.Args, " "), err)
	}
	cmd = exec.Command(spirv_cross,
		"--es",
		"--version", "310",
		spvFile,
	)
	var shaderDump bytes.Buffer
	cmd.Stdout = &shaderDump
	cmd.Stderr = os.Stderr
	if err := cmd.Run(); err != nil {
		return "", fmt.Errorf("%s: %v", strings.Join(cmd.Args, " "), err)
	}
	return shaderDump.String(), nil
}