commit 155c56d722426241382a79150bfbe606077c2cee
parent 5669c87c32fb144616ab589aea2b9d78e20a56e1
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 04:07:04 +0000
Add native window presentation and viewport clipping
Diffstat:
7 files changed, 258 insertions(+), 28 deletions(-)
diff --git a/decompiled/runtime/gl_renderer.c b/decompiled/runtime/gl_renderer.c
@@ -209,11 +209,12 @@ 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)
+ unsigned vertex_count, const uint16_t *indices, unsigned index_count, const int32_t clip[4])
{
glBindFramebuffer(GL_FRAMEBUFFER, target->framebuffer);
glViewport(0, 0, target->width, target->height);
- glDisable(GL_SCISSOR_TEST);
+ glEnable(GL_SCISSOR_TEST);
+ glScissor(clip[0], clip[1], clip[2], clip[3]);
glDisable(GL_CULL_FACE);
if (target->depth) glEnable(GL_DEPTH_TEST);
else glDisable(GL_DEPTH_TEST);
@@ -227,6 +228,7 @@ void gl_target_draw(struct gl_target *target, const struct gl_vertex *vertices,
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_count * sizeof(*indices), indices, GL_STREAM_DRAW);
glDrawElements(GL_TRIANGLES, index_count, GL_UNSIGNED_SHORT, NULL);
+ glDisable(GL_SCISSOR_TEST);
glBindVertexArray(0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
check_graphics("indexed triangles");
@@ -262,6 +264,18 @@ void gl_target_blit(struct gl_target *source, struct gl_target *target, const in
check_graphics("surface blit");
}
+void gl_target_present(struct gl_target *target, const int32_t rectangle[4], int width, int height)
+{
+ glBindFramebuffer(GL_READ_FRAMEBUFFER, target->framebuffer);
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
+ glDisable(GL_SCISSOR_TEST);
+ /* Guest rows run top-down; the native window framebuffer runs bottom-up. */
+ glBlitFramebuffer(rectangle[0], rectangle[1], rectangle[2], rectangle[3],
+ 0, height, width, 0, GL_COLOR_BUFFER_BIT, GL_NEAREST);
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ check_graphics("window presentation");
+}
+
void gl_renderer_set_texture(const struct gl_texture_state *state)
{
glUseProgram(program);
diff --git a/decompiled/runtime/gl_renderer.h b/decompiled/runtime/gl_renderer.h
@@ -24,11 +24,13 @@ 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]);
+/* 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);
+ unsigned vertex_count, const uint16_t *indices, unsigned index_count, const int32_t clip[4]);
void gl_target_read(struct gl_target *target, unsigned char *rgba);
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]);
+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);
diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c
@@ -4,6 +4,8 @@
#include "gl_renderer.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
+#include <errno.h>
+#include <time.h>
/* Native graphics resources never appear in guest COM objects or vtables. */
static SDL_Window *window;
@@ -41,7 +43,7 @@ static struct device_object {
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 material[17];
+ uint32_t material[17], viewport[6];
int scene;
struct draw_object *owner;
struct surface_object *target;
@@ -176,6 +178,30 @@ static void test_cooperative_level(void)
g_esp += 8;
}
+static void wait_for_vertical_blank(void)
+{
+ find_object(MEM32(g_esp + 4));
+ uint32_t flags = MEM32(g_esp + 8), event = MEM32(g_esp + 12);
+ g_esp += 16;
+ g_eax = 0x80070057u;
+ if (flags != 1 || event) return;
+ SDL_DisplayMode mode;
+ if (SDL_GetCurrentDisplayMode(SDL_GetWindowDisplayIndex(window), &mode) || mode.refresh_rate <= 0) {
+ fprintf(stderr, "Cannot determine native display refresh rate\n"); exit(3);
+ }
+ /* WSL X11 exposes no retrace sync. Pace a virtual refresh clock instead. */
+ struct timespec now;
+ if (clock_gettime(CLOCK_MONOTONIC, &now)) abort();
+ uint64_t period = 1000000000u / mode.refresh_rate;
+ uint64_t ticks = (uint64_t)now.tv_sec * 1000000000u + now.tv_nsec;
+ uint64_t next = ticks + period - ticks % period;
+ struct timespec deadline = {next / 1000000000u, next % 1000000000u};
+ int error;
+ do { error = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &deadline, NULL); } while (error == EINTR);
+ if (error) { fprintf(stderr, "Native refresh wait failed: %d\n", error); abort(); }
+ g_eax = 0;
+}
+
static void get_device_identifier(void)
{
find_object(MEM32(g_esp + 4));
@@ -1015,7 +1041,9 @@ static void surface_blit(void)
uint32_t target_rect = MEM32(g_esp + 8), source_address = MEM32(g_esp + 12);
uint32_t source_rect = MEM32(g_esp + 16), flags = MEM32(g_esp + 20), effects = MEM32(g_esp + 24);
g_esp += 28;
- if (!source_address || (flags & ~0x01000200u) || effects || target->clipper || (target->caps & 0x200)) {
+ int primary = (target->caps & 0x200) != 0;
+ if (!source_address || (flags & ~0x01000200u) || effects ||
+ (target->clipper && !primary) || (primary && !target->clipper)) {
fprintf(stderr, "Unsupported surface blit flags=%08x effects=%08x caps=%08x\n", flags, effects, target->caps); exit(3);
}
struct surface_object *source = find_surface(source_address);
@@ -1032,16 +1060,42 @@ static void surface_blit(void)
g_eax = 0x80070057u;
if (from[0] < 0 || from[1] < 0 || from[2] <= from[0] || from[3] <= from[1] ||
from[2] > (int32_t)source->width || from[3] > (int32_t)source->height ||
- to[0] < 0 || to[1] < 0 || to[2] <= to[0] || to[3] <= to[1] ||
- to[2] > (int32_t)target->width || to[3] > (int32_t)target->height) return;
- if (from[2] - from[0] != to[2] - to[0] || from[3] - from[1] != to[3] - to[1]) {
+ to[2] <= to[0] || to[3] <= to[1]) return;
+ if ((int64_t)from[2] - from[0] != (int64_t)to[2] - to[0] || (int64_t)from[3] - from[1] != (int64_t)to[3] - to[1]) {
fprintf(stderr, "Stretched surface blits are not implemented\n"); exit(3);
}
+ SDL_Window *native = NULL;
+ int32_t client[4];
+ if (primary) {
+ native = linux_window_native(target->clipper->window);
+ if (!native) { g_eax = 0x80004005u; return; }
+ if (SDL_GetWindowFlags(native) & (SDL_WINDOW_HIDDEN | SDL_WINDOW_MINIMIZED)) { g_eax = 0; return; }
+ int x, y, width, height;
+ SDL_GetWindowPosition(native, &x, &y);
+ SDL_GetWindowSize(native, &width, &height);
+ client[0] = x; client[1] = y; client[2] = x + width; client[3] = y + height;
+ int32_t clipped[] = {x < 0 ? 0 : x, y < 0 ? 0 : y,
+ client[2] > (int32_t)target->width ? (int32_t)target->width : client[2],
+ client[3] > (int32_t)target->height ? (int32_t)target->height : client[3]};
+ for (unsigned i = 0; i < 4; ++i)
+ if (i < 2 ? clipped[i] < to[i] : clipped[i] > to[i]) clipped[i] = to[i];
+ if (clipped[0] >= clipped[2] || clipped[1] >= clipped[3]) { g_eax = 0; return; }
+ for (unsigned i = 0; i < 4; ++i) {
+ from[i] += clipped[i] - to[i];
+ to[i] = clipped[i];
+ }
+ } else if (to[0] < 0 || to[1] < 0 || to[2] > (int32_t)target->width || to[3] > (int32_t)target->height) return;
gl_target_blit(source->target, target->target, from, to);
if (target->caps & 0x800) {
gl_target_read(target->target, (void *)ADDR(target->pixels));
swap_surface_channels(target);
}
+ if (primary) {
+ int width, height;
+ SDL_GL_GetDrawableSize(native, &width, &height);
+ gl_target_present(target->target, client, width, height);
+ SDL_GL_SwapWindow(native);
+ }
g_eax = 0;
}
@@ -1079,6 +1133,9 @@ static void create_device(void)
device->destination_blend = 1;
device->owner = owner;
device->target = surface;
+ device->viewport[2] = surface->width;
+ device->viewport[3] = surface->height;
+ device->viewport[5] = 0x3f800000;
++owner->references[1];
++surface->references[0];
gl_target_set_depth(surface->target, surface->attached ? surface->attached->depth : NULL);
@@ -1181,8 +1238,7 @@ static void device_viewport(void)
uint32_t output = MEM32(g_esp + 8);
g_eax = 0x80070057u;
if (output) {
- const uint32_t viewport[] = {0, 0, device->target->width, device->target->height, 0, 0x3f800000};
- memcpy((void *)ADDR(output), viewport, sizeof(viewport));
+ memcpy((void *)ADDR(output), device->viewport, sizeof(device->viewport));
g_eax = 0;
}
g_esp += 12;
@@ -1194,10 +1250,16 @@ static void device_set_viewport(void)
uint32_t input = MEM32(g_esp + 8);
g_eax = 0x80070057u;
if (input) {
- const uint32_t viewport[] = {0, 0, device->target->width, device->target->height, 0, 0x3f800000};
- if (memcmp((void *)ADDR(input), viewport, sizeof(viewport))) {
- fprintf(stderr, "Unsupported Direct3D viewport\n"); exit(3);
+ uint32_t viewport[6];
+ memcpy(viewport, (void *)ADDR(input), sizeof(viewport));
+ if (viewport[0] > device->target->width || viewport[1] > device->target->height ||
+ viewport[2] > device->target->width - viewport[0] || viewport[3] > device->target->height - viewport[1]) {
+ g_esp += 12; return;
+ }
+ if (viewport[4] || viewport[5] != 0x3f800000) {
+ fprintf(stderr, "Unsupported Direct3D viewport depth range\n"); exit(3);
}
+ memcpy(device->viewport, viewport, sizeof(viewport));
g_eax = 0;
}
g_esp += 12;
@@ -1227,13 +1289,17 @@ static void device_clear(void)
}
float z;
memcpy(&z, &depth, 4);
- if (!count) gl_target_clear(device->target->target, color, z);
+ 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);
for (uint32_t i = 0; i < count; ++i) {
int32_t rectangle[4];
memcpy(rectangle, (void *)ADDR(rectangles + i * 16), 16);
if (rectangle[2] < rectangle[0] || rectangle[3] < rectangle[1]) {
g_eax = 0x80070057u; return;
}
+ 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);
}
g_eax = 0;
@@ -1336,7 +1402,8 @@ 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_target_draw(device->target->target, vertices, count, indices, count);
+ 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);
gl_renderer_set_texture(NULL);
free(indices);
@@ -1540,6 +1607,7 @@ recomp_func_t linux_graphics_virtual(uint32_t address)
if (current_interface == 1 && current_slot == 12) return get_display_mode;
if (current_slot == 11) return get_caps;
if (current_slot == 20) return set_cooperative_level;
+ if (current_interface == 1 && current_slot == 22) return wait_for_vertical_blank;
if (current_interface == 1 && current_slot == 26) return test_cooperative_level;
if (current_interface == 1 && current_slot == 27) return get_device_identifier;
return unavailable_method;
diff --git a/decompiled/tests/devices.c b/decompiled/tests/devices.c
@@ -273,6 +273,59 @@ static void rectangle_clears_preserve_outside_depth(uint32_t device, uint32_t ta
}
}
+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;
+ uint32_t rectangle = workspace() + 96;
+ const uint32_t full[] = {0, 0, 32, 32, 0, 0x3f800000};
+ const uint32_t regions[][6] = {{0, 0, 1, 1, 0, 0x3f800000}, {7, 9, 12, 10, 0, 0x3f800000}};
+ struct vertex { float x, y, z, rhw; uint32_t color; } vertices[] = {
+ {0, 0, .5f, 1, 0xffffffff}, {32, 0, .5f, 1, 0xffffffff}, {0, 32, .5f, 1, 0xffffffff}
+ };
+ memcpy(memory(input), vertices, sizeof(vertices));
+ for (unsigned scenario = 0; scenario < 6; ++scenario) {
+ memcpy(memory(view), full, sizeof(full));
+ assert(!invoke(device, 13, 2, view));
+ assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
+ memcpy(memory(view), regions[scenario / 3], 24);
+ const int32_t clear[] = {0, 0, 32, 32};
+ memcpy(memory(rectangle), clear, sizeof(clear));
+
+ assert(!invoke(device, 13, 2, view));
+ memset(memory(desc), 0xa5, 28);
+ assert(!invoke(device, 15, 2, desc));
+ assert(!memcmp(memory(desc), regions[scenario / 3], 24) && *memory(desc + 24) == 0xa5a5a5a5);
+ assert(!invoke(device, 10, 7, scenario % 3 == 2 ? 1u : 0u,
+ scenario % 3 == 2 ? rectangle : 0u, 3u, 0xff335577u, 0x3f800000u, 0u));
+ if (scenario % 3 == 1) {
+ 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 + 31 * pitch + 31 * 4) & 0xffffff) == 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);
+ rgb[(y * 32 + x) * 3] = pixel >> 16;
+ rgb[(y * 32 + x) * 3 + 1] = pixel >> 8;
+ rgb[(y * 32 + x) * 3 + 2] = pixel;
+ }
+ assert(!invoke(target, 32, 2, 0u));
+ assert(fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb));
+ }
+ memcpy(memory(view), full, sizeof(full));
+ assert(!invoke(device, 13, 2, view));
+ *memory(view + 8) = 33;
+ assert(invoke(device, 13, 2, view) == 0x80070057u);
+ assert(invoke(device, 13, 2, 0u) == 0x80070057u);
+ assert(!invoke(device, 15, 2, desc));
+ assert(!memcmp(memory(desc), full, sizeof(full)));
+}
+
static void material_state_is_copied(uint32_t device)
{
uint32_t input = workspace() + 128, output = workspace() + 256;
@@ -418,6 +471,7 @@ 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);
+ viewport_limits_clearing_and_drawing(device, color, file);
assert(!fclose(file));
assert(!invoke(device, 2, 1));
assert(!invoke(draw, 2, 1));
diff --git a/decompiled/tests/graphics.c b/decompiled/tests/graphics.c
@@ -162,6 +162,32 @@ static void direct_draw_seven_creation_and_windowed_mode(void)
assert(create_ex(workspace() + 4, workspace(), 0) == 0x80070057u);
}
+static void vertical_blank_wait_preserves_guest_state(void)
+{
+ const uint32_t draw7[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797};
+ memcpy(memory(workspace() + 4), draw7, 16);
+ assert(!create_ex(workspace() + 4, workspace(), 0));
+ uint32_t object = *memory(workspace());
+ assert(!invoke(memory(*memory(object))[20], 3, object, 0, 8));
+ uint32_t wait = memory(*memory(object))[22];
+#ifndef _WIN32
+ SDL_DisplayMode mode;
+ assert(!SDL_GetCurrentDisplayMode(SDL_GetWindowDisplayIndex(SDL_GL_GetCurrentWindow()), &mode));
+ assert(mode.refresh_rate > 0);
+ uint64_t start = SDL_GetPerformanceCounter();
+#endif
+
+ for (unsigned i = 0; i < 12; ++i) assert(!invoke(wait, 3, object, 1, 0));
+
+#ifndef _WIN32
+ double elapsed = (double)(SDL_GetPerformanceCounter() - start) / SDL_GetPerformanceFrequency();
+ assert(elapsed >= 10.0 / mode.refresh_rate && elapsed < 5);
+ fprintf(stderr, "Native refresh clock: 12 waits in %.3f seconds at %d Hz\n", elapsed, mode.refresh_rate);
+#endif
+ assert(invoke(wait, 3, object, 0, 0) == 0x80070057u);
+ assert(!ref(object, 2));
+}
+
static void device_information_has_stable_identity_and_preserves_padding(void)
{
const uint32_t draw7[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797};
@@ -579,17 +605,17 @@ static void attached_depth_is_shared_and_retained(void)
const uint16_t indices[] = {0, 1, 2};
unsigned char pixels[32 * 32 * 4];
- gl_target_draw(first, vertices, 3, indices, 3);
+ gl_target_draw(first, vertices, 3, indices, 3, (const int32_t[]){0, 0, 32, 32});
gl_target_set_depth(first, NULL);
gl_target_destroy(first);
for (unsigned i = 0; i < 3; ++i) vertices[i].position[2] = 0.75f;
- gl_target_draw(second, vertices, 3, indices, 3);
+ gl_target_draw(second, vertices, 3, indices, 3, (const int32_t[]){0, 0, 32, 32});
gl_target_read(second, pixels);
unsigned center = (16 * 32 + 16) * 4;
assert(pixels[center] == 0 && pixels[center + 1] == 0 && pixels[center + 2] == 255);
gl_target_set_depth(second, NULL);
- gl_target_draw(second, vertices, 3, indices, 3);
+ gl_target_draw(second, vertices, 3, indices, 3, (const int32_t[]){0, 0, 32, 32});
gl_target_read(second, pixels);
assert(pixels[center] == 255 && pixels[center + 1] == 255 && pixels[center + 2] == 255);
gl_target_destroy(second);
@@ -611,7 +637,7 @@ static void uploads_preserve_depth_and_top_down_pixels(void)
unsigned char output[16];
gl_target_write(target, input);
- gl_target_draw(target, vertices, 3, indices, 3);
+ gl_target_draw(target, vertices, 3, indices, 3, (const int32_t[]){0, 0, 2, 2});
gl_target_read(target, output);
assert(!memcmp(input, output, sizeof(input)));
@@ -631,14 +657,14 @@ static void indexed_triangles_preserve_depth_and_target_contents(void)
unsigned char pixels[32 * 32 * 4];
gl_target_clear(first, 0xff102030u, 1.0f);
- gl_target_draw(first, vertices, 3, indices, 3);
+ gl_target_draw(first, vertices, 3, indices, 3, (const int32_t[]){0, 0, 32, 32});
gl_target_clear(second, 0xff405060u, 1.0f);
for (unsigned i = 0; i < 3; ++i) {
vertices[i].position[2] = 0.75f;
vertices[i].color[0] = 1;
vertices[i].color[1] = 0;
}
- gl_target_draw(first, vertices, 3, indices, 3);
+ gl_target_draw(first, vertices, 3, indices, 3, (const int32_t[]){0, 0, 32, 32});
gl_target_read(first, pixels);
const unsigned center = (16 * 32 + 16) * 4;
@@ -650,12 +676,12 @@ static void indexed_triangles_preserve_depth_and_target_contents(void)
gl_target_read(second, pixels);
assert(pixels[center] == 64 && pixels[center + 1] == 80 && pixels[center + 2] == 96 && pixels[center + 3] == 255);
gl_target_clear(first, 0xff102030u, 0.0f);
- gl_target_draw(first, vertices, 3, indices, 3);
+ gl_target_draw(first, vertices, 3, indices, 3, (const int32_t[]){0, 0, 32, 32});
gl_target_read(first, pixels);
assert(pixels[center] == 16 && pixels[center + 1] == 32 && pixels[center + 2] == 48);
gl_target_clear(first, 0xff102030u, 1.0f);
for (unsigned i = 0; i < 3; ++i) vertices[i].position[2] = -0.25f;
- gl_target_draw(first, vertices, 3, indices, 3);
+ gl_target_draw(first, vertices, 3, indices, 3, (const int32_t[]){0, 0, 32, 32});
gl_target_read(first, pixels);
assert(pixels[center] == 16 && pixels[center + 1] == 32 && pixels[center + 2] == 48);
struct gl_vertex gradient[] = {
@@ -664,17 +690,17 @@ static void indexed_triangles_preserve_depth_and_target_contents(void)
{{-1, 1, 0.25f, 1}, {0, 0, 1, 1}}
};
const uint16_t reversed[] = {0, 2, 1};
- gl_target_draw(first, gradient, 3, reversed, 3);
+ gl_target_draw(first, gradient, 3, reversed, 3, (const int32_t[]){0, 0, 32, 32});
gl_target_read(first, pixels);
unsigned sample = (24 * 32 + 8) * 4;
assert((pixels[sample] == 127 || pixels[sample] == 128) && pixels[sample + 1] == 68 && pixels[sample + 2] == 60);
for (unsigned i = 0; i < 4; ++i) gradient[1].position[i] *= 2;
- gl_target_draw(first, gradient, 3, reversed, 3);
+ gl_target_draw(first, gradient, 3, reversed, 3, (const int32_t[]){0, 0, 32, 32});
gl_target_read(first, pixels);
assert(pixels[sample] == 147 && pixels[sample + 1] == 39 && pixels[sample + 2] == 69);
gl_target_clear(first, 0xff102030u, 1.0f);
for (unsigned i = 0; i < 3; ++i) vertices[i].position[2] = 1.25f;
- gl_target_draw(first, vertices, 3, indices, 3);
+ gl_target_draw(first, vertices, 3, indices, 3, (const int32_t[]){0, 0, 32, 32});
gl_target_read(first, pixels);
assert(pixels[center] == 16 && pixels[center + 1] == 32 && pixels[center + 2] == 48);
gl_target_destroy(first);
@@ -682,6 +708,30 @@ static void indexed_triangles_preserve_depth_and_target_contents(void)
fprintf(stderr, "Native renderer: indexed triangles, depth, clipping, independent targets and top-down readback passed\n");
}
+static void presentation_flips_guest_rows_into_window_coordinates(void)
+{
+ struct gl_target *target = gl_target_create(4, 4);
+ unsigned char input[64] = {0}, output[32 * 32 * 4];
+ const int32_t rectangle[] = {1, 1, 3, 3};
+ input[(1 * 4 + 1) * 4] = 255;
+ input[(1 * 4 + 2) * 4 + 1] = 255;
+ input[(2 * 4 + 1) * 4 + 2] = 255;
+ input[(2 * 4 + 2) * 4] = 255;
+ input[(2 * 4 + 2) * 4 + 1] = 255;
+ gl_target_write(target, input);
+
+ gl_target_present(target, rectangle, 32, 32);
+ glReadBuffer(GL_BACK);
+ glReadPixels(0, 0, 32, 32, GL_RGBA, GL_UNSIGNED_BYTE, output);
+
+ assert(glGetError() == GL_NO_ERROR);
+ for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) {
+ unsigned source = ((y < 16 ? 2 : 1) * 4 + (x < 16 ? 1 : 2)) * 4;
+ assert(!memcmp(output + (y * 32 + x) * 4, input + source, 3));
+ }
+ gl_target_destroy(target);
+}
+
static void native_context_renders_and_reads_pixels(void)
{
assert(SDL_GL_GetCurrentContext());
@@ -711,6 +761,7 @@ int main(void)
#endif
adapter_enumeration_invokes_guest_callbacks();
direct_draw_seven_creation_and_windowed_mode();
+ vertical_blank_wait_preserves_guest_state();
device_information_has_stable_identity_and_preserves_padding();
capability_queries_respect_structure_sizes();
display_modes_describe_the_native_monitor();
@@ -722,6 +773,7 @@ int main(void)
indexed_triangles_preserve_depth_and_target_contents();
uploads_preserve_depth_and_top_down_pixels();
attached_depth_is_shared_and_retained();
+ presentation_flips_guest_rows_into_window_coordinates();
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/decompiled/tests/surfaces.c b/decompiled/tests/surfaces.c
@@ -393,7 +393,47 @@ static void window_clipper_regions(void)
*memory(filter + 8) = x + 30; *memory(filter + 12) = y + 40;
assert(!invoke(clipper, 3, 4, filter, output, size));
assert(!memcmp(memory(output + 16), memory(filter), 16));
+
+ uint32_t primary = create_surface(draw, 0x200), source = create_surface(draw, 0x840);
+ uint32_t desc = workspace() + 128, rectangle = workspace() + 80;
+ assert(!invoke(primary, 28, 2, clipper));
+ assert(!invoke(source, 25, 5, 0u, desc, 0x21u, 0u));
+ uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
+ for (unsigned row = 0; row < 16; ++row) for (unsigned column = 0; column < 32; ++column)
+ *memory(pixels + row * pitch + column * 4) = (row << 16) | (column << 8) | 0x55;
+ assert(!invoke(source, 32, 2, 0u));
+ *memory(desc) = 124;
+ assert(!invoke(primary, 25, 5, 0u, desc, 0x21u, 0u));
+ memset(memory(*memory(desc + 36)), 0, *memory(desc + 16) * *memory(desc + 8));
+ assert(!invoke(primary, 32, 2, 0u));
+ int32_t destination[] = {x - 8, y - 4, x + 24, y + 12};
+ memcpy(memory(rectangle), destination, 16);
+
+ assert(!invoke(primary, 5, 6, rectangle, source, 0u, 0x200u, 0u));
+ const int32_t outside[] = {INT32_MIN, 0, INT32_MIN + 32, 16};
+ memcpy(memory(rectangle), outside, sizeof(outside));
+ assert(!invoke(primary, 5, 6, rectangle, source, 0u, 0x200u, 0u));
+ *memory(desc) = 124;
+ assert(!invoke(primary, 25, 5, 0u, desc, 0x11u, 0u));
+ pixels = *memory(desc + 36); pitch = *memory(desc + 16);
+
+ for (int row = -4; row < 16; ++row) for (int column = -8; column < 32; ++column) {
+ uint32_t expected = 0;
+ if (row >= 0 && row < 12 && column >= 0 && column < 24)
+ expected = ((row + 4) << 16) | ((column + 8) << 8) | 0x55;
+ assert((*memory(pixels + (y + row) * pitch + (x + column) * 4) & 0xffffff) == expected);
+ }
+ assert(!invoke(primary, 32, 2, 0u));
SDL_HideWindow(native);
+ destination[0] = x; destination[1] = y; destination[2] = x + 32; destination[3] = y + 16;
+ memcpy(memory(rectangle), destination, 16);
+ assert(!invoke(primary, 5, 6, rectangle, source, 0u, 0x200u, 0u));
+ *memory(desc) = 124;
+ assert(!invoke(primary, 25, 5, 0u, desc, 0x11u, 0u));
+ pixels = *memory(desc + 36); pitch = *memory(desc + 16);
+ assert((*memory(pixels + y * pitch + x * 4) & 0xffffff) == 0x040855);
+ assert(!invoke(primary, 32, 2, 0u));
+ assert(!invoke(primary, 2, 1) && !invoke(source, 2, 1));
assert(window_call("DestroyWindow", 1, &handle));
uint32_t unregister[] = {atom, 1};
assert(window_call("UnregisterClassA", 2, unregister));
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) == 503808
+assert len(windows_pixels) == len(linux_pixels) == 522240
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 167936 pixels agree within one 8-bit channel step; maximum error:", error)
+print("All 174080 pixels agree within one 8-bit channel step; maximum error:", error)