aorecomp

Anarchy Online classic in the browser

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


commit e447a943a2006b9af8e0c54cb7e4772b04ce2ac6
parent 3f497fbecfb94d7773d7370840eac3b4703dc90d
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 09:59:18 +0000

Support managed texture eviction and startup render states on Linux

Diffstat:
Mdecompiled/runtime/gl_renderer.c | 81++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
Mdecompiled/runtime/gl_renderer.h | 4+++-
Mdecompiled/runtime/linux_graphics.c | 38++++++++++++++++++++++++++++++--------
Mdecompiled/tests/devices.c | 122+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Mdecompiled/tests/graphics.c | 25+++++++++++++++++++++++++
Mtools/reconstruction/test_portable_devices.py | 4++--
6 files changed, 241 insertions(+), 33 deletions(-)

diff --git a/decompiled/runtime/gl_renderer.c b/decompiled/runtime/gl_renderer.c @@ -8,6 +8,8 @@ static GLuint program, vertex_array, vertex_buffer, index_buffer; static GLint texture_enabled, texture_alpha, texture_modulate, texture_modulate_alpha; +static int depth_write = 1; +static uint32_t depth_function = 4; struct gl_depth { unsigned width, height, references; GLuint buffer; @@ -16,7 +18,9 @@ struct gl_target { unsigned width, height; GLuint framebuffer, color; struct gl_depth *depth; + unsigned char *saved_pixels; }; +static void make_resident(struct gl_target *target); static void check_graphics(const char *operation) { @@ -132,6 +136,7 @@ void gl_depth_destroy(struct gl_depth *depth) void gl_target_set_depth(struct gl_target *target, struct gl_depth *depth) { + make_resident(target); assert(!depth || (depth->width == target->width && depth->height == target->height)); if (depth) ++depth->references; if (target->depth) gl_depth_destroy(target->depth); @@ -143,34 +148,46 @@ void gl_target_set_depth(struct gl_target *target, struct gl_depth *depth) check_graphics("depth attachment"); } -struct gl_target *gl_target_create(unsigned width, unsigned height) +static int allocate_target(struct gl_target *target) { - struct gl_target *target = calloc(1, sizeof(*target)); - if (!target) return NULL; - target->width = width; - target->height = height; - /* Color and depth belong to the target, so switching targets preserves both. */ glGenTextures(1, &target->color); glBindTexture(GL_TEXTURE_2D, target->color); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, target->width, target->height, 0, + GL_RGBA, GL_UNSIGNED_BYTE, target->saved_pixels); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0); - target->depth = gl_depth_create(width, height, 24); - if (!target->depth) { gl_target_destroy(target); return NULL; } glGenFramebuffers(1, &target->framebuffer); glBindFramebuffer(GL_FRAMEBUFFER, target->framebuffer); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, target->color, 0); - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, target->depth->buffer); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, + target->depth ? target->depth->buffer : 0); GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); glBindFramebuffer(GL_FRAMEBUFFER, 0); if (status != GL_FRAMEBUFFER_COMPLETE) { fprintf(stderr, "Native framebuffer creation failed: %04x\n", status); - gl_target_destroy(target); - return NULL; + return 0; } check_graphics("target creation"); + free(target->saved_pixels); + target->saved_pixels = NULL; + return 1; +} + +static void make_resident(struct gl_target *target) +{ + if (target->saved_pixels && !allocate_target(target)) abort(); +} + +struct gl_target *gl_target_create(unsigned width, unsigned height) +{ + struct gl_target *target = calloc(1, sizeof(*target)); + if (!target) return NULL; + target->width = width; + target->height = height; + target->depth = gl_depth_create(width, height, 24); + if (!target->depth || !allocate_target(target)) { gl_target_destroy(target); return NULL; } return target; } @@ -179,17 +196,34 @@ void gl_target_destroy(struct gl_target *target) glDeleteFramebuffers(1, &target->framebuffer); if (target->depth) gl_depth_destroy(target->depth); glDeleteTextures(1, &target->color); + free(target->saved_pixels); free(target); } +void gl_target_evict(struct gl_target *target) +{ + /* Managed textures retain their contents while releasing GPU allocations. */ + assert(!target->depth); + if (target->saved_pixels) return; + unsigned char *pixels = malloc((size_t)target->width * target->height * 4); + if (!pixels) abort(); + gl_target_read(target, pixels); + glDeleteFramebuffers(1, &target->framebuffer); + glDeleteTextures(1, &target->color); + target->framebuffer = target->color = 0; + target->saved_pixels = pixels; + check_graphics("texture eviction"); +} + void gl_target_clear(struct gl_target *target, uint32_t argb, float depth) { const int32_t rectangle[] = {0, 0, target->width, target->height}; - gl_target_clear_rect(target, argb, depth, rectangle); + gl_target_clear_rect(target, 3, argb, depth, rectangle); } -void gl_target_clear_rect(struct gl_target *target, uint32_t argb, float depth, const int32_t rectangle[4]) +void gl_target_clear_rect(struct gl_target *target, uint32_t flags, uint32_t argb, float depth, const int32_t rectangle[4]) { + make_resident(target); int32_t left = rectangle[0] < 0 ? 0 : rectangle[0], top = rectangle[1] < 0 ? 0 : rectangle[1]; int32_t right = rectangle[2] > (int32_t)target->width ? (int32_t)target->width : rectangle[2]; int32_t bottom = rectangle[3] > (int32_t)target->height ? (int32_t)target->height : rectangle[3]; @@ -202,7 +236,7 @@ void gl_target_clear_rect(struct gl_target *target, uint32_t argb, float depth, glClearColor(((argb >> 16) & 255) / 255.0f, ((argb >> 8) & 255) / 255.0f, (argb & 255) / 255.0f, (argb >> 24) / 255.0f); glClearDepth(depth); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glClear((flags & 1 ? GL_COLOR_BUFFER_BIT : 0) | (flags & 2 ? GL_DEPTH_BUFFER_BIT : 0)); glDisable(GL_SCISSOR_TEST); glBindFramebuffer(GL_FRAMEBUFFER, 0); check_graphics("target clear"); @@ -211,6 +245,7 @@ void gl_target_clear_rect(struct gl_target *target, uint32_t argb, float depth, void gl_target_draw(struct gl_target *target, const struct gl_vertex *vertices, unsigned vertex_count, const uint16_t *indices, unsigned index_count, const int32_t clip[4]) { + make_resident(target); glBindFramebuffer(GL_FRAMEBUFFER, target->framebuffer); glViewport(0, 0, target->width, target->height); glEnable(GL_SCISSOR_TEST); @@ -218,8 +253,8 @@ void gl_target_draw(struct gl_target *target, const struct gl_vertex *vertices, glDisable(GL_CULL_FACE); if (target->depth) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); - glDepthFunc(GL_LEQUAL); - glDepthMask(GL_TRUE); + glDepthFunc(GL_NEVER + depth_function - 1); + glDepthMask(depth_write ? GL_TRUE : GL_FALSE); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glUseProgram(program); glBindVertexArray(vertex_array); @@ -236,6 +271,7 @@ void gl_target_draw(struct gl_target *target, const struct gl_vertex *vertices, void gl_target_read(struct gl_target *target, unsigned char *rgba) { + make_resident(target); glBindFramebuffer(GL_FRAMEBUFFER, target->framebuffer); glReadPixels(0, 0, target->width, target->height, GL_RGBA, GL_UNSIGNED_BYTE, rgba); glBindFramebuffer(GL_FRAMEBUFFER, 0); @@ -247,6 +283,7 @@ void gl_target_read(struct gl_target *target, unsigned char *rgba) void gl_target_write(struct gl_target *target, const unsigned char *rgba) { + make_resident(target); glBindTexture(GL_TEXTURE_2D, target->color); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, target->width, target->height, GL_RGBA, GL_UNSIGNED_BYTE, rgba); @@ -255,6 +292,8 @@ void gl_target_write(struct gl_target *target, const unsigned char *rgba) void gl_target_blit(struct gl_target *source, struct gl_target *target, const int32_t source_rect[4], const int32_t target_rect[4]) { + make_resident(source); + make_resident(target); glBindFramebuffer(GL_READ_FRAMEBUFFER, source->framebuffer); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, target->framebuffer); glDisable(GL_SCISSOR_TEST); @@ -266,6 +305,7 @@ void gl_target_blit(struct gl_target *source, struct gl_target *target, const in void gl_target_present(struct gl_target *target, const int32_t rectangle[4], int width, int height) { + make_resident(target); glBindFramebuffer(GL_READ_FRAMEBUFFER, target->framebuffer); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glDisable(GL_SCISSOR_TEST); @@ -282,6 +322,7 @@ void gl_renderer_set_texture(const struct gl_texture_state *state) glUniform1i(texture_enabled, state != NULL); glActiveTexture(GL_TEXTURE0); if (state) { + make_resident(state->image); glBindTexture(GL_TEXTURE_2D, state->image->color); GLint min_filter = state->min_linear ? GL_LINEAR : GL_NEAREST; if (state->mip_point) min_filter = state->min_linear ? GL_LINEAR_MIPMAP_NEAREST : GL_NEAREST_MIPMAP_NEAREST; @@ -296,6 +337,12 @@ void gl_renderer_set_texture(const struct gl_texture_state *state) check_graphics("texture binding"); } +void gl_renderer_set_depth(int write, uint32_t function) +{ + depth_write = write; + depth_function = function; +} + static GLenum blend_factor(uint32_t factor) { /* Direct3D blend factors supported by the platform adapter. */ diff --git a/decompiled/runtime/gl_renderer.h b/decompiled/runtime/gl_renderer.h @@ -19,11 +19,12 @@ int gl_renderer_initialize(void); void gl_renderer_shutdown(void); struct gl_target *gl_target_create(unsigned width, unsigned height); void gl_target_destroy(struct gl_target *target); +void gl_target_evict(struct gl_target *target); struct gl_depth *gl_depth_create(unsigned width, unsigned height, unsigned bits); void gl_depth_destroy(struct gl_depth *depth); void gl_target_set_depth(struct gl_target *target, struct gl_depth *depth); void gl_target_clear(struct gl_target *target, uint32_t argb, float depth); -void gl_target_clear_rect(struct gl_target *target, uint32_t argb, float depth, const int32_t rectangle[4]); +void gl_target_clear_rect(struct gl_target *target, uint32_t flags, uint32_t argb, float depth, const int32_t rectangle[4]); /* Clip coordinates are guest top-down x, y, width, height. */ void gl_target_draw(struct gl_target *target, const struct gl_vertex *vertices, unsigned vertex_count, const uint16_t *indices, unsigned index_count, const int32_t clip[4]); @@ -33,5 +34,6 @@ void gl_target_blit(struct gl_target *source, struct gl_target *target, const in void gl_target_present(struct gl_target *target, const int32_t rectangle[4], int width, int height); void gl_renderer_set_texture(const struct gl_texture_state *state); void gl_renderer_set_blend(int enabled, uint32_t source, uint32_t destination); +void gl_renderer_set_depth(int write, uint32_t function); #endif diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c @@ -42,7 +42,7 @@ static struct device_object { uint32_t interface, references, cull; uint32_t min_filter, mag_filter, mip_filter, address_u, address_v, color_op, alpha_op; uint32_t alpha_arg2; - uint32_t blend, source_blend, destination_blend; + uint32_t blend, source_blend, destination_blend, depth_write, depth_function, clipping, lighting; uint32_t material[17], viewport[6]; int scene; struct draw_object *owner; @@ -719,6 +719,15 @@ static void create_surface(void) g_eax = 0; } +static void evict_managed_textures(void) +{ + struct draw_object *owner = find_object(MEM32(g_esp + 4)); + for (struct surface_object *surface = surfaces; surface; surface = surface->next) + if (surface->owner == owner && surface->caps2 == 0x10) gl_target_evict(surface->target); + g_eax = 0; + g_esp += 8; +} + static void surface_query_interface(void) { struct surface_object *surface = find_surface(MEM32(g_esp + 4)); @@ -1136,6 +1145,10 @@ static void create_device(void) device->alpha_arg2 = 1; device->source_blend = 2; device->destination_blend = 1; + device->depth_write = 1; + device->depth_function = 4; + device->clipping = 1; + device->lighting = 1; device->owner = owner; device->target = surface; device->viewport[2] = surface->width; @@ -1289,14 +1302,14 @@ static void device_clear(void) uint32_t count = MEM32(g_esp + 8), rectangles = MEM32(g_esp + 12), flags = MEM32(g_esp + 16); uint32_t color = MEM32(g_esp + 20), depth = MEM32(g_esp + 24), stencil = MEM32(g_esp + 28); g_esp += 32; - if ((count && !rectangles) || flags != 3 || stencil) { + if ((count && !rectangles) || (flags & ~3u)) { fprintf(stderr, "Unsupported Direct3D clear count=%u flags=%08x stencil=%u\n", count, flags, stencil); exit(3); } float z; memcpy(&z, &depth, 4); const int32_t viewport[] = {device->viewport[0], device->viewport[1], device->viewport[0] + device->viewport[2], device->viewport[1] + device->viewport[3]}; - if (!count) gl_target_clear_rect(device->target->target, color, z, viewport); + if (!count) gl_target_clear_rect(device->target->target, flags, color, z, viewport); for (uint32_t i = 0; i < count; ++i) { int32_t rectangle[4]; memcpy(rectangle, (void *)ADDR(rectangles + i * 16), 16); @@ -1305,7 +1318,7 @@ static void device_clear(void) } for (unsigned j = 0; j < 4; ++j) if (j < 2 ? rectangle[j] < viewport[j] : rectangle[j] > viewport[j]) rectangle[j] = viewport[j]; - gl_target_clear_rect(device->target->target, color, z, rectangle); + gl_target_clear_rect(device->target->target, flags, color, z, rectangle); } g_eax = 0; } @@ -1325,17 +1338,20 @@ static uint32_t device_state(struct device_object *device, uint32_t state) case 58: case 59: case 60: return UINT32_MAX; /* Stencil masks/texture factor. */ case 128: case 129: case 130: case 131: case 132: case 133: case 134: case 135: return 0; /* Coordinate wrapping. */ - case 136: case 141: case 142: case 145: return 1; + case 141: case 142: case 145: return 1; + case 136: return device->clipping; case 138: case 139: case 140: case 143: case 144: case 147: case 148: case 151: case 152: return 0; case 146: return 2; /* Specular material uses vertex color 2. */ - case 7: case 14: case 137: return 1; /* Z enable/write and lighting. */ + case 7: return 1; /* Z enable. */ + case 137: return device->lighting; + case 14: return device->depth_write; case 8: return 3; /* Solid fill. */ case 9: return 2; /* Gouraud shading. */ case 19: return device->source_blend; case 20: return device->destination_blend; case 22: return device->cull; - case 23: return 4; /* LESSEQUAL depth comparison. */ + case 23: return device->depth_function; case 27: return device->blend; case 26: case 28: return 0; /* Dither and fog. */ default: fprintf(stderr, "Unsupported Direct3D render state %u\n", state); exit(3); @@ -1357,6 +1373,10 @@ static void device_set_state(void) uint32_t state = MEM32(g_esp + 8), value = MEM32(g_esp + 12); if (state == 22 && (value == 1 || value == 3)) device->cull = value; else if (state == 27 && value <= 1) device->blend = value; + else if (state == 14 && value <= 1) device->depth_write = value; + else if (state == 23 && value >= 1 && value <= 8) device->depth_function = value; + else if (state == 136 && value <= 1) device->clipping = value; + else if (state == 137 && value <= 1) device->lighting = value; else if (state == 19 && (value == 1 || value == 2 || value == 5 || value == 6)) device->source_blend = value; else if (state == 20 && (value == 1 || value == 2 || value == 5 || value == 6)) device->destination_blend = value; else if (value != device_state(device, state)) { @@ -1407,6 +1427,7 @@ static void device_draw_primitive(void) gl_renderer_set_texture(&state); } else gl_renderer_set_texture(NULL); gl_renderer_set_blend(device->blend, device->source_blend, device->destination_blend); + gl_renderer_set_depth(device->depth_write, device->depth_function); const int32_t clip[] = {device->viewport[0], device->viewport[1], device->viewport[2], device->viewport[3]}; gl_target_draw(device->target->target, vertices, count, indices, count, clip); gl_renderer_set_blend(0, 2, 1); @@ -1629,7 +1650,7 @@ static void vertex_buffer_lock(void) g_esp += 20; g_eax = 0x80070057u; if (!output) return; - if (flags & ~0x2831u) { fprintf(stderr, "Unsupported vertex buffer lock flags %08x\n", flags); exit(3); } + if (flags & ~0x3831u) { fprintf(stderr, "Unsupported vertex buffer lock flags %08x\n", flags); exit(3); } MEM32(output) = buffer->data; if (size) MEM32(size) = buffer->size; g_eax = 0; @@ -1740,6 +1761,7 @@ recomp_func_t linux_graphics_virtual(uint32_t address) if (current_interface == 2 && current_slot == 3) return enumerate_devices; if (current_interface == 2 && current_slot == 4) return create_device; if (current_interface == 2 && current_slot == 5) return create_vertex_buffer; + if (current_interface == 2 && current_slot == 7) return evict_managed_textures; if (current_interface == 1 && current_slot == 4) return create_clipper; if (current_interface == 1 && current_slot == 6) return create_surface; if (current_interface == 1 && current_slot == 8) return enumerate_display_modes; diff --git a/decompiled/tests/devices.c b/decompiled/tests/devices.c @@ -101,6 +101,9 @@ static void format_callback(void) static void textured_triangles(uint32_t draw, uint32_t device, uint32_t target, FILE *file) { uint32_t desc = workspace() + 128, input = workspace() + 512; + const uint32_t iid[] = {0xf5049e77, 0x11d24861, 0xa00007a4, 0xa82906c9}; + uint32_t d3d = query(draw, iid); + assert(!invoke(d3d, 7, 1)); #ifdef _WIN32 uint32_t callback = (uintptr_t)format_callback; #else @@ -144,6 +147,7 @@ static void textured_triangles(uint32_t draw, uint32_t device, uint32_t target, assert(*memory(desc + 108) == (managed ? 0x10 : 0)); for (unsigned y = 0; y < 4; ++y) memcpy(memory(pixels + y * pitch), texels + y * 4, 16); assert(!invoke(texture, 32, 2, 0u)); + assert(!invoke(d3d, 7, 1) && !invoke(d3d, 7, 1)); assert(!invoke(texture, 25, 5, 0u, desc, 0x11u, 0u)); pixels = *memory(desc + 36); pitch = *memory(desc + 16); for (unsigned y = 0; y < 4; ++y) assert(!memcmp(memory(pixels + y * pitch), texels + y * 4, 16)); @@ -182,6 +186,7 @@ static void textured_triangles(uint32_t draw, uint32_t device, uint32_t target, assert(!invoke(device, 36, 4, 0u, 14u, workspace()) && *memory(workspace()) == address); assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u)); + assert(!invoke(d3d, 7, 1)); assert(!invoke(device, 5, 1)); assert(!invoke(device, 25, 6, 4u, 0x144u, input, 3u, 0u)); assert(!invoke(device, 6, 1)); @@ -202,6 +207,7 @@ static void textured_triangles(uint32_t draw, uint32_t device, uint32_t target, assert(!invoke(device, 35, 3, 0u, 0u)); assert(!invoke(device, 34, 3, 0u, workspace()) && !*memory(workspace())); } + invoke(d3d, 2, 1); } static void initial_device_states(uint32_t device) @@ -247,10 +253,11 @@ static void rectangle_clears_preserve_outside_depth(uint32_t device, uint32_t ta }; memcpy(memory(input), vertices, sizeof(vertices)); assert(!invoke(device, 20, 3, 27u, 0u)); - for (unsigned count = 1; count <= 2; ++count) { + for (unsigned flags = 1; flags <= 3; ++flags) for (unsigned count = 0; count <= 2; ++count) { assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u)); - assert(!invoke(device, 10, 7, count, rectangles + (count == 2 ? 16 : 0), 3u, 0xff335577u, 0u, 0u)); + assert(!invoke(device, 10, 7, count, count ? rectangles + (count == 2 ? 16 : 0) : 0, + flags, 0xff335577u, 0u, 0u)); assert(!invoke(device, 5, 1)); assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u)); assert(!invoke(device, 6, 1)); @@ -258,9 +265,11 @@ static void rectangle_clears_preserve_outside_depth(uint32_t device, uint32_t ta assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u)); uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16); - assert((*memory(pixels) & 0xffffff) == 0xffffff); - assert((*memory(pixels + 6 * pitch + 6 * 4) & 0xffffff) == 0x335577); - assert((*memory(pixels + 31 * pitch + 31 * 4) & 0xffffff) == 0x102030); + uint32_t inside = flags == 1 ? 0xffffff : flags == 2 ? 0x102030 : 0x335577; + assert((*memory(pixels) & 0xffffff) == (count ? 0xffffff : inside)); + assert((*memory(pixels + 6 * pitch + 6 * 4) & 0xffffff) == inside); + assert((*memory(pixels + 31 * pitch + 31 * 4) & 0xffffff) == + (!count && (flags & 1) ? 0x335577 : 0x102030)); unsigned char rgb[32 * 32 * 3]; for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) { uint32_t pixel = *memory(pixels + y * pitch + x * 4); @@ -273,6 +282,97 @@ static void rectangle_clears_preserve_outside_depth(uint32_t device, uint32_t ta } } +static void depth_write_controls_occlusion(uint32_t device, uint32_t target) +{ + for (unsigned enabled = 0; enabled <= 1; ++enabled) { + uint32_t input = workspace() + 512, desc = workspace() + 128; + struct vertex { float x, y, z, rhw; uint32_t color; } vertices[] = { + {0, 0, .25f, 1, 0xffff0000}, {32, 0, .25f, 1, 0xffff0000}, {0, 32, .25f, 1, 0xffff0000} + }; + assert(!invoke(device, 20, 3, 14u, enabled)); + assert(!invoke(device, 21, 3, 14u, workspace()) && *memory(workspace()) == enabled); + assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u)); + memcpy(memory(input), vertices, sizeof(vertices)); + + assert(!invoke(device, 5, 1)); + assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u)); + assert(!invoke(device, 20, 3, 14u, 1u)); + for (unsigned i = 0; i < 3; ++i) { vertices[i].z = .75f; vertices[i].color = 0xff0000ff; } + memcpy(memory(input), vertices, sizeof(vertices)); + assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u)); + assert(!invoke(device, 6, 1)); + + *memory(desc) = 124; + assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u)); + uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16); + assert((*memory(pixels + 6 * pitch + 6 * 4) & 0xffffff) == (enabled ? 0xff0000 : 0x0000ff)); + assert((*memory(pixels + 31 * pitch + 31 * 4) & 0xffffff) == 0x102030); + assert(!invoke(target, 32, 2, 0u)); + } +} + +static void depth_comparisons_control_visibility(uint32_t device, uint32_t target) +{ + const unsigned visible[8][3] = {{0,0,0}, {1,0,0}, {0,1,0}, {1,1,0}, + {0,0,1}, {1,0,1}, {0,1,1}, {1,1,1}}; + const float depths[] = {.25f, .5f, .75f}; + uint32_t input = workspace() + 512, desc = workspace() + 128; + struct vertex { float x, y, z, rhw; uint32_t color; } vertices[] = { + {0, 0, 0, 1, 0xff00ff00}, {32, 0, 0, 1, 0xff00ff00}, {0, 32, 0, 1, 0xff00ff00} + }; + for (unsigned function = 1; function <= 8; ++function) for (unsigned z = 0; z < 3; ++z) { + assert(!invoke(device, 20, 3, 23u, function)); + assert(!invoke(device, 21, 3, 23u, workspace()) && *memory(workspace()) == function); + assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f000000u, 0u)); + for (unsigned i = 0; i < 3; ++i) vertices[i].z = depths[z]; + memcpy(memory(input), vertices, sizeof(vertices)); + assert(!invoke(device, 5, 1)); + assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u)); + assert(!invoke(device, 6, 1)); + + *memory(desc) = 124; + assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u)); + uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16); + assert((*memory(pixels + 6 * pitch + 6 * 4) & 0xffffff) == + (visible[function - 1][z] ? 0x00ff00 : 0x102030)); + assert(!invoke(target, 32, 2, 0u)); + } + assert(!invoke(device, 20, 3, 23u, 4u)); +} + +static void transformed_render_states_preserve_rasterization(uint32_t device, uint32_t target) +{ + uint32_t input = workspace() + 512, desc = workspace() + 128; + struct vertex { float x, y, z, rhw; uint32_t color; } vertices[] = { + {-16, -16, 0, 1, 0xff00ff00}, {48, -16, 0, 1, 0xff00ff00}, {-16, 48, 0, 1, 0xff00ff00} + }; + const float depths[] = {-.25f, .5f, 1.25f}; + assert(!invoke(device, 20, 3, 23u, 8u)); + for (unsigned lighting = 0; lighting <= 1; ++lighting) + for (unsigned clipping = 0; clipping <= 1; ++clipping) for (unsigned z = 0; z < 3; ++z) { + assert(!invoke(device, 20, 3, 136u, clipping)); + assert(!invoke(device, 21, 3, 136u, workspace()) && *memory(workspace()) == clipping); + assert(!invoke(device, 20, 3, 137u, lighting)); + assert(!invoke(device, 21, 3, 137u, workspace()) && *memory(workspace()) == lighting); + assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u)); + for (unsigned i = 0; i < 3; ++i) vertices[i].z = depths[z]; + memcpy(memory(input), vertices, sizeof(vertices)); + assert(!invoke(device, 5, 1)); + assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u)); + assert(!invoke(device, 6, 1)); + + *memory(desc) = 124; + assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u)); + uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16); + uint32_t pixel = *memory(pixels + 6 * pitch + 6 * 4) & 0xffffff; + assert(pixel == (z == 1 ? 0x00ff00 : 0x102030)); + assert((*memory(pixels + 31 * pitch + 31 * 4) & 0xffffff) == 0x102030); + assert(!invoke(target, 32, 2, 0u)); + } + assert(!invoke(device, 20, 3, 136u, 1u)); + assert(!invoke(device, 20, 3, 23u, 4u)); +} + static void viewport_limits_clearing_and_drawing(uint32_t device, uint32_t target, FILE *file) { uint32_t view = workspace() + 64, desc = workspace() + 128, input = workspace() + 512; @@ -373,6 +473,15 @@ static void vertex_buffers_preserve_data_and_description(uint32_t d3d) for (unsigned n = 0; n < size; ++n) assert(((unsigned char *)memory(data))[n] == ((n * 37) & 255)); assert(!invoke(buffer, 4, 1)); + assert(!invoke(buffer, 3, 4, 0x1020u, workspace(), 0u)); + data = *memory(workspace()); + ((unsigned char *)memory(data))[17] = 0x5a; + assert(!invoke(buffer, 4, 1)); + assert(!invoke(buffer, 3, 4, 0x11u, workspace(), 0u)); + data = *memory(workspace()); + for (unsigned n = 0; n < size; ++n) + assert(((unsigned char *)memory(data))[n] == (n == 17 ? 0x5a : ((n * 37) & 255))); + assert(!invoke(buffer, 4, 1)); assert(!invoke(buffer, 4, 1)); memset(memory(desc), 0xa5, 20); *memory(desc) = 0; @@ -515,6 +624,9 @@ static void device_lifetime_and_rendering(const char *pixels_path) assert(fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb)); textured_triangles(draw, device, color, file); rectangle_clears_preserve_outside_depth(device, color, file); + depth_write_controls_occlusion(device, color); + depth_comparisons_control_visibility(device, color); + transformed_render_states_preserve_rasterization(device, color); viewport_limits_clearing_and_drawing(device, color, file); assert(!fclose(file)); assert(!invoke(device, 2, 1)); diff --git a/decompiled/tests/graphics.c b/decompiled/tests/graphics.c @@ -732,6 +732,30 @@ static void presentation_flips_guest_rows_into_window_coordinates(void) gl_target_destroy(target); } +static void eviction_releases_gpu_storage_and_restores_pixels(void) +{ + struct gl_target *texture = gl_target_create(2, 2); + const unsigned char input[] = {1,2,3,4, 5,6,7,8, 9,10,11,12, 13,14,15,16}; + unsigned char output[16]; + gl_target_set_depth(texture, NULL); + gl_target_write(texture, input); + GLint name; + glGetIntegerv(GL_TEXTURE_BINDING_2D, &name); + assert(glIsTexture(name)); + + gl_target_evict(texture); + gl_target_evict(texture); + + assert(!glIsTexture(name)); + gl_target_read(texture, output); + assert(!memcmp(output, input, 16)); + glGetIntegerv(GL_TEXTURE_BINDING_2D, &name); + assert(glIsTexture(name)); + gl_target_evict(texture); + gl_target_destroy(texture); + assert(!glIsTexture(name) && glGetError() == GL_NO_ERROR); +} + static void native_context_renders_and_reads_pixels(void) { assert(SDL_GL_GetCurrentContext()); @@ -774,6 +798,7 @@ int main(void) uploads_preserve_depth_and_top_down_pixels(); attached_depth_is_shared_and_retained(); presentation_flips_guest_rows_into_window_coordinates(); + eviction_releases_gpu_storage_and_restores_pixels(); native_context_renders_and_reads_pixels(); #endif puts("DirectDraw: adapter/mode callbacks, reentrancy, capabilities, DirectDrawCreateEx, windowed mode, device information, DirectDraw/Direct3D interface lifetime and ABI passed"); diff --git a/tools/reconstruction/test_portable_devices.py b/tools/reconstruction/test_portable_devices.py @@ -25,8 +25,8 @@ actual = subprocess.check_output(wsl + ["env", "GALLIUM_DRIVER=d3d12", linux_wor assert actual == expected, (expected, actual) windows_pixels = (work / "reference.rgb").read_bytes() linux_pixels = (work / "native.rgb").read_bytes() -assert len(windows_pixels) == len(linux_pixels) == 522240 +assert len(windows_pixels) == len(linux_pixels) == 543744 error = max(abs(a - b) for a, b in zip(windows_pixels, linux_pixels)) assert error <= 1, error print("Windows == Linux: " + actual.strip()) -print("All 174080 pixels agree within one 8-bit channel step; maximum error:", error) +print("All 181248 pixels agree within one 8-bit channel step; maximum error:", error)