~eliasnaur/gio

gio/gpu/gl/util.go -rw-r--r-- 2.1 KiB View raw
d489c20bElias Naur io/pointer: update documentation examples 12 hours 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
// SPDX-License-Identifier: Unlicense OR MIT

package gl

import (
	"errors"
	"fmt"
	"strings"
)

func CreateProgram(ctx Functions, vsSrc, fsSrc string, attribs []string) (Program, error) {
2 references
vs, err := createShader(ctx, VERTEX_SHADER, vsSrc) if err != nil { return Program{}, err } defer ctx.DeleteShader(vs) fs, err := createShader(ctx, FRAGMENT_SHADER, fsSrc) if err != nil { return Program{}, err } defer ctx.DeleteShader(fs) prog := ctx.CreateProgram() if !prog.valid() { return Program{}, errors.New("glCreateProgram failed") } ctx.AttachShader(prog, vs) ctx.AttachShader(prog, fs) for i, a := range attribs { ctx.BindAttribLocation(prog, Attrib(i), a) } ctx.LinkProgram(prog) if ctx.GetProgrami(prog, LINK_STATUS) == 0 { log := ctx.GetProgramInfoLog(prog) ctx.DeleteProgram(prog) return Program{}, fmt.Errorf("program link failed: %s", strings.TrimSpace(log)) } return prog, nil } func GetUniformLocation(ctx Functions, prog Program, name string) Uniform {
2 references
loc := ctx.GetUniformLocation(prog, name) if !loc.valid() { panic(fmt.Errorf("uniform %s not found", name)) } return loc } func createShader(ctx Functions, typ Enum, src string) (Shader, error) {
2 references
sh := ctx.CreateShader(typ) if !sh.valid() { return Shader{}, errors.New("glCreateShader failed") } ctx.ShaderSource(sh, src) ctx.CompileShader(sh) if ctx.GetShaderi(sh, COMPILE_STATUS) == 0 { log := ctx.GetShaderInfoLog(sh) ctx.DeleteShader(sh) return Shader{}, fmt.Errorf("shader compilation failed: %s", strings.TrimSpace(log)) } return sh, nil } func ParseGLVersion(glVer string) (version [2]int, gles bool, err error) {
2 references
var ver [2]int if _, err := fmt.Sscanf(glVer, "OpenGL ES %d.%d", &ver[0], &ver[1]); err == nil { return ver, true, nil } else if _, err := fmt.Sscanf(glVer, "WebGL %d.%d", &ver[0], &ver[1]); err == nil { // WebGL major version v corresponds to OpenGL ES version v + 1 ver[0]++ return ver, true, nil } else if _, err := fmt.Sscanf(glVer, "%d.%d", &ver[0], &ver[1]); err == nil { return ver, false, nil } return ver, false, fmt.Errorf("failed to parse OpenGL ES version (%s)", glVer) }