aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


client/tests/browser_context.c (11265B)


      1 #include "gl_renderer.h"
      2 #include "linux_graphics.h"
      3 #include <GLES3/gl3.h>
      4 #include <assert.h>
      5 #include <emscripten.h>
      6 #include <emscripten/html5.h>
      7 #include <emscripten/threading.h>
      8 #include <stdatomic.h>
      9 #include <stdio.h>
     10 #include <string.h>
     11 
     12 static struct SDL_Window *window;
     13 static struct gl_target *image, *first, *second;
     14 static struct gl_depth *depth;
     15 static unsigned iteration;
     16 static atomic_int restored;
     17 static atomic_int context_queries;
     18 static atomic_int vertex_uploads, vertex_bytes, uniform_updates;
     19 static struct gl_vertex_buffer *buffer;
     20 static struct {
     21     float position[3];
     22     uint32_t color;
     23     float uv[2];
     24 } buffer_vertices[64] = {
     25     {{0, 0, 0}, 0, {0, 0}}, {{0, 0, 0}, 0, {0, 0}},
     26     {{-1, -1, .5f}, 0xffffffff, {0, 0}}, {{1, -1, .5f}, 0xffffffff, {1, 0}},
     27     {{-1, 1, .5f}, 0xffffffff, {0, 1}}, {{1, 1, .5f}, 0xffffffff, {1, 1}}
     28 };
     29 
     30 static void count_context_queries_on_owner(void)
     31 {
     32     EM_ASM({
     33         const context = GL.currentContext.GLctx;
     34         const original = context.isContextLost;
     35         context.isContextLost = function() {
     36             Atomics.add(HEAP32, $0 >> 2, 1);
     37             return original.call(this);
     38         };
     39         const upload = context.bufferData;
     40         context.bufferData = function(target, data, ...args) {
     41             if (target === context.ARRAY_BUFFER && typeof data !== 'number') {
     42                 Atomics.add(HEAP32, $1 >> 2, 1);
     43                 Atomics.add(HEAP32, $3 >> 2, args.length >= 3 ? args[2] : data.byteLength);
     44             }
     45             return upload.call(this, target, data, ...args);
     46         };
     47         const update = context.bufferSubData;
     48         context.bufferSubData = function(target, offset, data, ...args) {
     49             if (target === context.ARRAY_BUFFER) {
     50                 Atomics.add(HEAP32, $1 >> 2, 1);
     51                 Atomics.add(HEAP32, $3 >> 2, args.length >= 2 ? args[1] : data.byteLength);
     52             }
     53             return update.call(this, target, offset, data, ...args);
     54         };
     55         const uniform = context.uniform1i;
     56         context.uniform1i = function(...args) {
     57             Atomics.add(HEAP32, $2 >> 2, 1);
     58             return uniform.call(this, ...args);
     59         };
     60     }, &context_queries, &vertex_uploads, &uniform_updates, &vertex_bytes);
     61 }
     62 
     63 static void retained_vertices_reuse_uploads(const struct gl_texture_state *texture)
     64 {
     65     const uint16_t indices[] = {0, 1, 2, 1, 3, 2};
     66     const unsigned coordinates[] = {0, 1};
     67     const float white[] = {1, 1, 1, 1};
     68     const int32_t clip[] = {0, 0, 2, 2};
     69     memcpy(&buffer_vertices[10], &buffer_vertices[2], 4 * sizeof(buffer_vertices[0]));
     70     buffer = gl_vertex_buffer_create(buffer_vertices, sizeof(buffer_vertices));
     71     gl_renderer_set_vertex_transform(NULL);
     72     gl_renderer_set_texture(0, texture);
     73     gl_renderer_set_fog(0, 0);
     74     gl_renderer_set_alpha_test(0, 8, 0);
     75     gl_target_draw_fvf(first, 4, NULL, 0x142, coordinates, white, 4, indices, 6, clip, buffer, 48);
     76     unsigned char actual[16];
     77     gl_target_read(first, actual);
     78     assert(atomic_load(&vertex_uploads) == 1);
     79     assert(atomic_load(&vertex_bytes) == 96);
     80     atomic_store(&uniform_updates, 0);
     81 
     82     for (unsigned index = 0; index < 3; ++index) {
     83         gl_renderer_set_vertex_transform(NULL);
     84         gl_renderer_set_texture(0, texture);
     85         gl_renderer_set_fog(0, 0);
     86         gl_renderer_set_alpha_test(0, 8, 0);
     87         gl_target_draw_fvf(first, 4, NULL, 0x142, coordinates, white, 4, indices, 6, clip, buffer, 48);
     88     }
     89     gl_target_read(first, actual);
     90 
     91     const unsigned char expected[] = {0, 0, 255, 255, 255, 255, 255, 255,
     92                                      255, 0, 0, 255, 0, 255, 0, 255};
     93     assert(!memcmp(actual, expected, sizeof(expected)));
     94     assert(atomic_load(&vertex_uploads) == 1);
     95     assert(atomic_load(&vertex_bytes) == 96);
     96     assert(atomic_load(&uniform_updates) == 0);
     97 
     98     /* Extending a draw uploads only the additional vertices. */
     99     gl_target_draw_fvf(first, 4, NULL, 0x142, coordinates, white, 6, indices, 6, clip, buffer, 48);
    100     gl_target_read(first, actual);
    101     assert(!memcmp(actual, expected, sizeof(expected)));
    102     assert(atomic_load(&vertex_uploads) == 2);
    103     assert(atomic_load(&vertex_bytes) == 144);
    104 
    105     /* Disjoint slices do not upload their unused gap, and repeated slices reuse data. */
    106     gl_target_draw_fvf(first, 4, NULL, 0x142, coordinates, white, 4, indices, 6, clip, buffer, 240);
    107     gl_target_draw_fvf(first, 4, NULL, 0x142, coordinates, white, 4, indices, 6, clip, buffer, 240);
    108     gl_target_read(first, actual);
    109     assert(!memcmp(actual, expected, sizeof(expected)));
    110     assert(atomic_load(&vertex_uploads) == 3);
    111     assert(atomic_load(&vertex_bytes) == 240);
    112     gl_target_draw_fvf(first, 4, NULL, 0x142, coordinates, white, 4, indices, 6, clip, buffer, 48);
    113     gl_target_read(first, actual);
    114     assert(!memcmp(actual, expected, sizeof(expected)));
    115 
    116     /* A marked write uploads once and changes the rendered pixels. */
    117     buffer_vertices[2].position[0] = buffer_vertices[4].position[0] = 1;
    118     gl_vertex_buffer_invalidate(buffer);
    119     gl_target_clear(first, 0xff000000, 1);
    120     gl_target_draw_fvf(first, 4, NULL, 0x142, coordinates, white, 4, indices, 6, clip, buffer, 48);
    121     gl_target_read(first, actual);
    122     const unsigned char black[] = {0, 0, 0, 255, 0, 0, 0, 255,
    123                                   0, 0, 0, 255, 0, 0, 0, 255};
    124     assert(!memcmp(actual, black, sizeof(black)));
    125     assert(atomic_load(&vertex_uploads) == 5);
    126     assert(atomic_load(&vertex_bytes) == 432);
    127     buffer_vertices[2].position[0] = buffer_vertices[4].position[0] = -1;
    128     gl_vertex_buffer_invalidate(buffer);
    129 }
    130 
    131 static void lose_context_on_owner(void)
    132 {
    133     EM_ASM({
    134         Atomics.store(HEAP32, $0 >> 2, 0);
    135         const context = GL.currentContext.GLctx;
    136         const extension = context.getExtension('WEBGL_lose_context');
    137         if (!extension) throw new Error('WEBGL_lose_context is required for this test');
    138         context.canvas.addEventListener('webglcontextlost', () => {
    139             setTimeout(() => extension.restoreContext(), 50);
    140         }, {once: true});
    141         context.canvas.addEventListener('webglcontextrestored', () => {
    142             Atomics.store(HEAP32, $0 >> 2, 1);
    143         }, {once: true});
    144         extension.loseContext();
    145     }, &restored);
    146 }
    147 
    148 static void lose_context(void)
    149 {
    150     if (EM_ASM_INT({ return !!GL.currentContextIsProxied; }))
    151         emscripten_sync_run_in_main_runtime_thread(EM_FUNC_SIG_V, lose_context_on_owner);
    152     else lose_context_on_owner();
    153 }
    154 
    155 static bool restored_frame(double timestamp, void *argument);
    156 
    157 int main(void)
    158 {
    159     window = linux_graphics_window();
    160     assert(window);
    161     image = gl_target_create(2, 2, 0);
    162     first = gl_target_create(2, 2, 0);
    163     second = gl_target_create(2, 2, 0);
    164     depth = gl_depth_create(2, 2, 16);
    165     gl_target_set_depth(first, depth);
    166     gl_target_set_depth(second, depth);
    167     const unsigned char pixels[] = {255, 0, 0, 255, 0, 255, 0, 255,
    168                                    0, 0, 255, 255, 255, 255, 255, 255};
    169     gl_target_write(image, pixels);
    170     const struct gl_vertex vertices[] = {
    171         {.position = {-1, -1, .5f, 1}, .color = {1, 1, 1, 1}, .uv = {0, 0}},
    172         {.position = {1, -1, .5f, 1}, .color = {1, 1, 1, 1}, .uv = {1, 0}},
    173         {.position = {-1, 1, .5f, 1}, .color = {1, 1, 1, 1}, .uv = {0, 1}},
    174         {.position = {1, 1, .5f, 1}, .color = {1, 1, 1, 1}, .uv = {1, 1}}
    175     };
    176     const uint16_t indices[] = {0, 1, 2, 1, 3, 2};
    177     const int32_t clip[] = {0, 0, 2, 2};
    178     struct gl_texture_state texture = {.image = image, .alpha = 1,
    179         .color_op = 2, .alpha_op = 2, .color_arg1 = 2, .alpha_arg1 = 2};
    180     gl_renderer_set_texture(0, &texture);
    181     gl_target_clear(first, 0xff000000, 1);
    182     gl_target_draw(first, 4, vertices, 4, indices, 6, clip);
    183 
    184     if (EM_ASM_INT({ return !!GL.currentContextIsProxied; }))
    185         emscripten_sync_run_in_main_runtime_thread(EM_FUNC_SIG_V, count_context_queries_on_owner);
    186     else count_context_queries_on_owner();
    187 
    188     for (unsigned index = 0; index < 1000; ++index)
    189         assert(linux_graphics_window() == window);
    190 
    191     assert(atomic_load(&context_queries) == 0);
    192     retained_vertices_reuse_uploads(&texture);
    193 
    194     /* Exercise recovery after repeated losses using the same graphics context. */
    195     lose_context();
    196     emscripten_request_animation_frame_loop(restored_frame, NULL);
    197     emscripten_exit_with_live_runtime();
    198 }
    199 
    200 static bool restored_frame(double timestamp, void *argument)
    201 {
    202     (void)timestamp;
    203     (void)argument;
    204     if (!atomic_load(&restored)) return true;
    205     const unsigned char expected[] = {0, 0, 255, 255, 255, 255, 255, 255,
    206                                      255, 0, 0, 255, 0, 255, 0, 255};
    207     const struct gl_vertex vertices[] = {
    208         {.position = {-1, -1, .5f, 1}, .color = {1, 1, 1, 1}, .uv = {0, 0}},
    209         {.position = {1, -1, .5f, 1}, .color = {1, 1, 1, 1}, .uv = {1, 0}},
    210         {.position = {-1, 1, .5f, 1}, .color = {1, 1, 1, 1}, .uv = {0, 1}},
    211         {.position = {1, 1, .5f, 1}, .color = {1, 1, 1, 1}, .uv = {1, 1}}
    212     };
    213     const uint16_t indices[] = {0, 1, 2, 1, 3, 2};
    214     const int32_t clip[] = {0, 0, 2, 2};
    215     struct gl_texture_state texture = {.image = image, .alpha = 1,
    216         .color_op = 2, .alpha_op = 2, .color_arg1 = 2, .alpha_arg1 = 2};
    217     assert(linux_graphics_window() == window);
    218     gl_renderer_set_vertex_transform(NULL);
    219     gl_renderer_set_texture(0, &texture);
    220     gl_renderer_set_texture(1, NULL);
    221     gl_renderer_set_alpha_test(0, 8, 0);
    222     gl_renderer_set_blend(0, 1, 1);
    223     gl_renderer_set_fog(0, 0);
    224     gl_renderer_set_depth(1, 1, 4, 0);
    225     gl_target_clear(first, 0xff000000, 1);
    226     gl_target_clear(second, 0xff000000, 1);
    227     gl_target_draw(first, 4, vertices, 4, indices, 6, clip);
    228     const unsigned coordinates[] = {0, 1};
    229     const float white[] = {1, 1, 1, 1};
    230     gl_target_draw_fvf(second, 4, NULL, 0x142, coordinates, white, 4, indices, 6, clip, buffer, 48);
    231     unsigned char actual[16];
    232     gl_target_read(first, actual);
    233 
    234     assert(!memcmp(actual, expected, sizeof(expected)));
    235     gl_target_read(second, actual);
    236     assert(!memcmp(actual, expected, sizeof(expected)));
    237 
    238     /* Resize and present through the default framebuffer after every restoration. */
    239     assert(emscripten_set_canvas_element_size("#canvas", 2 + iteration, 2 + iteration) == EMSCRIPTEN_RESULT_SUCCESS);
    240     gl_target_present(first, clip, 2, 2);
    241     glReadPixels(0, 0, 2, 2, GL_RGBA, GL_UNSIGNED_BYTE, actual);
    242     const unsigned char presented[] = {255, 0, 0, 255, 0, 255, 0, 255,
    243                                       0, 0, 255, 255, 255, 255, 255, 255};
    244     assert(!memcmp(actual, presented, sizeof(presented)));
    245     if (EM_ASM_INT({ return !!GL.currentContextIsProxied; }))
    246         assert(emscripten_webgl_commit_frame() == EMSCRIPTEN_RESULT_SUCCESS);
    247     assert(glGetError() == GL_NO_ERROR);
    248     if (++iteration < 3) {
    249         lose_context();
    250         return true;
    251     }
    252     gl_target_destroy(image);
    253     gl_target_destroy(first);
    254     gl_target_destroy(second);
    255     gl_depth_destroy(depth);
    256     gl_vertex_buffer_destroy(buffer);
    257     linux_graphics_shutdown();
    258     puts("PASS: repeated context loss restores textures, shared depth, shaders, resizing and presentation without JSPI");
    259     return false;
    260 }