| 1 | // Copyright 2014 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | //go:build darwin || linux || windows |
| 6 | |
| 7 | package glutilscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/gl/glutil`/ |
| 8 | |
| 9 | import ( |
| 10 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 11 | |
| 12 | "golang.org/x/mobile/exp/f32scip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/f32`/" |
| 13 | "golang.org/x/mobile/glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/" |
| 14 | ) |
| 15 | |
| 16 | // CreateProgram creates, compiles, and links a gl.Program. |
| 17 | func CreateProgramscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/gl/glutil`/CreateProgram().(glctx glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.Contextscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#, vertexSrc, fragmentSrc string) (glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.Programscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Program#, error) { |
| 18 | program := glctx.CreateProgramscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#CreateProgram.() |
| 19 | if program.Valuescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Program#Value. == 0 { |
| 20 | return glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.Programscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Program#{}, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("glutil: no programs available") |
| 21 | } |
| 22 | |
| 23 | vertexShader, err := loadShaderscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/gl/glutil`/loadShader().(glctx, glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.VERTEX_SHADERscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/VERTEX_SHADER., vertexSrc) |
| 24 | if err != nil { |
| 25 | return glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.Programscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Program#{}, err |
| 26 | } |
| 27 | fragmentShader, err := loadShaderscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/gl/glutil`/loadShader().(glctx, glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.FRAGMENT_SHADERscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/FRAGMENT_SHADER., fragmentSrc) |
| 28 | if err != nil { |
| 29 | glctx.DeleteShaderscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#DeleteShader.(vertexShader) |
| 30 | return glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.Programscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Program#{}, err |
| 31 | } |
| 32 | |
| 33 | glctx.AttachShaderscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#AttachShader.(program, vertexShader) |
| 34 | glctx.AttachShaderscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#AttachShader.(program, fragmentShader) |
| 35 | glctx.LinkProgramscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#LinkProgram.(program) |
| 36 | |
| 37 | // Flag shaders for deletion when program is unlinked. |
| 38 | glctx.DeleteShaderscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#DeleteShader.(vertexShader) |
| 39 | glctx.DeleteShaderscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#DeleteShader.(fragmentShader) |
| 40 | |
| 41 | if glctx.GetProgramiscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#GetProgrami.(program, glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.LINK_STATUSscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/LINK_STATUS.) == 0 { |
| 42 | defer glctx.DeleteProgramscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#DeleteProgram.(program) |
| 43 | return glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.Programscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Program#{}, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("glutil: %s", glctx.GetProgramInfoLogscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#GetProgramInfoLog.(program)) |
| 44 | } |
| 45 | return program, nil |
| 46 | } |
| 47 | |
| 48 | func loadShaderscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/gl/glutil`/loadShader().(glctx glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.Contextscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#, shaderType glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.Enumscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Enum#, src string) (glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.Shaderscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Shader#, error) { |
| 49 | shader := glctx.CreateShaderscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#CreateShader.(shaderType) |
| 50 | if shader.Valuescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Shader#Value. == 0 { |
| 51 | return glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.Shaderscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Shader#{}, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("glutil: could not create shader (type %v)", shaderType) |
| 52 | } |
| 53 | glctx.ShaderSourcescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#ShaderSource.(shader, src) |
| 54 | glctx.CompileShaderscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#CompileShader.(shader) |
| 55 | if glctx.GetShaderiscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#GetShaderi.(shader, glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.COMPILE_STATUSscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/COMPILE_STATUS.) == 0 { |
| 56 | defer glctx.DeleteShaderscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#DeleteShader.(shader) |
| 57 | return glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.Shaderscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Shader#{}, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("shader compile: %s", glctx.GetShaderInfoLogscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#GetShaderInfoLog.(shader)) |
| 58 | } |
| 59 | return shader, nil |
| 60 | } |
| 61 | |
| 62 | // writeAffine writes the contents of an Affine to a 3x3 matrix GL uniform. |
| 63 | func writeAffinescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/gl/glutil`/writeAffine().(glctx glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.Contextscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#, u glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.Uniformscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Uniform#, a *f32scip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/f32`/.Affinescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/f32`/Affine#) { |
| 64 | var m [9]float32 |
| 65 | m[0*3+0] = a[0][0] |
| 66 | m[0*3+1] = a[1][0] |
| 67 | m[0*3+2] = 0 |
| 68 | m[1*3+0] = a[0][1] |
| 69 | m[1*3+1] = a[1][1] |
| 70 | m[1*3+2] = 0 |
| 71 | m[2*3+0] = a[0][2] |
| 72 | m[2*3+1] = a[1][2] |
| 73 | m[2*3+2] = 1 |
| 74 | glctx.UniformMatrix3fvscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#UniformMatrix3fv.(u, m[:]) |
| 75 | } |