Skip to content

Convert webgl tests to C where possible. NFC #23534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@
#include <emscripten/html5.h>
#include <GLES2/gl2.h>

GLuint CompileShader(GLenum type, const char *src)
{
GLuint CompileShader(GLenum type, const char *src) {
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &src, NULL);
glCompileShader(shader);
assert(glGetError() == GL_NO_ERROR && "Shader compilation failed!");
return shader;
}

int main(int argc, char *argv[])
{
int main(int argc, char *argv[]) {
emscripten_set_canvas_element_size("#canvas", 256, 256);
EmscriptenWebGLContextAttributes attr;
emscripten_webgl_init_context_attributes(&attr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
#include <stdio.h>
#include <assert.h>

int main()
{
int main() {
EmscriptenWebGLContextAttributes attr;
emscripten_webgl_init_context_attributes(&attr);
attr.majorVersion = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
// WebGL 2 core does not support GL_UNSIGNED_SHORT_5_6_5_REV extension.
#define GL_UNSIGNED_SHORT_5_6_5_REV 0x8364

int main()
{
int main() {
EmscriptenWebGLContextAttributes attrs;
emscripten_webgl_init_context_attributes(&attrs);
attrs.majorVersion = 2;
Expand All @@ -30,7 +29,7 @@ int main()
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
void* data = new char[512];
char data[512];
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5_REV, data);
assert(glGetError() != 0); // We should be getting an error
return 0;
Expand Down
34 changes: 16 additions & 18 deletions test/browser/webgl2_objects.cpp → test/browser/webgl2_objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@
{ \
x; \
GLenum error = glGetError(); \
if( error != GL_NO_ERROR ) { \
printf( "GL ERROR: %d, %s\n", (int)error, #x ); \
if (error != GL_NO_ERROR) { \
printf("GL ERROR: %d, %s\n", (int)error, #x); \
assert(false); \
} \
} \


int main()
{
emscripten_set_canvas_element_size( "#canvas", 100, 100 );
int main() {
emscripten_set_canvas_element_size("#canvas", 100, 100);

EmscriptenWebGLContextAttributes attrs;
emscripten_webgl_init_context_attributes(&attrs);
Expand All @@ -34,9 +33,8 @@ int main()
attrs.majorVersion = 2;
attrs.minorVersion = 0;

EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context = emscripten_webgl_create_context( "#canvas", &attrs );
if (!context)
{
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context = emscripten_webgl_create_context("#canvas", &attrs);
if (!context) {
printf("Skipped: WebGL 2 is not supported.\n");
return 0;
}
Expand All @@ -45,27 +43,27 @@ int main()
// Anisotropy
//
GLfloat maxAnisotropy;
GL_CALL( glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropy) );
GL_CALL(glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropy));
printf("max anisotropy: %f\n", maxAnisotropy);
assert(maxAnisotropy > 1.f);

// Vertex Arrays
//
GLuint vao1;
GL_CALL( glGenVertexArrays( 1, &vao1 ) );
GL_CALL( glBindVertexArray( vao1 ) );
printf( "vao1: %d\n", vao1 );
GL_CALL(glGenVertexArrays(1, &vao1));
GL_CALL(glBindVertexArray(vao1));
printf("vao1: %d\n", vao1);
assert(vao1 > 0);

GLint vao2;
GL_CALL( glGetIntegerv( GL_VERTEX_ARRAY_BINDING, &vao2 ) );
printf( "vao2: %d\n", vao2 );
GL_CALL(glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &vao2));
printf("vao2: %d\n", vao2);
assert(vao2 == vao1);

// Sampler Objects
//
GLuint sampler;
GL_CALL( glGenSamplers( 1, &sampler ) );
GL_CALL(glGenSamplers(1, &sampler));
assert(sampler > 0);

#if 0 // TODO: Disabled due to https://github.com/KhronosGroup/WebGL/issues/2006
Expand All @@ -77,11 +75,11 @@ int main()
assert(maxAnisotropy2 == maxAnisotropy);
#endif

GL_CALL( glBindSampler( 0, sampler ) );
GL_CALL(glBindSampler(0, sampler));

GLint sampler2;
GL_CALL( glGetIntegerv( GL_SAMPLER_BINDING, &sampler2 ) );
printf( "sampler2: %d\n", sampler2 );
GL_CALL(glGetIntegerv(GL_SAMPLER_BINDING, &sampler2));
printf("sampler2: %d\n", sampler2);
assert(sampler2 == sampler);

return 0;
Expand Down
17 changes: 8 additions & 9 deletions test/browser/webgl2_pbo.cpp → test/browser/webgl2_pbo.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
#include <GLES3/gl3.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <emscripten.h>
#include <emscripten/html5.h>
#include <assert.h>

#include <string>

#define GL_CALL( x ) \
{ \
x; \
Expand Down Expand Up @@ -58,7 +57,7 @@ int main()
glBufferData(GL_PIXEL_UNPACK_BUFFER, 4*4, pixels, GL_STATIC_DRAW);

/* This should use the unpack buffer */
GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr));
GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 4, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL));

glDeleteBuffers(1, &buffer);

Expand All @@ -67,9 +66,8 @@ int main()
}

/* Verify that unpack buffer is used for compressed image upload as well */
const std::string exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
if(exts.find("WEBGL_compressed_texture_s3tc") != std::string::npos)
{
const char* exts = (const char*)glGetString(GL_EXTENSIONS);
if (strstr(exts, "WEBGL_compressed_texture_s3tc") != NULL) {
printf("WEBGL_compressed_texture_s3tc is supported, testing ...\n");

glBindTexture(GL_TEXTURE_2D, tex[1]);
Expand All @@ -81,17 +79,18 @@ int main()
glBufferData(GL_PIXEL_UNPACK_BUFFER, 8, pixels, GL_STATIC_DRAW);

/* This should all use the unpack buffer */
GL_CALL(glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, 4, 4, 0, 8, nullptr));
GL_CALL(glCompressedTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 4, 4, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, 8, nullptr));
GL_CALL(glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, 4, 4, 0, 8, NULL));
GL_CALL(glCompressedTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 4, 4, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, 8, NULL));

glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);

/* This not anymore */
GL_CALL(glCompressedTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 4, 4, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, 8, pixels));

glDeleteBuffers(1, &buffer);
} else {
printf("WEBGL_compressed_texture_s3tc is NOT supported\n");
}
else printf("WEBGL_compressed_texture_s3tc is NOT supported\n");

glDeleteTextures(2, tex);

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void report_result(int result) {
emscripten_force_exit(result);
}

void finish(void*) {
void finish(void* arg) {
report_result(0);
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
// found in the LICENSE file.

#include <stdlib.h>
#include <cassert>
#include <cstdio>
#include <assert.h>
#include <stdio.h>
#include <emscripten.h>
#include <emscripten/html5.h>

Expand All @@ -25,8 +25,7 @@ GLuint sampleQuery = 0;
} \
} \

void getQueryResult()
{
void getQueryResult() {
/* Get the result. It should be nonzero. */
GLuint any;
GL_CALL(glGetQueryObjectuiv(sampleQuery, GL_QUERY_RESULT, &any));
Expand All @@ -42,8 +41,7 @@ void getQueryResult()
exit(result);
}

GLuint compile_shader(GLenum shaderType, const char *src)
{
GLuint compile_shader(GLenum shaderType, const char *src) {
GLuint shader = glCreateShader(shaderType);
GL_CALL(glShaderSource(shader, 1, &src, NULL));
GL_CALL(glCompileShader(shader));
Expand All @@ -54,8 +52,7 @@ GLuint compile_shader(GLenum shaderType, const char *src)
return shader;
}

GLuint create_program(GLuint vert, GLuint frag)
{
GLuint create_program(GLuint vert, GLuint frag) {
GLuint program = glCreateProgram();
GL_CALL(glAttachShader(program, vert));
GL_CALL(glAttachShader(program, frag));
Expand Down Expand Up @@ -108,7 +105,7 @@ int main() {
"}\n");
GLuint program = create_program(vert, frag);
GL_CALL(glUseProgram(program));
const float positions[]{
const float positions[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ int main()
int mips = 10;
int sizeO = 512;

unsigned short* data = new unsigned short[ 512 * 512 * 4 ];
unsigned short* data = malloc(512 * 512 * 4 * 2);
//memset( data, 0, 512 * 512 * 4 );

// Create texture 1
Expand All @@ -113,7 +113,7 @@ int main()
for( int i=0; i<6; ++i )
GL_CALL( glTexSubImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X+i, 0, 0, 0, sizeO, sizeO, GL_RGBA, GL_HALF_FLOAT, data ) );

delete [] data;
free(data);

// Create texture 2
GLuint tex2;
Expand Down
32 changes: 16 additions & 16 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,7 @@ def test_webgl_context_attributes(self):

@requires_graphics_hardware
def test_webgl_no_double_error(self):
self.btest_exit('webgl_error.cpp')
self.btest_exit('webgl_error.c')

@requires_graphics_hardware
def test_webgl_parallel_shader_compile(self):
Expand Down Expand Up @@ -2644,7 +2644,7 @@ def test_html5_webgl_create_context(self, args):
@requires_graphics_hardware
# Verify bug https://github.com/emscripten-core/emscripten/issues/4556: creating a WebGL context to Module.canvas without an ID explicitly assigned to it.
def test_html5_webgl_create_context2(self):
self.btest_exit('webgl_create_context2.cpp')
self.btest_exit('webgl_create_context2.c')

@requires_graphics_hardware
# Verify bug https://github.com/emscripten-core/emscripten/issues/22943: creating a WebGL context with explicit swap control and offscreenCanvas
Expand All @@ -2668,11 +2668,11 @@ def test_html5_special_event_targets(self):
'full_es2': (['-sFULL_ES2'],),
})
def test_html5_webgl_destroy_context(self, args):
self.btest_exit('webgl_destroy_context.cpp', args=args + ['--shell-file', test_file('browser/webgl_destroy_context_shell.html'), '-lGL'])
self.btest_exit('webgl_destroy_context.c', args=args + ['--shell-file', test_file('browser/webgl_destroy_context_shell.html'), '-lGL'])

@requires_graphics_hardware
def test_webgl_context_params(self):
self.btest_exit('webgl_color_buffer_readpixels.cpp', args=['-lGL'])
self.btest_exit('webgl_color_buffer_readpixels.c', args=['-lGL'])

# Test for PR#5373 (https://github.com/emscripten-core/emscripten/pull/5373)
@requires_graphics_hardware
Expand All @@ -2681,7 +2681,7 @@ def test_webgl_context_params(self):
'full_es2': (['-sFULL_ES2'],),
})
def test_webgl_shader_source_length(self, args):
self.btest_exit('webgl_shader_source_length.cpp', args=args + ['-lGL'])
self.btest_exit('webgl_shader_source_length.c', args=args + ['-lGL'])

# Tests calling glGetString(GL_UNMASKED_VENDOR_WEBGL).
@requires_graphics_hardware
Expand All @@ -2697,23 +2697,23 @@ def test_webgl_unmasked_vendor_webgl(self):
def test_webgl2(self, args):
if '-sMIN_CHROME_VERSION=0' in args and self.is_wasm64():
self.skipTest('wasm64 not supported by legacy browsers')
self.btest_exit('webgl2.cpp', args=['-sMAX_WEBGL_VERSION=2', '-lGL'] + args)
self.btest_exit('webgl2.c', args=['-sMAX_WEBGL_VERSION=2', '-lGL'] + args)

# Tests the WebGL 2 glGetBufferSubData() functionality.
@requires_graphics_hardware
@no_4gb('getBufferSubData fails: https://crbug.com/325090165')
def test_webgl2_get_buffer_sub_data(self):
self.btest_exit('webgl2_get_buffer_sub_data.cpp', args=['-sMAX_WEBGL_VERSION=2', '-lGL'])
self.btest_exit('webgl2_get_buffer_sub_data.c', args=['-sMAX_WEBGL_VERSION=2', '-lGL'])

@requires_graphics_hardware
def test_webgl2_pthreads(self):
# test that a program can be compiled with pthreads and render WebGL2 properly on the main thread
# (the testcase doesn't even use threads, but is compiled with thread support).
self.btest_exit('webgl2.cpp', args=['-sMAX_WEBGL_VERSION=2', '-lGL', '-pthread'])
self.btest_exit('webgl2.c', args=['-sMAX_WEBGL_VERSION=2', '-lGL', '-pthread'])

@requires_graphics_hardware
def test_webgl2_objects(self):
self.btest_exit('webgl2_objects.cpp', args=['-sMAX_WEBGL_VERSION=2', '-lGL'])
self.btest_exit('webgl2_objects.c', args=['-sMAX_WEBGL_VERSION=2', '-lGL'])

@requires_graphics_hardware
@parameterized({
Expand All @@ -2738,7 +2738,7 @@ def test_webgl_preprocessor_variables(self, opts):

@requires_graphics_hardware
def test_webgl2_ubos(self):
self.btest_exit('webgl2_ubos.cpp', args=['-sMAX_WEBGL_VERSION=2', '-lGL'])
self.btest_exit('webgl2_ubos.c', args=['-sMAX_WEBGL_VERSION=2', '-lGL'])

@requires_graphics_hardware
@parameterized({
Expand All @@ -2748,11 +2748,11 @@ def test_webgl2_ubos(self):
def test_webgl2_garbage_free_entrypoints(self, args):
if args and self.is_4gb():
self.skipTest('readPixels fails: https://crbug.com/324992397')
self.btest_exit('webgl2_garbage_free_entrypoints.cpp', args=args)
self.btest_exit('webgl2_garbage_free_entrypoints.c', args=args)

@requires_graphics_hardware
def test_webgl2_backwards_compatibility_emulation(self):
self.btest_exit('webgl2_backwards_compatibility_emulation.cpp', args=['-sMAX_WEBGL_VERSION=2', '-sWEBGL2_BACKWARDS_COMPATIBILITY_EMULATION'])
self.btest_exit('webgl2_backwards_compatibility_emulation.c', args=['-sMAX_WEBGL_VERSION=2', '-sWEBGL2_BACKWARDS_COMPATIBILITY_EMULATION'])

@requires_graphics_hardware
def test_webgl2_runtime_no_context(self):
Expand Down Expand Up @@ -2781,11 +2781,11 @@ def test_webgl_context_major_version(self):

@requires_graphics_hardware
def test_webgl2_invalid_teximage2d_type(self):
self.btest_exit('webgl2_invalid_teximage2d_type.cpp', args=['-sMAX_WEBGL_VERSION=2'])
self.btest_exit('webgl2_invalid_teximage2d_type.c', args=['-sMAX_WEBGL_VERSION=2'])

@requires_graphics_hardware
def test_webgl_with_closure(self):
self.btest_exit('webgl_with_closure.cpp', args=['-O2', '-sMAX_WEBGL_VERSION=2', '--closure=1', '-lGL'])
self.btest_exit('webgl_with_closure.c', args=['-O2', '-sMAX_WEBGL_VERSION=2', '--closure=1', '-lGL'])

# Tests that -sGL_ASSERTIONS and glVertexAttribPointer with packed types works
@requires_graphics_hardware
Expand All @@ -2795,7 +2795,7 @@ def test_webgl2_packed_types(self):
@requires_graphics_hardware
@no_4gb('compressedTexSubImage2D fails: https://crbug.com/324562920')
def test_webgl2_pbo(self):
self.btest_exit('webgl2_pbo.cpp', args=['-sMAX_WEBGL_VERSION=2', '-lGL'])
self.btest_exit('webgl2_pbo.c', args=['-sMAX_WEBGL_VERSION=2', '-lGL'])

@no_firefox('fails on CI likely due to GPU drivers there')
@requires_graphics_hardware
Expand Down Expand Up @@ -4356,7 +4356,7 @@ def test_webgl_draw_base_vertex_base_instance(self, multi_draw, draw_elements):

@requires_graphics_hardware
def test_webgl_sample_query(self):
self.btest_exit('webgl_sample_query.cpp', args=['-sMAX_WEBGL_VERSION=2', '-lGL'])
self.btest_exit('webgl_sample_query.c', args=['-sMAX_WEBGL_VERSION=2', '-lGL'])

@requires_graphics_hardware
@parameterized({
Expand Down