MCPcopy Create free account
hub / github.com/emscripten-core/emscripten / main

Function main

test/browser/webgl2_ubo_layout_binding.c:64–201  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

62}
63
64int main(int argc, char *argv[])
65{
66 EmscriptenWebGLContextAttributes attr;
67 emscripten_webgl_init_context_attributes(&attr);
68 attr.majorVersion = 2;
69 EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context("#canvas", &attr);
70 emscripten_webgl_make_context_current(ctx);
71
72 GLuint vs = CompileShader(GL_VERTEX_SHADER,
73 "#version 300 es\n"
74 "in vec4 pos;\n"
75 "void main() { gl_Position = pos; }");
76
77 GLuint ps = CompileShader(GL_FRAGMENT_SHADER,
78 "#version 300 es\n"
79 "precision lowp float;\n"
80 "layout(binding = 4) uniform red { vec4 unused1; float r; };\n"
81 "layout(binding = 5, std140) uniform Green { float unused2; float g; } green;\n"
82 "layout(std140, binding = 6) uniform Blue { vec3 unused3; float b; } blue[2];\n"
83 "out vec4 outColor;\n"
84 "void main() { outColor = vec4(r, green.g, blue[0].b + blue[1].b, 1.0); }");
85
86 // The above shader compilation should preserve the std140 layout part in the uniforms, so
87 // make sure that did happen. (https://github.com/emscripten-core/emscripten/issues/25828)
88 assert(strstr(emscripten_webgl_get_shader_source_utf8(ps), "layout(std140) uniform Green"));
89 assert(strstr(emscripten_webgl_get_shader_source_utf8(ps), "layout(std140) uniform Blue"));
90
91 GLuint program = CreateProgram(vs, ps);
92 glUseProgram(program);
93
94 int numBlocks;
95 glGetProgramiv(program, GL_ACTIVE_UNIFORM_BLOCKS, &numBlocks);
96 for(int i = 0; i < numBlocks; ++i) {
97 int size = -1;
98 glGetActiveUniformBlockiv(program, i, GL_UNIFORM_BLOCK_DATA_SIZE, &size);
99 printf("Uniform block at index %d: size: %d\n", i, size);
100 }
101
102 static const float vb[] = {
103 -1.0f, -1.0f,
104 1.0f, -1.0f,
105 -1.0f, 1.0f,
106 -1.0f, 1.0f,
107 1.0f, -1.0f,
108 1.0f, 1.0f,
109 };
110
111 GLuint vbo;
112 glGenBuffers(1, &vbo);
113 glBindBuffer(GL_ARRAY_BUFFER, vbo);
114 glBufferData(GL_ARRAY_BUFFER, sizeof(vb), vb, GL_STATIC_DRAW);
115 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 8, 0);
116 glEnableVertexAttribArray(0);
117
118 struct {
119 float unused1[4];
120 float r;
121 float unused2[3];

Callers

nothing calls this directly

Calls 10

printfFunction · 0.85
glBufferDataFunction · 0.85
sprintfFunction · 0.85
CompileShaderFunction · 0.70
CreateProgramFunction · 0.70
assertFunction · 0.50
strstrFunction · 0.50

Tested by

no test coverage detected