mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-03 08:59:22 +08:00
gl: keep textures that WebGL refuses off used sampler units, so that such a draw lands (#27)
virglrenderer patch 0020: on a WebGL host a used sampler unit holds no texture rather than one attached to the draw framebuffer or of another base type than its sampler. unkernel-check.mjs --drm gains step t (test/gpu/feedback.c).
This commit is contained in:
@@ -57,7 +57,7 @@ renderbuffers, which cannot be sampled (patch 17).
|
||||
| `test/gl/kwgl-state.c` | the stale-state check run by `vtest-check.sh state` (surfaceless EGL clients taking turns) |
|
||||
| `js/core/gl.ts`, `gl-stream.ts` | the SDK's host side: `createKernel({ gl })`, `GlHost` (`vm.gl`), `StreamHost`; see unkernel |
|
||||
| `js/core/gl-gpu.ts` | virtio-gpu 3D: `VirglGpuBackend` (device side), `GpuRenderer` (`vm.gl.gpu`); see The DRM path |
|
||||
| `test/gl/drm-page.js`, `drm.html`, `test/gpu/prime.c`, `test/gpu/window-image.c`, `test/gpu/int-attrib.c`, `test/gpu/msaa.c` | `unkernel-check.mjs --drm`: the DRM path in headless Chromium |
|
||||
| `test/gl/drm-page.js`, `drm.html`, `test/gpu/prime.c`, `test/gpu/window-image.c`, `test/gpu/int-attrib.c`, `test/gpu/msaa.c`, `test/gpu/feedback.c` | `unkernel-check.mjs --drm`: the DRM path in headless Chromium |
|
||||
| `test/gl-gpu.test.ts` | `VirglGpuBackend` against a fake link: the snapshot blocker, contexts made again after a restore; `GpuRenderer` against fake renderers: a lost context, stale ids, new renderers (`pnpm test:gl-gpu`) |
|
||||
| `test/gl/debian-disk.mjs` | makes the prepared data disk once (Mesa and the test programs installed in Debian's upper layer) |
|
||||
| `test/gl/unkernel-check.mjs`, `unkernel.html`, `unkernel-page.js` | the whole path in unkernel in headless Chromium; see Testing |
|
||||
@@ -184,6 +184,41 @@ In order, each with its reason in the commit message:
|
||||
fatal one such call put the context in error for good (Firefox's window
|
||||
went blank). `GL_OUT_OF_MEMORY` still puts the context in error, as do
|
||||
vrend's own checks of the guest's commands.
|
||||
20. textures WebGL refuses kept off used units
|
||||
(`vrend_state.strict_texture_units`, an emscripten host): WebGL refuses
|
||||
a draw when a texture on the unit of a sampler the program uses is
|
||||
attached to the draw framebuffer ("Feedback loop formed between
|
||||
Framebuffer and active Texture") or is of another base type (float,
|
||||
int, uint) than the sampler ("Mismatch between texture format and
|
||||
sampler type"), even when the shader never samples it; GLES leaves only
|
||||
what such a sampler returns undefined. Firefox's WebRender does both,
|
||||
and Mesa binds its RGBA8 fallback texture to an integer sampler that
|
||||
has no complete texture; each such draw was dropped (patch 19). Such a
|
||||
unit, and a used sampler with no view (which kept whatever another
|
||||
program or sub context left on its unit), now hold no texture: WebGL
|
||||
samples an empty unit as an incomplete texture of the sampler's type
|
||||
and does not refuse it. A unit is a loop when a level its view may
|
||||
sample (the view's levels and the texture's base and max level) is
|
||||
attached; the shader info records the integer samplers
|
||||
(`samplers_sint_mask`, `samplers_uint_mask`). A stencil-only view is
|
||||
uint only where the host samples stencil (`feat_stencil_texturing`;
|
||||
not WebGL 2, where it returns depth, float). A unit is looked at again,
|
||||
and its real texture comes back, when its view or the program changes,
|
||||
when a surface of its view's texture is attached or detached, when
|
||||
another view of that texture changes the texture's base or max level,
|
||||
and after the GL blitter set them behind the cache (then unknown: the
|
||||
loop check takes every level); taking a view away marks its unit too.
|
||||
Step t (`test/gpu/feedback.c`): issue #27's three draws, a draw with an
|
||||
RGBA8I texture on a `sampler2D` unit and an RGBA8 one on an
|
||||
`isampler2D` unit, and two draws that sample after each case (A back
|
||||
on its unit once only the framebuffer changed; correctly typed
|
||||
textures on the same units). Before the patch (without the sampled
|
||||
draws) two calls were dropped and the loop's and the type draw's
|
||||
colours were wrong; without the
|
||||
dirtying on detach, the sampled draw after the loop read an empty unit
|
||||
(2026-11-24). Not handled: a shadow sampler without a depth texture
|
||||
(or the reverse), a blit within one image and a vertex buffer too
|
||||
small for a draw; WebGL drops such a call (patch 19).
|
||||
|
||||
## Single GL context
|
||||
|
||||
@@ -1182,6 +1217,7 @@ Results (2026-11-21, the series of Patches):
|
||||
| `unkernel-check.mjs --drm --skip c` (inline; `--worker --skip c,d`) with window images (g), `--snapshot` (s) both modes | all pass (2026-11-22); The DRM path |
|
||||
| `unkernel-check.mjs --drm --lose` (inline, `--worker`), `page-check.mjs --lose`, `test/wland/page-check.mjs --built --gpu`'s loss case | pass (2026-11-22); Context loss |
|
||||
| `unkernel-check.mjs --drm --skip c` (inline) with multisampling (m, patch 17) | all pass (2026-11-22) |
|
||||
| `unkernel-check.mjs --drm --skip c` (inline) with texture units (t, patch 20) | all 15 pass, no WebGL error (2026-11-24) |
|
||||
| `unkernel-check.mjs --drm --skip a,b,c,d,e,g,h` (step r) and `page-check.mjs --no-apt` (R), inline and worker mode | all pass (2026-11-22); Display size |
|
||||
|
||||
Workloads that need no window system of ours (so nothing waits on wland):
|
||||
|
||||
@@ -28,7 +28,7 @@ Run the row for what you changed, and no more.
|
||||
| Bundle and kernel patch (`bundle.mjs`, `kwdiff.mjs`, `kpatch.ts`, `kremap.ts`) | `KPATCH_UP=vendor/build/kernel/vmlinux32.wasm KPATCH_SMP=vendor/build/kernel-smp/vmlinux32.wasm pnpm test:kpatch` |
|
||||
| Page (`web/`) | `pnpm typecheck:web`; `node --test web/test/open.test.ts`; `pnpm build:web` |
|
||||
| Wayland desktop (`web/src/wayland.ts`, `web/src/wayland/`, `WaylandOutput.tsx`, the foot app, the kw layer's `wland`, the Debian image's `wlandr`) | `node test/wland/page-check.mjs` (the dev server on port 8092, headless Chromium, worker mode then inline, about 30 s; the relay started at boot idle and nothing of wland fetched, a plain `foot` from a login shell and a second one on the same link, the launcher's foot tile, a killed relay replaced; makes the prepared disk `vendor/build/wland/debian-wland.img` and its `.kwsparse.gz` once with `test/wland/debian-disk.mjs`, which needs the internet; delete both after a new Debian image or a change to its package list); `--cpus 0` for the page's default CPU count, `--only worker\|inline`; longer cases in the first mode: `--apps` (two windows, the window manager, the page resized, foot's resize border, the clipboard and wland's bridge to the page's), `--gtk` (l3afpad and where its menu opens), `--keys` (foot's keys, the page's shortcuts and focus), `--pacing` (commits per second of busy clients, every mode), `--all` (those four); `--gpu` (GL apps by GPU resource: guest GL's virtio-gpu, wland's linux-dmabuf, weston-simple-egl's pixels, a stale-frame check, foot beside a GL window, gtk4-widget-factory, a relay without `--dmabuf`, an on-demand relay that finds its render node at the dial, Suspend with a GL app, then `?wlgpu=0` and `?gpu=0`; about 3 min with `--only worker`; imports `vendor/build/wland/debian-wland-gl.img.kwsparse.gz`, made once by `debian-disk.mjs --gl`; with `--pacing` frame rates by resource and in software); `--chromium` (instead of the other cases, in each mode: Debian's `chromium` started plainly maps a window, from a login shell and with no Wayland variables (the hook `/etc/chromium.d/kw-wayland` then runs `wland`), and renders on the GPU: its window by GPU resource, no shm reads, its GPU process kept; imports `vendor/build/wland/debian-wland-chromium.img.kwsparse.gz`, 260 MiB, made once by `debian-disk.mjs --chromium`: chromium as `apt-get install chromium` takes it, recommends included, and Mesa's EGL (`libegl1 libegl-mesa0`, with `libgles2 libgbm1 mesa-utils`), about 3 min; delete both files to make them again; the run about 2 min); `--firefox` (instead of the other cases, worker mode on 4 CPUs with `mem=2048` unless `--only` or `--cpus` say otherwise: firefox-esr from a login shell maps its window on a local page whose title shows what reached it, then two clicks, typing, the context menu opened and closed with Escape, the wheel, a drag selection (`wl-paste -p`) and Ctrl+C (`wl-paste`), a link and a typed URL; needs a wland that sends seat events to every pointer and keyboard of a client (0.1.0-261122-632f589 or later); imports `vendor/build/wland/debian-wland-firefox.img.kwsparse.gz`, 218 MiB, made once by `debian-disk.mjs --firefox`: the GL disk's packages and firefox-esr, about 3 min; the run about 3 min); `--suspend` (Suspend and resume with foot open: the relay goes idle, a plain foot maps; and a session saved with `?shm=0`: no `kw.wayland=1`, no relay), `--fresh` (no prepared disk: the tile's apt path, needs the internet), `--built` (web/.output; also when another dev server runs on the same `web/`: two at once disturbed each other's dep optimizer, pages failed to load, 2026-11-22), `--output DIR` (another build's output, e.g. `web/.wland-local/output`: the page built against a local wland package, web.md, Wayland desktop), `--shots DIR`, `--keep-open` (prints the DevTools port), `--verbose` (the server's and Chromium's output). Chromium runs with `--disable-dev-shm-usage`: in a container whose `/dev/shm` is small (64 MiB here, 25 MiB of it another session's) the GL page crashed its renderer and then got `net::ERR_INSUFFICIENT_RESOURCES` for its scripts (2026-11-22) |
|
||||
| Guest GL (`gl/`, `js/core/gl*.ts`, the forwarder; gl.md, Testing) | `node gl/wasm/test/check.mjs` (the module); `test/gl/stream-check.sh info state` (the stream link); `node test/gl/unkernel-check.mjs` (unkernel in headless Chromium; makes the prepared disk once); the DRM path and the renderer loaded on first use: `node test/gl/unkernel-check.mjs --drm --skip c` (`--eager`, `--fail`: gl.md, Testing; step h: integer vertex attributes and colour buffers, `test/gpu/int-attrib.c`; step m: multisampling, `test/gpu/msaa.c`; `--lose`: glmark2-es2-drm twice, then the WebGL2 context lost four ways, restored or not, and new GL programs after each; `pnpm test:gl-gpu` without a browser); the page's GL option (`web/`, `kw-gl.sh`): `node test/gl/page-check.mjs` (the dev server on port 8091 with the default URL, where GL is on, Mesa from apt over the page's network: needs the internet; `--no-apt` without; `--suspend` adds the session checks; with the device it resizes the page and checks the guest's DRM mode, `--no-resize` skips that; `--inline` for `?worker=0`; `--server URL` reads the page from a server that runs instead of starting one) |
|
||||
| Guest GL (`gl/`, `js/core/gl*.ts`, the forwarder; gl.md, Testing) | `node gl/wasm/test/check.mjs` (the module); `test/gl/stream-check.sh info state` (the stream link); `node test/gl/unkernel-check.mjs` (unkernel in headless Chromium; makes the prepared disk once); the DRM path and the renderer loaded on first use: `node test/gl/unkernel-check.mjs --drm --skip c` (`--eager`, `--fail`: gl.md, Testing; step h: integer vertex attributes and colour buffers, `test/gpu/int-attrib.c`; step m: multisampling, `test/gpu/msaa.c`; step t: textures WebGL refuses left on used sampler units (a feedback loop, int and float on the other type's sampler), `test/gpu/feedback.c`; `--lose`: glmark2-es2-drm twice, then the WebGL2 context lost four ways, restored or not, and new GL programs after each; `pnpm test:gl-gpu` without a browser); the page's GL option (`web/`, `kw-gl.sh`): `node test/gl/page-check.mjs` (the dev server on port 8091 with the default URL, where GL is on, Mesa from apt over the page's network: needs the internet; `--no-apt` without; `--suspend` adds the session checks; with the device it resizes the page and checks the guest's DRM mode, `--no-resize` skips that; `--inline` for `?worker=0`; `--server URL` reads the page from a server that runs instead of starting one) |
|
||||
| Guest Vulkan (`vk/`, `js/core/vk*.ts`, `test/vk/`; vk.md, Testing) | `pnpm typecheck`; `pnpm test:vk`; `vk/build.sh` (the guest's ICD and the translator); `node test/vk/relay.mjs -- vulkaninfo --summary` and the native test programs (`vk/icd/build.sh native` first); `node test/vk/unkernel-check.mjs` and `--worker` (headless Chrome with WebGPU on SwiftShader; makes the prepared disk once; `--source` skips `pnpm build`); `--snapshot`, `--lose` (device loss), `--cpus 2`; real programs: `--apps --cmd '...'`; the host alone: `node test/vk/host/check.mjs`; the translator: `vk/naga/test/run.ts`; the page's option (`web/`): `node test/vk/page-check.mjs` |
|
||||
|
||||
The optional checks boot the same bundle as `pnpm test:api`, which already
|
||||
|
||||
@@ -0,0 +1,367 @@
|
||||
From 96fe7c60ef27a8aa8bbaaa5b4098d892b4bbd09d Mon Sep 17 00:00:00 2001
|
||||
From: Pooya Parsa <pooya@pi0.io>
|
||||
Date: Tue, 24 Nov 2026 18:26:01 +0000
|
||||
Subject: [PATCH] vrend: keep textures that WebGL refuses off used texture
|
||||
units
|
||||
|
||||
WebGL refuses a draw (GL_INVALID_OPERATION) when a texture on the unit of
|
||||
a sampler the program uses is attached to the draw framebuffer ("Feedback
|
||||
loop formed between Framebuffer and active Texture"), or is of another
|
||||
base type (float, signed or unsigned integer) than the sampler ("Mismatch
|
||||
between texture format and sampler type"), even when the shader never
|
||||
samples it in that draw. GLES leaves only what such a sampler returns
|
||||
undefined. Firefox's WebRender does both: it leaves an input texture on a
|
||||
unit and renders into it, and leaves an RGBA32I texture on the units of
|
||||
float samplers; Mesa itself binds its RGBA8 fallback texture to an integer
|
||||
sampler that has no complete texture. WebGL dropped each such draw.
|
||||
|
||||
On such a host (emscripten: vrend_state.strict_texture_units), bind no
|
||||
texture to such a unit instead: WebGL samples an empty unit as an
|
||||
incomplete texture of the sampler's type and does not refuse the draw.
|
||||
The shader info now records which samplers return integers. A
|
||||
stencil-only view counts as unsigned integer only where the host can
|
||||
sample stencil; without stencil texturing (GLES 3.0, WebGL 2) it still
|
||||
returns depth. A unit forms a loop when a level its view may sample (the
|
||||
view's levels and, without texture views, the texture's base and max
|
||||
level) is attached to the draw framebuffer.
|
||||
|
||||
A unit is looked at again, and its texture comes back, whenever its
|
||||
answer can change: as before when its view or the program changes, and
|
||||
now also when a surface of its view's texture is attached or detached,
|
||||
when another view of that texture changes the texture's base or max
|
||||
level, and when the GL blitter sets them behind the cache (which is then
|
||||
marked unknown, so the loop check takes every level). A used sampler
|
||||
with no view gets an empty unit too, instead of whatever another program
|
||||
or sub context left on its unit or on the unit its uniform last named,
|
||||
and taking a view away marks its unit for that.
|
||||
---
|
||||
src/vrend/vrend_renderer.c | 172 +++++++++++++++++++++++++++++++++++--
|
||||
src/vrend/vrend_shader.c | 14 +++
|
||||
src/vrend/vrend_shader.h | 4 +
|
||||
3 files changed, 184 insertions(+), 6 deletions(-)
|
||||
|
||||
diff --git a/src/vrend/vrend_renderer.c b/src/vrend/vrend_renderer.c
|
||||
index addcca9..4c0cfe4 100644
|
||||
--- a/src/vrend/vrend_renderer.c
|
||||
+++ b/src/vrend/vrend_renderer.c
|
||||
@@ -424,6 +424,11 @@ struct global_renderer_state {
|
||||
* are multisample renderbuffers, which can be rendered to, cleared and
|
||||
* blitted (resolved) from, but not sampled */
|
||||
bool ms_renderbuffers : 1;
|
||||
+ /* the host refuses a draw with a texture unit that GLES leaves
|
||||
+ * undefined (WebGL 2): a used unit holds no texture rather than one
|
||||
+ * attached to the draw framebuffer or of another base type than its
|
||||
+ * sampler */
|
||||
+ bool strict_texture_units : 1;
|
||||
|
||||
/* host GL errors that were not fatal (no CHECK_GL_ERRORS) */
|
||||
uint32_t host_gl_errors;
|
||||
@@ -484,6 +489,9 @@ struct vrend_linked_shader_program {
|
||||
uint32_t samplers_used_mask[PIPE_SHADER_TYPES];
|
||||
// a subset of samplers_used_mask
|
||||
uint32_t shadow_samp_mask[PIPE_SHADER_TYPES];
|
||||
+ // subsets of samplers_used_mask: integer samplers (isampler*, usampler*)
|
||||
+ uint32_t samplers_sint_mask[PIPE_SHADER_TYPES];
|
||||
+ uint32_t samplers_uint_mask[PIPE_SHADER_TYPES];
|
||||
|
||||
// compact arrays of uniform locations for each set bit in samplers_used_mask
|
||||
GLuint *sampler_locs[PIPE_SHADER_TYPES];
|
||||
@@ -1718,6 +1726,8 @@ static int bind_sampler_locs(struct vrend_linked_shader_program *sprog,
|
||||
}
|
||||
sprog->samplers_used_mask[shader_type] = sinfo->samplers_used_mask;
|
||||
sprog->shadow_samp_mask[shader_type] = sinfo->shadow_samp_mask;
|
||||
+ sprog->samplers_sint_mask[shader_type] = sinfo->samplers_sint_mask;
|
||||
+ sprog->samplers_uint_mask[shader_type] = sinfo->samplers_uint_mask;
|
||||
|
||||
return next_sampler_id;
|
||||
}
|
||||
@@ -3220,6 +3230,38 @@ static void vrend_hw_emit_framebuffer_state(struct vrend_sub_context *sub_ctx)
|
||||
glDrawBuffers(sub_ctx->nr_cbufs, buffers);
|
||||
}
|
||||
|
||||
+/* With strict texture units, whether a unit may hold its view's texture
|
||||
+ * depends on the draw framebuffer and on the texture's base and max level:
|
||||
+ * when either changes for a texture, look again at the next draw at the
|
||||
+ * units of every graphics stage whose view is of that texture. */
|
||||
+static void vrend_dirty_views_of_texture(struct vrend_sub_context *sub_ctx,
|
||||
+ const struct vrend_resource *res)
|
||||
+{
|
||||
+ if (!vrend_state.strict_texture_units || !res)
|
||||
+ return;
|
||||
+ for (int stage = PIPE_SHADER_VERTEX; stage < PIPE_SHADER_COMPUTE; stage++) {
|
||||
+ struct vrend_shader_view *shader_view = &sub_ctx->views[stage];
|
||||
+ int n = MIN2(shader_view->max_num_views, 32);
|
||||
+
|
||||
+ for (int i = 0; i < n; i++) {
|
||||
+ if (shader_view->views[i] && shader_view->views[i]->texture == res)
|
||||
+ shader_view->dirty_mask |= 1u << i;
|
||||
+ }
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+/* An attachment changes from old to new (either may be NULL): only the
|
||||
+ * units of their textures can change their answer. */
|
||||
+static void vrend_framebuffer_changed(struct vrend_sub_context *sub_ctx,
|
||||
+ const struct vrend_surface *old_surf,
|
||||
+ const struct vrend_surface *new_surf)
|
||||
+{
|
||||
+ if (old_surf)
|
||||
+ vrend_dirty_views_of_texture(sub_ctx, old_surf->texture);
|
||||
+ if (new_surf)
|
||||
+ vrend_dirty_views_of_texture(sub_ctx, new_surf->texture);
|
||||
+}
|
||||
+
|
||||
void vrend_set_framebuffer_state(struct vrend_context *ctx,
|
||||
uint32_t nr_cbufs, uint32_t surf_handle[PIPE_MAX_COLOR_BUFS],
|
||||
uint32_t zsurf_handle)
|
||||
@@ -3244,6 +3286,7 @@ void vrend_set_framebuffer_state(struct vrend_context *ctx,
|
||||
zsurf = NULL;
|
||||
|
||||
if (sub_ctx->zsurf != zsurf) {
|
||||
+ vrend_framebuffer_changed(sub_ctx, sub_ctx->zsurf, zsurf);
|
||||
vrend_surface_reference(&sub_ctx->zsurf, zsurf);
|
||||
vrend_hw_set_zsurf_texture(ctx);
|
||||
}
|
||||
@@ -3262,6 +3305,7 @@ void vrend_set_framebuffer_state(struct vrend_context *ctx,
|
||||
surf = NULL;
|
||||
|
||||
if (sub_ctx->surf[i] != surf) {
|
||||
+ vrend_framebuffer_changed(sub_ctx, sub_ctx->surf[i], surf);
|
||||
vrend_surface_reference(&sub_ctx->surf[i], surf);
|
||||
vrend_hw_set_color_surface(sub_ctx, i);
|
||||
}
|
||||
@@ -3269,6 +3313,7 @@ void vrend_set_framebuffer_state(struct vrend_context *ctx,
|
||||
|
||||
if (old_num > sub_ctx->nr_cbufs) {
|
||||
for (uint32_t i = sub_ctx->nr_cbufs; i < old_num; i++) {
|
||||
+ vrend_framebuffer_changed(sub_ctx, sub_ctx->surf[i], NULL);
|
||||
vrend_surface_reference(&sub_ctx->surf[i], NULL);
|
||||
vrend_hw_set_color_surface(sub_ctx, i);
|
||||
}
|
||||
@@ -3745,6 +3790,11 @@ void vrend_set_single_sampler_view(struct vrend_context *ctx,
|
||||
struct vrend_sampler_view *view = NULL;
|
||||
struct vrend_texture *tex;
|
||||
|
||||
+ /* a view taken away: with strict texture units its unit is emptied at
|
||||
+ * the next draw, rather than keep a texture that nothing looks at again */
|
||||
+ if (vrend_state.strict_texture_units && !handle && index < 32 && shader_view->views[index])
|
||||
+ shader_view->dirty_mask |= 1u << index;
|
||||
+
|
||||
if (handle) {
|
||||
view = vrend_object_lookup(ctx->sub->object_hash, handle, VIRGL_OBJECT_SAMPLER_VIEW);
|
||||
if (!view) {
|
||||
@@ -3789,6 +3839,12 @@ void vrend_set_single_sampler_view(struct vrend_context *ctx,
|
||||
|
||||
view->levels = view->u.tex.last_level - view->u.tex.first_level + 1;
|
||||
|
||||
+ if (tex->cur_base != view->u.tex.first_level ||
|
||||
+ tex->cur_max != view->u.tex.last_level) {
|
||||
+ /* the units of other views of this texture sample the new
|
||||
+ * range too: their loop check is redone */
|
||||
+ vrend_dirty_views_of_texture(ctx->sub, view->texture);
|
||||
+ }
|
||||
if (tex->cur_base != view->u.tex.first_level) {
|
||||
glTexParameteri(view->texture->target, GL_TEXTURE_BASE_LEVEL, view->u.tex.first_level);
|
||||
tex->cur_base = view->u.tex.first_level;
|
||||
@@ -3860,8 +3916,12 @@ void vrend_set_num_sampler_views(struct vrend_context *ctx,
|
||||
int i;
|
||||
|
||||
struct vrend_shader_view *shader_view = &ctx->sub->views[shader_type];
|
||||
- for (i = last_slot; i < shader_view->max_num_views; i++)
|
||||
+ for (i = last_slot; i < shader_view->max_num_views; i++) {
|
||||
+ /* as in vrend_set_single_sampler_view: empty the unit at the next draw */
|
||||
+ if (vrend_state.strict_texture_units && i < 32 && shader_view->views[i])
|
||||
+ shader_view->dirty_mask |= 1u << i;
|
||||
vrend_sampler_view_reference(&shader_view->views[i], NULL);
|
||||
+ }
|
||||
|
||||
shader_view->max_num_views = last_slot;
|
||||
}
|
||||
@@ -5326,6 +5386,67 @@ static void vrend_draw_bind_vertex_binding(struct vrend_context *ctx,
|
||||
}
|
||||
}
|
||||
|
||||
+/* Whether a level of the view's texture that the unit may sample is
|
||||
+ * attached to the draw framebuffer (a feedback loop, which WebGL refuses
|
||||
+ * even when the shader never samples the unit). */
|
||||
+static bool vrend_view_in_framebuffer(const struct vrend_sub_context *sub_ctx,
|
||||
+ const struct vrend_sampler_view *view)
|
||||
+{
|
||||
+ const struct vrend_resource *res = view->texture;
|
||||
+ GLuint first = view->u.tex.first_level, last = view->u.tex.last_level;
|
||||
+
|
||||
+ /* without a texture view the texture's base and max level apply, which
|
||||
+ * the last view set of this texture chose: take both ranges */
|
||||
+ if (view->gl_id == res->gl_id) {
|
||||
+ const struct vrend_texture *tex = (const struct vrend_texture *)res;
|
||||
+
|
||||
+ first = tex->cur_base == (GLuint)-1 ? 0 : MIN2(first, tex->cur_base);
|
||||
+ last = MAX2(last, tex->cur_max);
|
||||
+ }
|
||||
+
|
||||
+ for (uint32_t i = 0; i < sub_ctx->nr_cbufs; i++) {
|
||||
+ const struct vrend_surface *surf = sub_ctx->surf[i];
|
||||
+
|
||||
+ if (surf && surf->texture == res && surf->level >= first && surf->level <= last)
|
||||
+ return true;
|
||||
+ }
|
||||
+ return sub_ctx->zsurf && sub_ctx->zsurf->texture == res &&
|
||||
+ sub_ctx->zsurf->level >= first && sub_ctx->zsurf->level <= last;
|
||||
+}
|
||||
+
|
||||
+/* Whether a strict host (WebGL) would refuse a draw with the view's texture
|
||||
+ * on the unit of sampler i: the texture is of another base type (float,
|
||||
+ * signed or unsigned integer) than the sampler, or it is attached to the
|
||||
+ * draw framebuffer. GLES leaves what such a sampler returns undefined. */
|
||||
+static bool vrend_texture_unit_refused(const struct vrend_sub_context *sub_ctx,
|
||||
+ int shader_type, int i,
|
||||
+ const struct vrend_sampler_view *view)
|
||||
+{
|
||||
+ const struct vrend_linked_shader_program *sprog = sub_ctx->prog;
|
||||
+ bool samp_sint = sprog->samplers_sint_mask[shader_type] & (1u << i);
|
||||
+ bool samp_uint = sprog->samplers_uint_mask[shader_type] & (1u << i);
|
||||
+ /* what the unit samples: the texture's own storage when the view has no
|
||||
+ * texture of its own; a depth/stencil texture returns floats (depth),
|
||||
+ * or unsigned integers for a stencil-only view where the host can
|
||||
+ * sample stencil (not GLES 3.0, WebGL 2: there it still returns depth) */
|
||||
+ enum virgl_formats format = view->gl_id == view->texture->gl_id ?
|
||||
+ (enum virgl_formats)view->texture->base.format : view->format;
|
||||
+ bool view_sint, view_uint;
|
||||
+
|
||||
+ if (util_format_is_depth_or_stencil(view->format)) {
|
||||
+ view_sint = false;
|
||||
+ view_uint = has_feature(feat_stencil_texturing) &&
|
||||
+ !util_format_has_depth(util_format_description(view->format));
|
||||
+ } else {
|
||||
+ view_sint = util_format_is_pure_sint(format);
|
||||
+ view_uint = util_format_is_pure_uint(format);
|
||||
+ }
|
||||
+ if (view_sint != samp_sint || view_uint != samp_uint)
|
||||
+ return true;
|
||||
+
|
||||
+ return shader_type != PIPE_SHADER_COMPUTE && vrend_view_in_framebuffer(sub_ctx, view);
|
||||
+}
|
||||
+
|
||||
static GLuint vrend_draw_bind_samplers_shader(struct vrend_sub_context *sub_ctx,
|
||||
int shader_type,
|
||||
GLuint next_sampler_id)
|
||||
@@ -5341,7 +5462,19 @@ static GLuint vrend_draw_bind_samplers_shader(struct vrend_sub_context *sub_ctx,
|
||||
int i = u_bit_scan(&mask);
|
||||
struct vrend_sampler_view *tview = shader_view->views[i];
|
||||
|
||||
- if ((dirty & (1 << i)) && tview) {
|
||||
+ if ((dirty & (1 << i)) && !tview && vrend_state.strict_texture_units) {
|
||||
+ /* a used sampler with no view samples an empty unit, not what
|
||||
+ * another program or sub context left on it (on the one GL
|
||||
+ * context), nor unit 0 by the uniform's default */
|
||||
+ static const GLenum targets[] = {
|
||||
+ GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_2D_ARRAY
|
||||
+ };
|
||||
+
|
||||
+ glActiveTexture(GL_TEXTURE0 + next_sampler_id);
|
||||
+ glUniform1i(sprog->sampler_locs[shader_type][sampler_index], next_sampler_id);
|
||||
+ for (unsigned t = 0; t < ARRAY_SIZE(targets); t++)
|
||||
+ glBindTexture(targets[t], 0);
|
||||
+ } else if ((dirty & (1 << i)) && tview) {
|
||||
glActiveTexture(GL_TEXTURE0 + next_sampler_id);
|
||||
glUniform1i(sprog->sampler_locs[shader_type][sampler_index], next_sampler_id);
|
||||
|
||||
@@ -5393,10 +5526,19 @@ static GLuint vrend_draw_bind_samplers_shader(struct vrend_sub_context *sub_ctx,
|
||||
target = GL_TEXTURE_BUFFER;
|
||||
}
|
||||
|
||||
- glBindTexture(target, id);
|
||||
- vrend_apply_sampler_state(sub_ctx, tview->texture,
|
||||
- shader_view->samplers[i],
|
||||
- next_sampler_id, tview);
|
||||
+ if (vrend_state.strict_texture_units && target != GL_TEXTURE_BUFFER &&
|
||||
+ vrend_texture_unit_refused(sub_ctx, shader_type, i, tview)) {
|
||||
+ /* no texture: an empty unit is never refused (WebGL samples
|
||||
+ * an incomplete texture of the sampler's type), and the
|
||||
+ * unit is looked at again when the view or the framebuffer
|
||||
+ * changes */
|
||||
+ glBindTexture(target, 0);
|
||||
+ } else {
|
||||
+ glBindTexture(target, id);
|
||||
+ vrend_apply_sampler_state(sub_ctx, tview->texture,
|
||||
+ shader_view->samplers[i],
|
||||
+ next_sampler_id, tview);
|
||||
+ }
|
||||
|
||||
if (vrend_state.use_gles) {
|
||||
const unsigned levels = tview->levels ? tview->levels : tview->texture->base.last_level + 1u;
|
||||
@@ -7889,6 +8031,13 @@ int vrend_renderer_init(const struct vrend_if_cbs *cbs, uint32_t flags)
|
||||
max_samples);
|
||||
}
|
||||
|
||||
+ /* WebGL refuses a draw with a texture both on a used unit and attached
|
||||
+ * to the draw framebuffer, or of another base type than its sampler,
|
||||
+ * even when the shader never samples it */
|
||||
+#ifdef __EMSCRIPTEN__
|
||||
+ vrend_state.strict_texture_units = true;
|
||||
+#endif
|
||||
+
|
||||
/* disable for format testing */
|
||||
if (has_feature(feat_debug_cb)) {
|
||||
glEnable(GL_DEBUG_OUTPUT);
|
||||
@@ -11493,6 +11642,17 @@ static void vrend_renderer_blit_int(struct vrend_context *ctx,
|
||||
VREND_DEBUG(dbg_blit, ctx, "BLIT_INT: use GL fallback\n");
|
||||
if (resolved || !has_bit(src_res->storage_bits, VREND_STORAGE_GL_RENDERBUFFER)) {
|
||||
vrend_renderer_blit_gl(ctx, resolved ? resolved : src_res, dst_res, &blit_info);
|
||||
+ if (vrend_state.strict_texture_units && !resolved &&
|
||||
+ has_bit(src_res->storage_bits, VREND_STORAGE_GL_TEXTURE)) {
|
||||
+ /* it set the source's base and max level to the blitted level
|
||||
+ * without telling the cache: unknown, so the loop check takes
|
||||
+ * every level and the next view sets them again */
|
||||
+ struct vrend_texture *tex = (struct vrend_texture *)src_res;
|
||||
+
|
||||
+ tex->cur_base = -1;
|
||||
+ tex->cur_max = 10000;
|
||||
+ vrend_dirty_views_of_texture(ctx->sub, src_res);
|
||||
+ }
|
||||
if (vrend_state.single_gl_context)
|
||||
vrend_single_acquire(ctx->sub);
|
||||
else
|
||||
diff --git a/src/vrend/vrend_shader.c b/src/vrend/vrend_shader.c
|
||||
index 2b4a07d..425dba7 100644
|
||||
--- a/src/vrend/vrend_shader.c
|
||||
+++ b/src/vrend/vrend_shader.c
|
||||
@@ -7931,6 +7931,20 @@ static void fill_sinfo(const struct dump_ctx *ctx, struct vrend_shader_info *sin
|
||||
sinfo->num_inputs = ctx->num_inputs;
|
||||
sinfo->num_outputs = ctx->num_outputs;
|
||||
sinfo->shadow_samp_mask = ctx->shadow_samp_mask;
|
||||
+ sinfo->samplers_sint_mask = 0;
|
||||
+ sinfo->samplers_uint_mask = 0;
|
||||
+ for (uint32_t mask = ctx->samplers_used; mask; ) {
|
||||
+ int i = u_bit_scan(&mask);
|
||||
+ /* the type the declaration has: an array's is its first sampler's */
|
||||
+ int first = (ctx->info.indirect_files & (1 << TGSI_FILE_SAMPLER)) ?
|
||||
+ lookup_sampler_array(ctx, i) : -1;
|
||||
+ enum tgsi_return_type type = ctx->samplers[first >= 0 ? first : i].tgsi_sampler_return;
|
||||
+
|
||||
+ if (type == TGSI_RETURN_TYPE_SINT)
|
||||
+ sinfo->samplers_sint_mask |= 1u << i;
|
||||
+ else if (type == TGSI_RETURN_TYPE_UINT)
|
||||
+ sinfo->samplers_uint_mask |= 1u << i;
|
||||
+ }
|
||||
sinfo->gs_out_prim = ctx->gs_out_prim;
|
||||
sinfo->tes_prim = ctx->tes_prim_mode;
|
||||
sinfo->tes_point_mode = ctx->tes_point_mode;
|
||||
diff --git a/src/vrend/vrend_shader.h b/src/vrend/vrend_shader.h
|
||||
index 7f5897b..fb2a8b0 100644
|
||||
--- a/src/vrend/vrend_shader.h
|
||||
+++ b/src/vrend/vrend_shader.h
|
||||
@@ -142,6 +142,10 @@ struct vrend_shader_info {
|
||||
uint32_t ssbo_binding_offset;
|
||||
int32_t ssbo_last_binding;
|
||||
uint32_t shadow_samp_mask;
|
||||
+ /* subsets of samplers_used_mask: the samplers declared to return signed
|
||||
+ * and unsigned integers (isampler*, usampler*) */
|
||||
+ uint32_t samplers_sint_mask;
|
||||
+ uint32_t samplers_uint_mask;
|
||||
uint32_t attrib_input_mask;
|
||||
uint32_t fs_blend_equation_advanced;
|
||||
uint32_t fog_input_mask;
|
||||
--
|
||||
2.56.1
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
// has the scanout, frame() otherwise), runs the steps in the guest with no
|
||||
// GL environment variables and posts what they printed; the harness judges.
|
||||
// Query: size=WxH (the display), resize=WxH (step r's size, default 960x540; not 800x600: DRM_GLMARK),
|
||||
// scenes= (glmark2 -b list), skip=a,b,c,d,e,g,h,r,
|
||||
// scenes= (glmark2 -b list), skip=a,b,c,d,e,g,h,m,t,r,
|
||||
// cmd= (a command run first, as root; for debugging), eager (gl: { eager:
|
||||
// true }: the renderer loads before the boot; by default it loads on the
|
||||
// guest's first GL use), glmodule=URL (another virgl.mjs: the harness's
|
||||
@@ -153,7 +153,7 @@ async function main() {
|
||||
};
|
||||
|
||||
// the test programs, in /tmp
|
||||
for (const name of ["kwgl-state", "prime", "kms-dumb", "kms-hotplug", "window-image", "int-attrib", "msaa"]) {
|
||||
for (const name of ["kwgl-state", "prime", "kms-dumb", "kms-hotplug", "window-image", "int-attrib", "msaa", "feedback"]) {
|
||||
const r = await fetch(`/files/${name}`);
|
||||
if (!r.ok) continue;
|
||||
const bin = new Uint8Array(await r.arrayBuffer());
|
||||
@@ -283,6 +283,13 @@ async function main() {
|
||||
if (!skip.has("m"))
|
||||
await run("msaa", "dmesg -C; /tmp/msaa /dev/dri/renderD128; e=$?; echo \"kernel errors $(dmesg | grep -c 'response 0x')\"; exit $e");
|
||||
|
||||
// (t) textures left on used sampler units that the draws never sample
|
||||
// (Firefox's WebRender): one attached to the draw framebuffer, integer
|
||||
// and float ones on samplers of the other type; right pixels, and no
|
||||
// virtio-gpu command failed in the guest's kernel
|
||||
if (!skip.has("t"))
|
||||
await run("feedback", "dmesg -C; /tmp/feedback /dev/dri/renderD128; e=$?; echo \"kernel errors $(dmesg | grep -c 'response 0x')\"; exit $e");
|
||||
|
||||
// (r) a display size change on the running machine (vm.updateDisplaySize()):
|
||||
// a KMS client reading uevents hears the hotplug and probes the new
|
||||
// preferred mode; kmscube and glmark2-es2-drm started afterwards take it,
|
||||
|
||||
@@ -70,6 +70,14 @@
|
||||
// before it offers ES 3.0), and a 4-sample draw with a depth test
|
||||
// resolved by glBlitFramebuffer: right colours, partly covered pixels
|
||||
// on the triangle's edge, no failed virtio-gpu command
|
||||
// t test/gpu/feedback.c (built as kwgl-state is): draws with textures
|
||||
// that the shaders never sample left on used units (as Firefox's
|
||||
// WebRender leaves them): one attached to the draw framebuffer (a
|
||||
// feedback loop), an integer one on a float sampler and a float one
|
||||
// on an integer sampler, then draws that sample the units again (the
|
||||
// real textures back once legal), each read back: the colours must
|
||||
// be right and no virtio-gpu command may fail (WebGL refuses such a
|
||||
// draw)
|
||||
// r a display size change on the running machine (vm.updateDisplaySize()
|
||||
// to --resize WxH, default 960x540): test/gpu/kms-hotplug.c (built
|
||||
// here) reads the drm uevent with HOTPLUG=1 from netlink and probes
|
||||
@@ -148,6 +156,7 @@ const windowBin = join(out, "window-image");
|
||||
const kmsDumbBin = join(out, "kms-dumb");
|
||||
const intAttribBin = join(out, "int-attrib");
|
||||
const msaaBin = join(out, "msaa");
|
||||
const feedbackBin = join(out, "feedback");
|
||||
|
||||
const step = (what, cmd, cmdArgs) => {
|
||||
console.log(`== ${what}`);
|
||||
@@ -176,6 +185,7 @@ if (drm) step("test/gpu/prime.c for the guest", process.env.ZIG ?? "zig", ["cc",
|
||||
if (drm) step("test/gpu/window-image.c for the guest", join(here, "build-guest-state.sh"), [windowBin, join(root, "test/gpu/window-image.c")]);
|
||||
if (drm) step("test/gpu/int-attrib.c for the guest", join(here, "build-guest-state.sh"), [intAttribBin, join(root, "test/gpu/int-attrib.c")]);
|
||||
if (drm) step("test/gpu/msaa.c for the guest", join(here, "build-guest-state.sh"), [msaaBin, join(root, "test/gpu/msaa.c")]);
|
||||
if (drm) step("test/gpu/feedback.c for the guest", join(here, "build-guest-state.sh"), [feedbackBin, join(root, "test/gpu/feedback.c")]);
|
||||
if (drm) step("test/gpu/kms-hotplug.c for the guest", process.env.ZIG ?? "zig", ["cc", "-target", "aarch64-linux-musl", "-mcpu=generic", "-static", "-O2", "-s", "-o", join(out, "kms-hotplug"), join(root, "test/gpu/kms-hotplug.c")]);
|
||||
if (drm && (has("--fail") || has("--lose"))) step("test/gpu/kms-dumb.c for the guest", process.env.ZIG ?? "zig", ["cc", "-target", "aarch64-linux-musl", "-mcpu=generic", "-static", "-O2", "-s", "-o", kmsDumbBin, join(root, "test/gpu/kms-dumb.c")]);
|
||||
|
||||
@@ -236,6 +246,7 @@ const server = createServer(async (req, res) => {
|
||||
if (p === "/files/window-image" && drm) return sendFile(req, res, windowBin, "application/octet-stream");
|
||||
if (p === "/files/int-attrib" && drm) return sendFile(req, res, intAttribBin, "application/octet-stream");
|
||||
if (p === "/files/msaa" && drm) return sendFile(req, res, msaaBin, "application/octet-stream");
|
||||
if (p === "/files/feedback" && drm) return sendFile(req, res, feedbackBin, "application/octet-stream");
|
||||
if (p === "/files/kms-dumb" && drm && existsSync(kmsDumbBin)) return sendFile(req, res, kmsDumbBin, "application/octet-stream");
|
||||
if (p === "/files/kms-hotplug" && drm) return sendFile(req, res, join(out, "kms-hotplug"), "application/octet-stream");
|
||||
// --gl-delay: the renderer's module, late (/slow/virgl.mjs; virgl.wasm next to it on time)
|
||||
@@ -262,7 +273,7 @@ if (has("--lose")) query.set("lose", "1");
|
||||
if (opt("--gl-delay")) query.set("glmodule", "/slow/virgl.mjs");
|
||||
if (has("--fail")) {
|
||||
query.set("fail", "1");
|
||||
if (!opt("--skip")) query.set("skip", "a,b,c,d,e,g,h");
|
||||
if (!opt("--skip")) query.set("skip", "a,b,c,d,e,g,h,t");
|
||||
}
|
||||
const url = `http://127.0.0.1:${server.address().port}/test/${drm ? "drm" : "unkernel"}.html?${query}`;
|
||||
console.log(`== ${url}\n output in ${out}`);
|
||||
@@ -461,6 +472,9 @@ function judgeDrm(res) {
|
||||
const ms = res.steps.msaa;
|
||||
if (ms) report("multisampling", ms.exitCode === 0 && /^ok samples/m.test(ms.stdout) && /^ok draw/m.test(ms.stdout) && /^kernel errors 0$/m.test(ms.stdout),
|
||||
ms.stdout.trim().split("\n").join("; ") + (ms.stderr.trim() ? ` (stderr: ${ms.stderr.trim()})` : ""));
|
||||
const fb = res.steps.feedback;
|
||||
if (fb) report("texture units", fb.exitCode === 0 && ["into-b-a-bound", "into-a-a-bound", "sampled-a", "into-b-unbound", "types", "sampled-types"].every((n) => new RegExp(`^ok ${n}:`, "m").test(fb.stdout)) && /^kernel errors 0$/m.test(fb.stdout),
|
||||
fb.stdout.trim().split("\n").join("; ") + (fb.stderr.trim() ? ` (stderr: ${fb.stderr.trim()})` : ""));
|
||||
// A WebGL error drops one call and the guest's context goes on (gl.md, Patches, 19): none is expected.
|
||||
// A lost context may raise one (CONTEXT_LOST_WEBGL), so not after --lose.
|
||||
const glErrors = res.gl?.gpu?.glErrors;
|
||||
|
||||
250
test/gpu/feedback.c
Normal file
250
test/gpu/feedback.c
Normal file
@@ -0,0 +1,250 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
||||
// Textures on used sampler units that the draws never sample, on Mesa's
|
||||
// virgl driver (unkernel-check.mjs --drm step t): what Firefox's WebRender
|
||||
// leaves there. GLES leaves what such a sampler returns undefined, but the
|
||||
// draw itself must land; WebGL refuses it (GL_INVALID_OPERATION) when the
|
||||
// texture is attached to the draw framebuffer (a feedback loop) or is of
|
||||
// another base type than its sampler (float, int, uint), even when the
|
||||
// shader never samples it, so the renderer must keep such a texture off
|
||||
// the unit (renderer patch 20). From issue #27's feedback.c.
|
||||
//
|
||||
// Surfaceless EGL on GBM (the render node), a GLES 3.0 context, 64x64
|
||||
// RGBA8 textures A and B that take turns as the framebuffer's colour. The
|
||||
// shaders sample only in a branch a uniform keeps closed, except in the
|
||||
// draws marked "sampled", where it is open (the renderer must have put the
|
||||
// real texture back). Each draw covers the target and is read back:
|
||||
//
|
||||
// into-b-a-bound into B, A on the fragment sampler's unit
|
||||
// into-a-a-bound into A, A on that unit (the feedback loop): green
|
||||
// sampled-a sampled: into B, A still on that unit, which must
|
||||
// hold A again now that A is not attached: green
|
||||
// into-b-unbound into B, nothing on the unit
|
||||
// types into B, an RGBA8I texture on the unit of the
|
||||
// fragment shader's sampler2D, and A on the unit of
|
||||
// the vertex shader's isampler2D (float on int)
|
||||
// sampled-types sampled: the same units with A on the sampler2D's
|
||||
// and the RGBA8I texture (texel 0 red 100) on the
|
||||
// isampler2D's: A's green plus blue 100
|
||||
//
|
||||
// It prints one line per draw, "ok NAME: read ..." or "bad NAME: read ...",
|
||||
// and exits 0 when all are ok.
|
||||
//
|
||||
// feedback [DEVICE]
|
||||
#define _GNU_SOURCE
|
||||
#define EGL_NO_X11
|
||||
#include <EGL/egl.h>
|
||||
#include <EGL/eglext.h>
|
||||
#include <GLES3/gl3.h>
|
||||
#include <fcntl.h>
|
||||
#include <gbm.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define SIZE 64
|
||||
|
||||
static _Noreturn void die(const char *what)
|
||||
{
|
||||
fprintf(stderr, "feedback: %s (EGL 0x%x, GL 0x%x)\n", what, eglGetError(), glGetError());
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static GLuint shader(GLenum kind, const char *src)
|
||||
{
|
||||
GLuint s = glCreateShader(kind);
|
||||
GLint ok = 0;
|
||||
char log[1024];
|
||||
|
||||
glShaderSource(s, 1, &src, NULL);
|
||||
glCompileShader(s);
|
||||
glGetShaderiv(s, GL_COMPILE_STATUS, &ok);
|
||||
if (!ok) {
|
||||
glGetShaderInfoLog(s, sizeof(log), NULL, log);
|
||||
fprintf(stderr, "feedback: shader: %s\n", log);
|
||||
exit(1);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
static GLuint program(const char *vs, const char *fs)
|
||||
{
|
||||
GLuint p = glCreateProgram();
|
||||
GLint ok = 0;
|
||||
|
||||
glAttachShader(p, shader(GL_VERTEX_SHADER, vs));
|
||||
glAttachShader(p, shader(GL_FRAGMENT_SHADER, fs));
|
||||
glBindAttribLocation(p, 0, "p");
|
||||
glLinkProgram(p);
|
||||
glGetProgramiv(p, GL_LINK_STATUS, &ok);
|
||||
if (!ok)
|
||||
die("link");
|
||||
glUseProgram(p);
|
||||
glUniform1f(glGetUniformLocation(p, "never"), 0.0f);
|
||||
return p;
|
||||
}
|
||||
|
||||
// Open (1) or close (0) the program's sampling branch.
|
||||
static void sampling(GLuint prog, int open)
|
||||
{
|
||||
glUseProgram(prog);
|
||||
glUniform1f(glGetUniformLocation(prog, "never"), open ? 1.0f : 0.0f);
|
||||
}
|
||||
|
||||
static GLuint texture(GLenum format)
|
||||
{
|
||||
GLuint t;
|
||||
|
||||
glGenTextures(1, &t);
|
||||
glBindTexture(GL_TEXTURE_2D, t);
|
||||
glTexStorage2D(GL_TEXTURE_2D, 1, format, SIZE, SIZE);
|
||||
// nearest: an integer texture is complete only so
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
return t;
|
||||
}
|
||||
|
||||
// Draw into the target with the colour and read the centre back: the
|
||||
// colour, or want when the draw samples.
|
||||
static int draw(const char *name, GLuint prog, GLuint target, const GLfloat *c, const unsigned char *want)
|
||||
{
|
||||
unsigned char px[4] = { 0 };
|
||||
int ok = 1;
|
||||
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, target, 0);
|
||||
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
||||
die("framebuffer");
|
||||
glUseProgram(prog);
|
||||
glUniform4fv(glGetUniformLocation(prog, "color"), 1, c);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
glReadPixels(SIZE / 2, SIZE / 2, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, px);
|
||||
for (int i = 0; i < 4; i++)
|
||||
ok &= px[i] == (want ? want[i] : (unsigned char)(c[i] * 255.0f));
|
||||
printf("%s %s: read %u,%u,%u,%u, want %u,%u,%u,%u\n", ok ? "ok" : "bad", name,
|
||||
px[0], px[1], px[2], px[3],
|
||||
want ? want[0] : (unsigned)(c[0] * 255.0f), want ? want[1] : (unsigned)(c[1] * 255.0f),
|
||||
want ? want[2] : (unsigned)(c[2] * 255.0f), want ? want[3] : (unsigned)(c[3] * 255.0f));
|
||||
return !ok;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
const char *dev = argc > 1 ? argv[1] : "/dev/dri/renderD128";
|
||||
static const EGLint ctx_attr[] = { EGL_CONTEXT_MAJOR_VERSION, 3, EGL_NONE };
|
||||
static const GLfloat verts[] = { -1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1 };
|
||||
static const GLfloat red[] = { 1, 0, 0, 1 }, green[] = { 0, 1, 0, 1 }, blue[] = { 0, 0, 1, 1 },
|
||||
yellow[] = { 1, 1, 0, 1 };
|
||||
// A as into-a-a-bound left it; that plus the RGBA8I texel's 100 as blue
|
||||
static const unsigned char a_green[] = { 0, 255, 0, 255 }, types_want[] = { 0, 255, 100, 255 };
|
||||
static const char *vs = "#version 300 es\n"
|
||||
"in vec2 p;\n"
|
||||
"void main() { gl_Position = vec4(p, 0.0, 1.0); }\n";
|
||||
static const char *fs = "#version 300 es\n"
|
||||
"precision mediump float;\n"
|
||||
"uniform sampler2D s;\n"
|
||||
"uniform float never;\n"
|
||||
"uniform vec4 color;\n"
|
||||
"out vec4 o;\n"
|
||||
"void main() { o = color; if (never > 0.5) o = texture(s, vec2(0.5)); }\n";
|
||||
// the type case: an isampler2D in the vertex shader, a sampler2D in the fragment shader
|
||||
static const char *vs_int = "#version 300 es\n"
|
||||
"uniform highp isampler2D si;\n"
|
||||
"uniform highp float never;\n"
|
||||
"in vec2 p;\n"
|
||||
"flat out highp int v;\n"
|
||||
"void main() {\n"
|
||||
" v = 0;\n"
|
||||
" if (never > 0.5) v = texelFetch(si, ivec2(0), 0).x;\n"
|
||||
" gl_Position = vec4(p, 0.0, 1.0);\n"
|
||||
"}\n";
|
||||
static const char *fs_int = "#version 300 es\n"
|
||||
"precision mediump float;\n"
|
||||
"uniform sampler2D s;\n"
|
||||
"uniform highp float never;\n"
|
||||
"uniform vec4 color;\n"
|
||||
"flat in highp int v;\n"
|
||||
"out vec4 o;\n"
|
||||
"void main() { o = color; if (never > 0.5) o = texture(s, vec2(0.5)) + vec4(0.0, 0.0, float(v) / 255.0, 0.0); }\n";
|
||||
int fd, bad = 0;
|
||||
struct gbm_device *gbm;
|
||||
EGLDisplay dpy;
|
||||
EGLContext ctx;
|
||||
GLuint prog, prog_int, vao, vbo, fb, a, b, i8;
|
||||
GLenum err;
|
||||
|
||||
fd = open(dev, O_RDWR | O_CLOEXEC);
|
||||
if (fd < 0)
|
||||
die("open");
|
||||
gbm = gbm_create_device(fd);
|
||||
if (!gbm)
|
||||
die("gbm_create_device");
|
||||
dpy = eglGetPlatformDisplay(EGL_PLATFORM_GBM_KHR, gbm, NULL);
|
||||
if (dpy == EGL_NO_DISPLAY || !eglInitialize(dpy, NULL, NULL))
|
||||
die("eglInitialize");
|
||||
if (!eglBindAPI(EGL_OPENGL_ES_API))
|
||||
die("eglBindAPI");
|
||||
ctx = eglCreateContext(dpy, EGL_NO_CONFIG_KHR, EGL_NO_CONTEXT, ctx_attr);
|
||||
if (ctx == EGL_NO_CONTEXT || !eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx))
|
||||
die("context");
|
||||
printf("renderer %s\n", glGetString(GL_RENDERER));
|
||||
|
||||
// two triangles that cover the target
|
||||
glGenVertexArrays(1, &vao);
|
||||
glBindVertexArray(vao);
|
||||
glGenBuffers(1, &vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
|
||||
|
||||
a = texture(GL_RGBA8);
|
||||
b = texture(GL_RGBA8);
|
||||
i8 = texture(GL_RGBA8I);
|
||||
{
|
||||
static const GLbyte texel[4] = { 100, 0, 0, 0 };
|
||||
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RGBA_INTEGER, GL_BYTE, texel);
|
||||
}
|
||||
glGenFramebuffers(1, &fb);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fb);
|
||||
glViewport(0, 0, SIZE, SIZE);
|
||||
|
||||
// the sampler on unit 0
|
||||
prog = program(vs, fs);
|
||||
glUniform1i(glGetUniformLocation(prog, "s"), 0);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, a);
|
||||
bad += draw("into-b-a-bound", prog, b, red, NULL);
|
||||
bad += draw("into-a-a-bound", prog, a, green, NULL);
|
||||
// only the framebuffer changes: A must be back on its unit
|
||||
sampling(prog, 1);
|
||||
bad += draw("sampled-a", prog, b, red, a_green);
|
||||
sampling(prog, 0);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
bad += draw("into-b-unbound", prog, b, blue, NULL);
|
||||
|
||||
// the sampler2D on unit 0 with an integer texture, the isampler2D on unit 1 with a float one
|
||||
prog_int = program(vs_int, fs_int);
|
||||
glUniform1i(glGetUniformLocation(prog_int, "s"), 0);
|
||||
glUniform1i(glGetUniformLocation(prog_int, "si"), 1);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, i8);
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, a);
|
||||
bad += draw("types", prog_int, b, yellow, NULL);
|
||||
|
||||
// the same units with textures of their samplers' types, sampled
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, a);
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, i8);
|
||||
sampling(prog_int, 1);
|
||||
bad += draw("sampled-types", prog_int, b, yellow, types_want);
|
||||
|
||||
err = glGetError();
|
||||
if (err != GL_NO_ERROR) {
|
||||
printf("bad GL error 0x%x\n", err);
|
||||
bad++;
|
||||
}
|
||||
return bad ? 1 : 0;
|
||||
}
|
||||
Reference in New Issue
Block a user