-
Notifications
You must be signed in to change notification settings - Fork 327
/
stub_program.go
278 lines (262 loc) · 8.58 KB
/
stub_program.go
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
// Copyright (C) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gles
import (
"context"
"fmt"
"sort"
"strings"
"github.com/google/gapid/core/log"
"github.com/google/gapid/gapis/api"
)
var (
// We don't include tests directly in the gles package as it adds
// signaficantly to the test build time.
VisibleForTestingStubShaderSource = stubShaderSource
)
func buildStubProgram(ctx context.Context, thread uint64, e *api.CmdExtras, s *api.GlobalState, programID ProgramId) []api.Cmd {
programInfo := FindLinkProgramExtra(s.Arena, e).Clone(s.Arena, api.CloneContext{})
vss, fss, err := stubShaderSource(programInfo)
if err != nil {
log.E(ctx, "Unable to build stub shader: %v", err)
}
c := GetContext(s, thread)
vertexShaderID := ShaderId(newUnusedID(ctx, 'S', func(x uint32) bool {
ok := c.Objects().Buffers().Contains(BufferId(x))
return ok
}))
fragmentShaderID := ShaderId(newUnusedID(ctx, 'S', func(x uint32) bool {
ok := c.Objects().Buffers().Contains(BufferId(x))
return ok || x == uint32(vertexShaderID)
}))
cb := CommandBuilder{Thread: thread, Arena: s.Arena}
glLinkProgram := cb.GlLinkProgram(programID)
glLinkProgram.Extras().Add(programInfo)
return append(
CompileProgram(ctx, s, cb, vertexShaderID, fragmentShaderID, programID, vss, fss),
glLinkProgram,
)
}
func stubShaderSource(pi LinkProgramExtraʳ) (vertexShaderSource, fragmentShaderSource string, err error) {
vsDecls, fsDecls := []string{}, []string{}
vsTickles, fsTickles := []string{}, []string{}
if !pi.IsNil() && !pi.ActiveResources().IsNil() {
for _, u := range pi.ActiveResources().DefaultUniformBlock().All() {
var decls, tickles *[]string
if isSampler(u.Type()) {
decls, tickles = &fsDecls, &fsTickles
} else {
decls, tickles = &vsDecls, &vsTickles
}
ty, err := glslTypeFor(u.Type())
if err != nil {
return "", "", err
}
if u.ArraySize() > 1 {
name := strings.TrimRight(u.Name(), "[0]")
*decls = append(*decls, fmt.Sprintf("uniform %s %s[%d];\n", ty, name, u.ArraySize()))
for i := GLint(0); i < u.ArraySize(); i++ {
tkl, err := glslTickle(u.Type(), fmt.Sprintf("%s[%d]", name, i))
if err != nil {
return "", "", err
}
*tickles = append(*tickles, fmt.Sprintf("no_strip += %s;\n ", tkl))
}
} else {
*decls = append(*decls, fmt.Sprintf("uniform %s %s;\n", ty, u.Name()))
tkl, err := glslTickle(u.Type(), u.Name())
if err != nil {
return "", "", err
}
*tickles = append(*tickles, fmt.Sprintf("no_strip += %s;\n ", tkl))
}
}
// Deterministic output FTW!
sort.Strings(vsDecls)
sort.Strings(fsDecls)
sort.Strings(vsTickles)
sort.Strings(fsTickles)
}
return fmt.Sprintf(`#version 150
/////////////////////////////////////////////
// GAPID stub shader (no source available) //
/////////////////////////////////////////////
precision highp float;
%svoid main() {
float no_strip = 0.0;
%sgl_Position = vec4(no_strip * 0.000001, 0., 0., 1.);
}`, strings.Join(vsDecls, ""), strings.Join(vsTickles, "")),
fmt.Sprintf(`#version 150
/////////////////////////////////////////////
// GAPID stub shader (no source available) //
/////////////////////////////////////////////
precision highp float;
%svoid main() {
float no_strip = 0.0;
%sgl_FragColor = vec4(1., no_strip * 0.000001, 1., 1.);
}`, strings.Join(fsDecls, ""), strings.Join(fsTickles, "")), nil
}
func glslTypeFor(ty GLenum) (string, error) {
switch ty {
case GLenum_GL_FLOAT:
return "float", nil
case GLenum_GL_FLOAT_VEC2:
return "vec2", nil
case GLenum_GL_FLOAT_VEC3:
return "vec3", nil
case GLenum_GL_FLOAT_VEC4:
return "vec4", nil
case GLenum_GL_INT:
return "int", nil
case GLenum_GL_INT_VEC2:
return "ivec2", nil
case GLenum_GL_INT_VEC3:
return "ivec3", nil
case GLenum_GL_INT_VEC4:
return "ivec4", nil
case GLenum_GL_UNSIGNED_INT:
return "unsigned int", nil
case GLenum_GL_UNSIGNED_INT_VEC2:
return "uvec2", nil
case GLenum_GL_UNSIGNED_INT_VEC3:
return "uvec3", nil
case GLenum_GL_UNSIGNED_INT_VEC4:
return "uvec4", nil
case GLenum_GL_BOOL:
return "bool", nil
case GLenum_GL_BOOL_VEC2:
return "bvec2", nil
case GLenum_GL_BOOL_VEC3:
return "bvec3", nil
case GLenum_GL_BOOL_VEC4:
return "bvec4", nil
case GLenum_GL_FLOAT_MAT2:
return "mat2", nil
case GLenum_GL_FLOAT_MAT3:
return "mat3", nil
case GLenum_GL_FLOAT_MAT4:
return "mat4", nil
case GLenum_GL_FLOAT_MAT2x3:
return "mat2x3", nil
case GLenum_GL_FLOAT_MAT2x4:
return "mat2x4", nil
case GLenum_GL_FLOAT_MAT3x2:
return "mat3x2", nil
case GLenum_GL_FLOAT_MAT3x4:
return "mat3x4", nil
case GLenum_GL_FLOAT_MAT4x2:
return "mat4x2", nil
case GLenum_GL_FLOAT_MAT4x3:
return "mat4x3", nil
case GLenum_GL_SAMPLER_2D:
return "sampler2D", nil
case GLenum_GL_SAMPLER_3D:
return "sampler3D", nil
case GLenum_GL_SAMPLER_CUBE:
return "samplerCube", nil
case GLenum_GL_SAMPLER_2D_SHADOW:
return "sampler2DShadow", nil
case GLenum_GL_SAMPLER_2D_ARRAY:
return "sampler2DArray", nil
case GLenum_GL_SAMPLER_2D_ARRAY_SHADOW:
return "sampler2DArrayShadow", nil
case GLenum_GL_SAMPLER_CUBE_SHADOW:
return "samplerCubeShadow", nil
case GLenum_GL_INT_SAMPLER_2D:
return "isampler2D", nil
case GLenum_GL_INT_SAMPLER_3D:
return "isampler3D", nil
case GLenum_GL_INT_SAMPLER_CUBE:
return "isamplerCube", nil
case GLenum_GL_INT_SAMPLER_2D_ARRAY:
return "isampler2DArray", nil
case GLenum_GL_UNSIGNED_INT_SAMPLER_2D:
return "usampler2D", nil
case GLenum_GL_UNSIGNED_INT_SAMPLER_3D:
return "usampler3D", nil
case GLenum_GL_UNSIGNED_INT_SAMPLER_CUBE:
return "usamplerCube", nil
case GLenum_GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
return "usampler2DArray", nil
default:
return "", fmt.Errorf("Unknown uniform type %s", ty)
}
}
func isSampler(ty GLenum) bool {
switch ty {
case GLenum_GL_SAMPLER_2D,
GLenum_GL_SAMPLER_3D,
GLenum_GL_SAMPLER_CUBE,
GLenum_GL_SAMPLER_2D_SHADOW,
GLenum_GL_SAMPLER_2D_ARRAY,
GLenum_GL_SAMPLER_2D_ARRAY_SHADOW,
GLenum_GL_SAMPLER_CUBE_SHADOW,
GLenum_GL_INT_SAMPLER_2D,
GLenum_GL_INT_SAMPLER_3D,
GLenum_GL_INT_SAMPLER_CUBE,
GLenum_GL_INT_SAMPLER_2D_ARRAY,
GLenum_GL_UNSIGNED_INT_SAMPLER_2D,
GLenum_GL_UNSIGNED_INT_SAMPLER_3D,
GLenum_GL_UNSIGNED_INT_SAMPLER_CUBE,
GLenum_GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
return true
default:
return false
}
}
func glslTickle(ty GLenum, name string) (string, error) {
switch ty {
case GLenum_GL_FLOAT:
return name, nil
case GLenum_GL_FLOAT_VEC2, GLenum_GL_FLOAT_VEC3, GLenum_GL_FLOAT_VEC4:
return fmt.Sprintf("%s.x", name), nil
case GLenum_GL_INT, GLenum_GL_UNSIGNED_INT:
return fmt.Sprintf("float(%s)", name), nil
case GLenum_GL_INT_VEC2, GLenum_GL_INT_VEC3, GLenum_GL_INT_VEC4,
GLenum_GL_UNSIGNED_INT_VEC2, GLenum_GL_UNSIGNED_INT_VEC3, GLenum_GL_UNSIGNED_INT_VEC4:
return fmt.Sprintf("float(%s.x)", name), nil
case GLenum_GL_BOOL:
return fmt.Sprintf("(%s?1.:0.)", name), nil
case GLenum_GL_BOOL_VEC2, GLenum_GL_BOOL_VEC3, GLenum_GL_BOOL_VEC4:
return fmt.Sprintf("(%s?1.:0.)", name), nil
case GLenum_GL_FLOAT_MAT2, GLenum_GL_FLOAT_MAT3, GLenum_GL_FLOAT_MAT4,
GLenum_GL_FLOAT_MAT2x3, GLenum_GL_FLOAT_MAT2x4,
GLenum_GL_FLOAT_MAT3x2, GLenum_GL_FLOAT_MAT3x4,
GLenum_GL_FLOAT_MAT4x2, GLenum_GL_FLOAT_MAT4x3:
return fmt.Sprintf("%s[0].x", name), nil
case GLenum_GL_SAMPLER_2D:
return fmt.Sprintf("texture2D(%s, vec2(0.)).x", name), nil
case GLenum_GL_SAMPLER_3D:
return fmt.Sprintf("texture3D(%s, vec3(0.)).x", name), nil
case GLenum_GL_SAMPLER_CUBE:
return fmt.Sprintf("textureCube(%s, vec3(0.)).x", name), nil
// The below types do not exist in GLSL 110.
case GLenum_GL_SAMPLER_2D_SHADOW,
GLenum_GL_SAMPLER_2D_ARRAY_SHADOW,
GLenum_GL_SAMPLER_CUBE_SHADOW:
return fmt.Sprintf("texture(%s, vec3(0.))", name), nil
case GLenum_GL_SAMPLER_2D_ARRAY,
GLenum_GL_INT_SAMPLER_2D,
GLenum_GL_INT_SAMPLER_3D,
GLenum_GL_INT_SAMPLER_CUBE,
GLenum_GL_INT_SAMPLER_2D_ARRAY,
GLenum_GL_UNSIGNED_INT_SAMPLER_2D,
GLenum_GL_UNSIGNED_INT_SAMPLER_3D,
GLenum_GL_UNSIGNED_INT_SAMPLER_CUBE,
GLenum_GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
return fmt.Sprintf("texture(%s, vec3(0.)).x", name), nil
default:
return "", fmt.Errorf("Unknown uniform type %s", ty)
}
}