Skip to content

asahi: emit clean G17P multi-draw sub-draws without re-validating state - #430

Merged
RyanTheTide merged 1 commit into
aurora-silicon:aurora-wipfrom
AxxzyWasTaken:pr/gl-fast-multidraw
Nov 19, 2026
Merged

RyanTheTide merged 1 commit into
aurora-silicon:aurora-wipfrom
AxxzyWasTaken:pr/gl-fast-multidraw

Conversation

@AxxzyWasTaken

Copy link
Copy Markdown

util_draw_multi() sends every sub-draw of a multi-draw (Sodium's glMultiDrawElementsBaseVertex terrain) through agx_draw_vbo(), which re-validates shaders, descriptors, feedback loops and queries each time, about 0.1 us of CPU per sub-draw. agx_encode_state() already emits nothing for a sub-draw once the context is clean, so after one full draw the following sub-draws only need their index list records.

The feedback-loop check and the index buffer read are made once per multi-draw, since bindings can't change inside it. Draw ID or base parameters in uniforms, statistics or primitives-generated queries, geometry/tessellation stages, transform feedback, passthrough GS and misaligned index starts keep the full path. AGX_FAST_MULTIDRAW=0 turns it off.

Only G17P (A18 Pro, M4 Pro, M4 Max) takes it. Measured only on the M4 Max.

Setup

  • MacBook Pro 16" M4 Max (J616C, T6041, 32-core GPU), GPU firmware 25G83, Linux 7.3 with the aurora-silicon GPU driver.
  • Mesa release builds of aurora-wip 4ad5457 (baseline) and this branch, 2b09226 (-Dbuildtype=release -Dgallium-drivers=asahi -Dvulkan-drivers=asahi -Dbuild-tests=true).
  • Offscreen (EGL surfaceless), with the machine's session environment (ASAHI_MESA_DEBUG=1queue, MESA_GLTHREAD=false). Each build's libgallium is picked with LD_LIBRARY_PATH. Desktop running, no game.

Benchmark

sodiumbench (source below) draws Sodium's 20-byte vertex format the way Sodium does: one glMultiDrawElementsBaseVertex per frame, one sub-draw per section, a shared quad index buffer, 864x558 RGBA8 + Z32F. real uses Sodium's terrain shaders (from the mod, not attached); trivial uses minimal built-in shaders. 10 warm-up frames, 300 timed. "CPU" is the time spent in the draw call and glFlush per frame. 3 runs per build, alternating base / PR.

workload aurora-wip 4ad5457 this PR median
16000 sub-draws x 64 quads, real frame ms 1.223 / 1.331 / 1.389 0.735 / 0.865 / 0.864 1.331 -> 0.864 (-35 %)
CPU ms 0.885 / 0.995 / 1.044 0.389 / 0.414 / 0.399 0.995 -> 0.399 (-60 %)
16000 x 64, trivial frame ms 1.409 / 1.274 / 1.298 0.798 / 0.857 / 0.851 1.298 -> 0.851 (-34 %)
CPU ms 1.094 / 0.939 / 0.974 0.389 / 0.412 / 0.403 0.974 -> 0.403 (-59 %)
400 sub-draws x 2560 quads, real frame ms 0.623 / 0.701 / 0.745 0.612 / 0.615 / 0.612 0.701 -> 0.612
CPU ms 0.123 / 0.090 / 0.067 0.092 / 0.075 / 0.096 0.090 -> 0.092 (noise)

The 400-draw case has little CPU to save; its frame time moved within the baseline's own spread (0.62-0.75 ms). The PR build with AGX_FAST_MULTIDRAW=0 gives the baseline: 1.287 ms frame, 0.948 ms CPU.

In Minecraft (15 Nov, render distance 32, OpenGL) an A/B of the switch gave 105.0 vs 105.1 fps: that scene is GPU-bound, so only the CPU time goes down. It has been in this machine's daily GL driver since 15 Nov.

Correctness

  • Image hashes of the 16000-draw frame with 32-bit indices, 16-bit indices, non-indexed (glMultiDrawArrays) and back-face culled: all 208274265f1ebe83 on both builds.
  • rangeverify verify, zsverify verify and gldepthcomp (1920x1080, 3456x2234): identical to the baseline.
  • 0 GPU recoveries. meson test --suite asahi: 138 passed, 0 failed (baseline 138/0).

Not tested: the A18 Pro and M4 Pro; games on this exact build.

Commands
cc -O2 -o sodiumbench sodiumbench.c -lEGL -lOpenGL -lm
# lib-base / lib-pr: the installed Mesa lib dir with that build's libgallium-*.so
SB="./sodiumbench sodium.vert sodium.frag"
for B in base pr; do for e in "" SB_IDX16=1 SB_NOINDEX=1 SB_CULL=1; do
  env $e LD_LIBRARY_PATH=lib-$B $SB 16000 64 30 864 558 real
done; done
for n in 1 2 3; do for B in base pr; do
  LD_LIBRARY_PATH=lib-$B $SB 16000 64 300 864 558 real
  LD_LIBRARY_PATH=lib-$B $SB 400 2560 300 864 558 real
  LD_LIBRARY_PATH=lib-$B $SB 16000 64 300 864 558 trivial   # shader files unused
done; done
AGX_FAST_MULTIDRAW=0 LD_LIBRARY_PATH=lib-pr $SB 16000 64 300 864 558 real
Raw output
[base] sodiumbench sodium.vert sodium.frag 16000 64 30 864 558 real ::
    image hash=208274265f1ebe83 non-sky pixels=482112 real sections=16000 quads=64 tris/frame=2.05M verts/frame=4.10M 864x558: 652.4 fps (1.533 ms/frame, 1336 Mtris/s) cpu=1.098 ms/frame 
[base] SB_IDX16=1 sodiumbench sodium.vert sodium.frag 16000 64 30 864 558 real ::
    image hash=208274265f1ebe83 non-sky pixels=482112 real sections=16000 quads=64 tris/frame=2.05M verts/frame=4.10M 864x558: 716.9 fps (1.395 ms/frame, 1468 Mtris/s) cpu=0.956 ms/frame 
[base] SB_NOINDEX=1 sodiumbench sodium.vert sodium.frag 16000 64 30 864 558 real ::
    image hash=208274265f1ebe83 non-sky pixels=482112 real sections=16000 quads=64 tris/frame=2.05M verts/frame=6.14M 864x558: 610.4 fps (1.638 ms/frame, 1250 Mtris/s) cpu=0.892 ms/frame 
[base] SB_CULL=1 sodiumbench sodium.vert sodium.frag 16000 64 30 864 558 real ::
    image hash=208274265f1ebe83 non-sky pixels=482112 real sections=16000 quads=64 tris/frame=2.05M verts/frame=4.10M 864x558: 708.5 fps (1.411 ms/frame, 1451 Mtris/s) cpu=0.952 ms/frame 
[pr] sodiumbench sodium.vert sodium.frag 16000 64 30 864 558 real ::
    image hash=208274265f1ebe83 non-sky pixels=482112 real sections=16000 quads=64 tris/frame=2.05M verts/frame=4.10M 864x558: 899.2 fps (1.112 ms/frame, 1842 Mtris/s) cpu=0.284 ms/frame 
[pr] SB_IDX16=1 sodiumbench sodium.vert sodium.frag 16000 64 30 864 558 real ::
    image hash=208274265f1ebe83 non-sky pixels=482112 real sections=16000 quads=64 tris/frame=2.05M verts/frame=4.10M 864x558: 1046.0 fps (0.956 ms/frame, 2142 Mtris/s) cpu=0.261 ms/frame 
[pr] SB_NOINDEX=1 sodiumbench sodium.vert sodium.frag 16000 64 30 864 558 real ::
    image hash=208274265f1ebe83 non-sky pixels=482112 real sections=16000 quads=64 tris/frame=2.05M verts/frame=6.14M 864x558: 850.3 fps (1.176 ms/frame, 1741 Mtris/s) cpu=0.208 ms/frame 
[pr] SB_CULL=1 sodiumbench sodium.vert sodium.frag 16000 64 30 864 558 real ::
    image hash=208274265f1ebe83 non-sky pixels=482112 real sections=16000 quads=64 tris/frame=2.05M verts/frame=4.10M 864x558: 1186.8 fps (0.843 ms/frame, 2430 Mtris/s) cpu=0.288 ms/frame 
[base] sodiumbench sodium.vert sodium.frag 16000 64 300 864 558 real ::
    image hash=208274265f1ebe83 non-sky pixels=482112 real sections=16000 quads=64 tris/frame=2.05M verts/frame=4.10M 864x558: 817.4 fps (1.223 ms/frame, 1674 Mtris/s) cpu=0.885 ms/frame 
[base] sodiumbench sodium.vert sodium.frag 400 2560 300 864 558 real ::
    image hash=208274265f1ebe83 non-sky pixels=482112 real sections=400 quads=2560 tris/frame=2.05M verts/frame=4.10M 864x558: 1604.9 fps (0.623 ms/frame, 3287 Mtris/s) cpu=0.123 ms/frame 
[base] sodiumbench sodium.vert sodium.frag 16000 64 300 864 558 trivial ::
    image hash=208274265f1ebe83 non-sky pixels=482112 trivial sections=16000 quads=64 tris/frame=2.05M verts/frame=4.10M 864x558: 709.9 fps (1.409 ms/frame, 1454 Mtris/s) cpu=1.094 ms/frame 
[pr] sodiumbench sodium.vert sodium.frag 16000 64 300 864 558 real ::
    image hash=208274265f1ebe83 non-sky pixels=482112 real sections=16000 quads=64 tris/frame=2.05M verts/frame=4.10M 864x558: 1361.1 fps (0.735 ms/frame, 2788 Mtris/s) cpu=0.389 ms/frame 
[pr] sodiumbench sodium.vert sodium.frag 400 2560 300 864 558 real ::
    image hash=208274265f1ebe83 non-sky pixels=482112 real sections=400 quads=2560 tris/frame=2.05M verts/frame=4.10M 864x558: 1634.5 fps (0.612 ms/frame, 3347 Mtris/s) cpu=0.092 ms/frame 
[pr] sodiumbench sodium.vert sodium.frag 16000 64 300 864 558 trivial ::
    image hash=208274265f1ebe83 non-sky pixels=482112 trivial sections=16000 quads=64 tris/frame=2.05M verts/frame=4.10M 864x558: 1253.8 fps (0.798 ms/frame, 2568 Mtris/s) cpu=0.389 ms/frame 
[base] sodiumbench sodium.vert sodium.frag 16000 64 300 864 558 real ::
    image hash=208274265f1ebe83 non-sky pixels=482112 real sections=16000 quads=64 tris/frame=2.05M verts/frame=4.10M 864x558: 751.2 fps (1.331 ms/frame, 1539 Mtris/s) cpu=0.995 ms/frame 
[base] sodiumbench sodium.vert sodium.frag 400 2560 300 864 558 real ::
    image hash=208274265f1ebe83 non-sky pixels=482112 real sections=400 quads=2560 tris/frame=2.05M verts/frame=4.10M 864x558: 1426.1 fps (0.701 ms/frame, 2921 Mtris/s) cpu=0.090 ms/frame 
[base] sodiumbench sodium.vert sodium.frag 16000 64 300 864 558 trivial ::
    image hash=208274265f1ebe83 non-sky pixels=482112 trivial sections=16000 quads=64 tris/frame=2.05M verts/frame=4.10M 864x558: 785.2 fps (1.274 ms/frame, 1608 Mtris/s) cpu=0.939 ms/frame 
[pr] sodiumbench sodium.vert sodium.frag 16000 64 300 864 558 real ::
    image hash=208274265f1ebe83 non-sky pixels=482112 real sections=16000 quads=64 tris/frame=2.05M verts/frame=4.10M 864x558: 1156.2 fps (0.865 ms/frame, 2368 Mtris/s) cpu=0.414 ms/frame 
[pr] sodiumbench sodium.vert sodium.frag 400 2560 300 864 558 real ::
    image hash=208274265f1ebe83 non-sky pixels=482112 real sections=400 quads=2560 tris/frame=2.05M verts/frame=4.10M 864x558: 1626.8 fps (0.615 ms/frame, 3332 Mtris/s) cpu=0.075 ms/frame 
[pr] sodiumbench sodium.vert sodium.frag 16000 64 300 864 558 trivial ::
    image hash=208274265f1ebe83 non-sky pixels=482112 trivial sections=16000 quads=64 tris/frame=2.05M verts/frame=4.10M 864x558: 1166.6 fps (0.857 ms/frame, 2389 Mtris/s) cpu=0.412 ms/frame 
[base] sodiumbench sodium.vert sodium.frag 16000 64 300 864 558 real ::
    image hash=208274265f1ebe83 non-sky pixels=482112 real sections=16000 quads=64 tris/frame=2.05M verts/frame=4.10M 864x558: 719.8 fps (1.389 ms/frame, 1474 Mtris/s) cpu=1.044 ms/frame 
[base] sodiumbench sodium.vert sodium.frag 400 2560 300 864 558 real ::
    image hash=208274265f1ebe83 non-sky pixels=482112 real sections=400 quads=2560 tris/frame=2.05M verts/frame=4.10M 864x558: 1342.3 fps (0.745 ms/frame, 2749 Mtris/s) cpu=0.067 ms/frame 
[base] sodiumbench sodium.vert sodium.frag 16000 64 300 864 558 trivial ::
    image hash=208274265f1ebe83 non-sky pixels=482112 trivial sections=16000 quads=64 tris/frame=2.05M verts/frame=4.10M 864x558: 770.2 fps (1.298 ms/frame, 1577 Mtris/s) cpu=0.974 ms/frame 
[pr] sodiumbench sodium.vert sodium.frag 16000 64 300 864 558 real ::
    image hash=208274265f1ebe83 non-sky pixels=482112 real sections=16000 quads=64 tris/frame=2.05M verts/frame=4.10M 864x558: 1157.4 fps (0.864 ms/frame, 2370 Mtris/s) cpu=0.399 ms/frame 
[pr] sodiumbench sodium.vert sodium.frag 400 2560 300 864 558 real ::
    image hash=208274265f1ebe83 non-sky pixels=482112 real sections=400 quads=2560 tris/frame=2.05M verts/frame=4.10M 864x558: 1633.1 fps (0.612 ms/frame, 3344 Mtris/s) cpu=0.096 ms/frame 
[pr] sodiumbench sodium.vert sodium.frag 16000 64 300 864 558 trivial ::
    image hash=208274265f1ebe83 non-sky pixels=482112 trivial sections=16000 quads=64 tris/frame=2.05M verts/frame=4.10M 864x558: 1175.1 fps (0.851 ms/frame, 2407 Mtris/s) cpu=0.403 ms/frame 
[pr] AGX_FAST_MULTIDRAW=0 sodiumbench sodium.vert sodium.frag 16000 64 300 864 558 real ::
    image hash=208274265f1ebe83 non-sky pixels=482112 real sections=16000 quads=64 tris/frame=2.05M verts/frame=4.10M 864x558: 776.9 fps (1.287 ms/frame, 1591 Mtris/s) cpu=0.948 ms/frame 
[base] rangeverify verify 4 200 :: verify: 200 frames, 0 wrong -> PASS 
[base] zsverify verify 100 :: zsverify: 100 frames, depth-only pass 0 wrong, sampling pass 0 wrong -> PASS 
[base] gldepthcomp 1920 1080 hash 7d68757b
[base] gldepthcomp 3456 2234 hash 18c08ae5
[pr] rangeverify verify 4 200 :: verify: 200 frames, 0 wrong -> PASS 
[pr] zsverify verify 100 :: zsverify: 100 frames, depth-only pass 0 wrong, sampling pass 0 wrong -> PASS 
[pr] gldepthcomp 1920 1080 hash 7d68757b
[pr] gldepthcomp 3456 2234 hash 18c08ae5
sodiumbench.c
// sodiumbench: Sodium 0.9's real terrain shaders on Sodium's 20-byte vertex format, drawn the way Sodium
// draws (glMultiDrawElementsBaseVertex, one draw per section, shared quad index buffer), off-screen.
// usage: sodiumbench <vert> <frag> <sections> <quads per section> <frames> [w h] [mode]
//   mode "real" = Sodium shaders; "trivial" = same vertex format and draws, minimal shaders
//   quads are sized so most are a few pixels on screen, like distant terrain.
#define GL_GLEXT_PROTOTYPES 1
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <math.h>
#include <time.h>
static double now(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec + t.tv_nsec * 1e-9; }
static char *slurp(const char *p) { FILE *f = fopen(p, "rb"); if (!f) { perror(p); exit(1); } fseek(f, 0, SEEK_END); long n = ftell(f); rewind(f); char *b = malloc(n + 1); fread(b, 1, n, f); b[n] = 0; fclose(f); return b; }
static GLuint sh(GLenum t, const char *s) { GLuint o = glCreateShader(t); glShaderSource(o, 1, &s, 0); glCompileShader(o);
  GLint ok; glGetShaderiv(o, GL_COMPILE_STATUS, &ok); if (!ok) { char b[4000]; glGetShaderInfoLog(o, 4000, 0, b); fprintf(stderr, "%s\n", b); exit(1); } return o; }
static const char *TRIV_VS = "#version 460 core\nlayout(location=0) in uvec2 a_Position; layout(location=1) in vec4 a_Color;\n"
  "layout(std140) uniform u_Globals { mat4 u_ProjectionMatrix; mat4 u_ModelViewMatrix; };\nuniform vec3 u_RegionOffset;\nout vec4 v_Color;\n"
  "void main(){ vec3 p = vec3(uvec3(a_Position.x, a_Position.x >> 10, a_Position.x >> 20) & 0x3FFu) * (32.0/1024.0) - 8.0;\n"
  " gl_Position = u_ProjectionMatrix * u_ModelViewMatrix * vec4(p + u_RegionOffset, 1.0); v_Color = a_Color; }\n";
static const char *NOUBO_VS = "#version 460 core\nlayout(location=0) in uvec2 a_Position; layout(location=1) in vec4 a_Color;\n"
  "uniform vec3 u_RegionOffset;\nout vec4 v_Color;\n"
  "void main(){ vec3 p = vec3(uvec3(a_Position.x, a_Position.x >> 10, a_Position.x >> 20) & 0x3FFu) * (32.0/1024.0) - 8.0 + u_RegionOffset;\n"
  " const float f = 1.4281480, a = 1.548387, n = 0.05, fa = 1000.0;\n"
  " gl_Position = vec4(p.x * f / a, p.y * f, p.z * (fa+n)/(n-fa) + 2.0*fa*n/(n-fa), -p.z); v_Color = a_Color; }\n";
static const char *PREMUL_VS = "#version 460 core\nlayout(location=0) in uvec2 a_Position; layout(location=1) in vec4 a_Color;\n"
  "layout(std140) uniform u_Globals { mat4 u_ProjectionMatrix; mat4 u_ModelViewMatrix; };\nuniform vec3 u_RegionOffset;\nout vec4 v_Color;\n"
  "void main(){ vec3 p = vec3(uvec3(a_Position.x, a_Position.x >> 10, a_Position.x >> 20) & 0x3FFu) * (32.0/1024.0) - 8.0;\n"
  " gl_Position = u_ProjectionMatrix * vec4(p + u_RegionOffset, 1.0); v_Color = a_Color; }\n";
static const char *NOVARY_VS = "#version 460 core\nlayout(location=0) in uvec2 a_Position;\nuniform vec3 u_RegionOffset;\n"
  "void main(){ vec3 p = vec3(uvec3(a_Position.x, a_Position.x >> 10, a_Position.x >> 20) & 0x3FFu) * (32.0/1024.0) - 8.0 + u_RegionOffset;\n"
  " gl_Position = vec4(p.x * 0.92, p.y * 1.43, p.z * -1.0001 - 0.1, -p.z); }\n";
static const char *VID_VS = "#version 460 core\nuniform vec3 u_RegionOffset;\n"
  "void main(){ uint i = uint(gl_VertexID); vec3 p = vec3(float(i & 63u), float((i >> 6) & 31u), float((i >> 11) & 255u)) * 0.5 + u_RegionOffset;\n"
  " gl_Position = vec4(p.x * 0.92, p.y * 1.43, p.z * -1.0001 - 0.1, -p.z); }\n";
static const char *CONST_FS = "#version 460 core\nout vec4 fragColor; void main(){ fragColor = vec4(1.0, 0.5, 0.2, 1.0); }\n";
static const char *TRIV_FS = "#version 460 core\nin vec4 v_Color; out vec4 fragColor; void main(){ fragColor = v_Color; }\n";
int main(int argc, char **argv) {
  if (argc < 6) { fprintf(stderr, "usage\n"); return 1; }
  int S = atoi(argv[3]), Q = atoi(argv[4]), frames = atoi(argv[5]);
  int W = argc > 7 ? atoi(argv[6]) : 3456, H = argc > 7 ? atoi(argv[7]) : 2234;
  const char *mode = argc > 8 ? argv[8] : "real";
  PFNEGLGETPLATFORMDISPLAYEXTPROC gpd = (void *)eglGetProcAddress("eglGetPlatformDisplayEXT");
  EGLDisplay d = gpd(EGL_PLATFORM_SURFACELESS_MESA, EGL_DEFAULT_DISPLAY, NULL); eglInitialize(d, 0, 0); eglBindAPI(EGL_OPENGL_API);
  EGLint ca[] = {EGL_CONTEXT_MAJOR_VERSION, 4, EGL_CONTEXT_MINOR_VERSION, 6, EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT, EGL_NONE};
  EGLContext c = eglCreateContext(d, EGL_NO_CONFIG_KHR, EGL_NO_CONTEXT, ca);
  if (!c || !eglMakeCurrent(d, EGL_NO_SURFACE, EGL_NO_SURFACE, c)) { fprintf(stderr, "ctx failed\n"); return 1; }
  GLuint fb, ct, dt; glGenFramebuffers(1, &fb); glBindFramebuffer(GL_FRAMEBUFFER, fb);
  glGenTextures(1, &ct); glBindTexture(GL_TEXTURE_2D, ct); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, W, H);
  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, ct, 0);
  glGenTextures(1, &dt); glBindTexture(GL_TEXTURE_2D, dt); glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT32F, W, H);
  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, dt, 0);
  GLuint pr = glCreateProgram();
  if (!strcmp(mode, "real")) { glAttachShader(pr, sh(GL_VERTEX_SHADER, slurp(argv[1]))); glAttachShader(pr, sh(GL_FRAGMENT_SHADER, slurp(argv[2]))); }
  else if (!strcmp(mode, "novary")) { glAttachShader(pr, sh(GL_VERTEX_SHADER, NOVARY_VS)); glAttachShader(pr, sh(GL_FRAGMENT_SHADER, CONST_FS)); }
  else if (!strcmp(mode, "vid")) { glAttachShader(pr, sh(GL_VERTEX_SHADER, VID_VS)); glAttachShader(pr, sh(GL_FRAGMENT_SHADER, CONST_FS)); }
  else if (!strcmp(mode, "noubo")) { glAttachShader(pr, sh(GL_VERTEX_SHADER, NOUBO_VS)); glAttachShader(pr, sh(GL_FRAGMENT_SHADER, TRIV_FS)); }
  else if (!strcmp(mode, "onemat")) { glAttachShader(pr, sh(GL_VERTEX_SHADER, PREMUL_VS)); glAttachShader(pr, sh(GL_FRAGMENT_SHADER, TRIV_FS)); }
  else { glAttachShader(pr, sh(GL_VERTEX_SHADER, TRIV_VS)); glAttachShader(pr, sh(GL_FRAGMENT_SHADER, TRIV_FS)); }
  glLinkProgram(pr); GLint ok; glGetProgramiv(pr, GL_LINK_STATUS, &ok); if (!ok) { char b[4000]; glGetProgramInfoLog(pr, 4000, 0, b); fprintf(stderr, "link: %s\n", b); return 1; }
  glUseProgram(pr);
  // globals UBO: projection (perspective, 70 deg, near 0.05), modelview looking down +z, fog far away
  float aspect = (float)W / H, f = 1.0f / tanf(35.0f * 3.14159f / 180.0f), n = 0.05f, fa = 1000.0f;
  float G[64] = {0};
  float P[16] = { f / aspect,0,0,0, 0,f,0,0, 0,0,(fa+n)/(n-fa),-1, 0,0,2*fa*n/(n-fa),0 };
  float M[16] = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 };
  memcpy(G, P, 64); memcpy(G + 16, M, 64);
  G[32] = 0.6f; G[33] = 0.7f; G[34] = 0.9f; G[35] = 1.0f;   // fog colour
  G[36] = 900; G[37] = 1000; G[38] = 900; G[39] = 1000;      // environment + render fog start/end
  G[40] = 1.0f / 1024; G[41] = 1.0f / 1024; G[42] = 0.999f; G[43] = 0.999f; // texel size, shrink
  G[44] = 1.0f; ((int *)G)[45] = 0;                           // fade period inv, RGSS off
  GLuint ubo; glGenBuffers(1, &ubo); glBindBuffer(GL_UNIFORM_BUFFER, ubo); glBufferData(GL_UNIFORM_BUFFER, sizeof G, G, GL_STATIC_DRAW);
  GLuint bi = glGetUniformBlockIndex(pr, "u_Globals"); glUniformBlockBinding(pr, bi, 0); glBindBufferBase(GL_UNIFORM_BUFFER, 0, ubo);
  // textures: block atlas (1024^2 RGBA8 with mips), light map 16x16, section time buffer
  GLuint tex[3]; glGenTextures(3, tex);
  glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, tex[0]); glTexStorage2D(GL_TEXTURE_2D, 11, GL_RGBA8, 1024, 1024);
  uint32_t *px = malloc(1024 * 1024 * 4); for (int i = 0; i < 1024 * 1024; i++) px[i] = 0xff000000u | (i * 2654435761u >> 8);
  glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1024, 1024, GL_RGBA, GL_UNSIGNED_BYTE, px); glGenerateMipmap(GL_TEXTURE_2D);
  glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, tex[1]); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 16, 16); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 16, 16, GL_RGBA, GL_UNSIGNED_BYTE, px);
  GLuint tbo; glGenBuffers(1, &tbo); glBindBuffer(GL_TEXTURE_BUFFER, tbo); int *tinfo = calloc(256 * 64, 4); for (int i = 0; i < 256 * 64; i++) tinfo[i] = -1;
  glBufferData(GL_TEXTURE_BUFFER, 256 * 64 * 4, tinfo, GL_STATIC_DRAW);
  glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_BUFFER, tex[2]); glTexBuffer(GL_TEXTURE_BUFFER, GL_R32I, tbo);
  glUniform1i(glGetUniformLocation(pr, "u_BlockTex"), 0); glUniform1i(glGetUniformLocation(pr, "u_LightTex"), 1); glUniform1i(glGetUniformLocation(pr, "u_SectionTimeInfo"), 2);
  glUniform1i(glGetUniformLocation(pr, "u_CurrentTime"), 1000); glUniform1ui(glGetUniformLocation(pr, "u_RegionID"), 0);
  // geometry: S sections laid out on a wide field in front of the camera, Q small quads each
  size_t verts = (size_t)S * Q * 4; uint8_t *vb = calloc(verts, 20);
  unsigned seed = 1;
  for (int s = 0; s < S; s++) for (int q = 0; q < Q; q++) {
    seed = seed * 1103515245 + 12345; unsigned x0 = seed >> 22 & 1023; seed = seed * 1103515245 + 12345; unsigned y0 = seed >> 22 & 1023; seed = seed * 1103515245 + 12345; unsigned z0 = seed >> 22 & 1023;
    for (int v = 0; v < 4; v++) {
      unsigned e = getenv("SB_BIG") ? atoi(getenv("SB_BIG")) : 10; unsigned x = x0 + (v & 1) * e, y = y0 + (v >> 1) * e, z = z0;  // 10/1024 of 32 blocks = ~0.3 block quads
      uint8_t *o = vb + ((size_t)(s * Q + q) * 4 + v) * 20;
      uint32_t hi = (x >> 10 & 0x3ff) | (y >> 10 & 0x3ff) << 10 | (z >> 10 & 0x3ff) << 20, lo = (x & 0x3ff) | (y & 0x3ff) << 10 | (z & 0x3ff) << 20;
      memcpy(o, &hi, 4); memcpy(o + 4, &lo, 4);
      o[8] = 200; o[9] = 180; o[10] = 160; o[11] = 255;
      uint16_t tc[2] = {(uint16_t)((q * 37) & 0x7fff), (uint16_t)((q * 91) & 0x7fff)}; memcpy(o + 12, tc, 4);
      o[16] = 240; o[17] = 240; o[18] = 0; o[19] = (uint8_t)(s & 255);
    }
  }
  int noidx = getenv("SB_NOINDEX") != NULL;
  if (noidx) {
    uint8_t *vb6 = malloc((size_t)S * Q * 6 * 20); static const int map[6] = {0, 1, 2, 2, 1, 3};
    for (size_t qq = 0; qq < (size_t)S * Q; qq++) for (int k = 0; k < 6; k++) memcpy(vb6 + (qq * 6 + k) * 20, vb + (qq * 4 + map[k]) * 20, 20);
    vb = vb6; verts = (size_t)S * Q * 6;
  }
  GLuint vao, vbo, ibo; glGenVertexArrays(1, &vao); glBindVertexArray(vao);
  glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferStorage(GL_ARRAY_BUFFER, verts * 20, vb, 0);
  glVertexAttribIPointer(0, 2, GL_UNSIGNED_INT, 20, (void *)0); glEnableVertexAttribArray(0);
  glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, 20, (void *)8); glEnableVertexAttribArray(1);
  glVertexAttribIPointer(2, 2, GL_UNSIGNED_SHORT, 20, (void *)12); glEnableVertexAttribArray(2);
  glVertexAttribIPointer(3, 4, GL_UNSIGNED_BYTE, 20, (void *)16); glEnableVertexAttribArray(3);
  int i16 = getenv("SB_IDX16") != NULL;
  unsigned *idx = malloc(Q * 6 * 4); uint16_t *idx16 = malloc(Q * 6 * 2);
  for (int q = 0; q < Q; q++) { unsigned b = q * 4; unsigned t[6] = {b, b + 1, b + 2, b + 2, b + 1, b + 3}; memcpy(idx + q * 6, t, 24); for (int k = 0; k < 6; k++) idx16[q * 6 + k] = t[k]; }
  glGenBuffers(1, &ibo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
  if (i16) glBufferStorage(GL_ELEMENT_ARRAY_BUFFER, Q * 12, idx16, 0); else glBufferStorage(GL_ELEMENT_ARRAY_BUFFER, Q * 24, idx, 0);
  GLsizei *cnt = malloc(S * sizeof *cnt); const void **off = malloc(S * sizeof *off); GLint *base = malloc(S * sizeof *base);
  for (int s = 0; s < S; s++) { cnt[s] = Q * 6; off[s] = 0; base[s] = s * Q * 4; }
  // sections spread over a 160 x 40 x 240-block field 4..244 blocks ahead: most quads end up a few pixels big
  glViewport(0, 0, W, H); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glFrontFace(getenv("SB_CULL") ? GL_CCW : GL_CW);
  GLint ro = glGetUniformLocation(pr, "u_RegionOffset");
  int game = getenv("SB_GAME") != NULL;
  GLuint cprog = 0, cbuf = 0, bprog = 0, bvao = 0, bfb[4], btex[4]; int bw[4] = {2048, 1024, 512, 256}, bh[4] = {1024, 512, 256, 128};
  if (game) {
    const char *cs = "#version 460 core\nlayout(local_size_x = 256) in;\nlayout(std430, binding = 3) buffer B { uint o[]; };\nuniform uint fr;\n"
                     "void main(){ uint i = gl_GlobalInvocationID.x; o[i] = o[i] * 1664525u + fr + i; }\n";
    cprog = glCreateProgram(); glAttachShader(cprog, sh(GL_COMPUTE_SHADER, cs)); glLinkProgram(cprog);
    glGenBuffers(1, &cbuf); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, cbuf); glBufferData(GL_SHADER_STORAGE_BUFFER, 256 * 1024, 0, GL_DYNAMIC_COPY);
    const char *bvs = "#version 460 core\nout vec2 uv; void main(){ vec2 p = vec2((gl_VertexID & 1) * 4 - 1, (gl_VertexID >> 1) * 4 - 1); uv = p * 0.5 + 0.5; gl_Position = vec4(p, 0, 1); }\n";
    const char *bfs = "#version 460 core\nin vec2 uv; uniform sampler2D src; out vec4 c; void main(){ vec2 d = 1.0 / vec2(textureSize(src, 0));\n"
                      " c = 0.25 * (texture(src, uv + d) + texture(src, uv - d) + texture(src, uv + vec2(d.x, -d.y)) + texture(src, uv - vec2(d.x, -d.y))); }\n";
    bprog = glCreateProgram(); glAttachShader(bprog, sh(GL_VERTEX_SHADER, bvs)); glAttachShader(bprog, sh(GL_FRAGMENT_SHADER, bfs)); glLinkProgram(bprog);
    glGenVertexArrays(1, &bvao); glGenFramebuffers(4, bfb); glGenTextures(4, btex);
    for (int k = 0; k < 4; k++) { glBindTexture(GL_TEXTURE_2D, btex[k]); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, bw[k], bh[k]);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glBindFramebuffer(GL_FRAMEBUFFER, bfb[k]);
      glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, btex[k], 0); }
    glBindFramebuffer(GL_FRAMEBUFFER, fb); glUseProgram(pr); glBindVertexArray(vao);
  }
  double t0 = 0, cpu = 0;
  for (int fr = 0; fr < frames + 10; fr++) {
    if (fr == 10) { glFinish(); t0 = now(); }
    glClearColor(0.5f, 0.7f, 1, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glUniform3f(ro, -60.0f, -20.0f, getenv("SB_OFFSCREEN") ? 40.0f : -244.0f);
    double c0 = now();
    if (game) {
      glUseProgram(cprog); glUniform1ui(glGetUniformLocation(cprog, "fr"), fr);
      for (int k = 0; k < 3; k++) { glDispatchCompute(256 * 1024 / 4 / 256, 1, 1); glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT); }
      glUseProgram(pr); glBindVertexArray(vao); glBindFramebuffer(GL_FRAMEBUFFER, fb); glViewport(0, 0, W, H);
      glEnable(GL_DEPTH_TEST); glClearColor(0.5f, 0.7f, 1, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }
    if (noidx) { static GLint *first; static GLsizei *c6; if (!first) { first = malloc(S * sizeof *first); c6 = malloc(S * sizeof *c6); for (int k = 0; k < S; k++) { first[k] = k * Q * 6; c6[k] = Q * 6; } }
      glMultiDrawArrays(GL_TRIANGLES, first, c6, S); }
    else glMultiDrawElementsBaseVertex(GL_TRIANGLES, cnt, i16 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, off, S, base);
    if (game) {
      glDisable(GL_DEPTH_TEST); glUseProgram(bprog); glBindVertexArray(bvao); glActiveTexture(GL_TEXTURE0);
      for (int k = 0; k < 4; k++) { glBindFramebuffer(GL_FRAMEBUFFER, bfb[k]); glViewport(0, 0, bw[k], bh[k]);
        glBindTexture(GL_TEXTURE_2D, k ? btex[k - 1] : ct); glDrawArrays(GL_TRIANGLES, 0, 3); }
      glBindFramebuffer(GL_FRAMEBUFFER, fb); glUseProgram(pr); glBindVertexArray(vao); glEnable(GL_DEPTH_TEST);
    }
    glFlush(); if (fr >= 10) cpu += now() - c0; if (!game && fr % 3 == 2) glFinish();
  }
  glFinish(); double t = now() - t0;
  { uint8_t *px = malloc((size_t)W * H * 4); glReadPixels(0, 0, W, H, GL_RGBA, GL_UNSIGNED_BYTE, px);
    uint64_t hsh = 1469598103934665603ull; size_t lit = 0;
    for (size_t k = 0; k < (size_t)W * H * 4; k++) { hsh = (hsh ^ px[k]) * 1099511628211ull; }
    for (size_t k = 0; k < (size_t)W * H; k++) lit += !(px[k*4] == 128 && px[k*4+1] == 179 && px[k*4+2] == 255);
    printf("image hash=%016llx non-sky pixels=%zu\n", (unsigned long long)hsh, lit); }
  printf("%s sections=%d quads=%d tris/frame=%.2fM verts/frame=%.2fM %dx%d: %.1f fps (%.3f ms/frame, %.0f Mtris/s) cpu=%.3f ms/frame\n",
         mode, S, Q, S * Q * 2 / 1e6, verts / 1e6, W, H, frames / t, t * 1000 / frames, S * Q * 2.0 * frames / t / 1e6, cpu * 1000 / frames);
  return 0;
}

util_draw_multi() sends every sub-draw of a multi-draw (Sodium's
glMultiDrawElementsBaseVertex terrain) through agx_draw_vbo(), which
re-validates shaders, descriptors, feedback loops and queries each time:
about 0.1 us of CPU per sub-draw. agx_encode_state() already emits nothing
for a sub-draw once the context is clean, so after one full draw the
following sub-draws only need their index list records.

The feedback-loop check and the index buffer read are made once per
multi-draw, since bindings cannot change inside it. Draw ID or base
parameters in uniforms, statistics or primitives-generated queries,
geometry/tessellation stages, transform feedback, passthrough GS and
misaligned index starts keep the full path. Only G17P (A18 Pro, M4 Pro,
M4 Max) takes it. AGX_FAST_MULTIDRAW=0 disables it.

On an M4 Max, a Sodium-style terrain frame (16000 sub-draws, 4.1M
vertices, 864x558), median of three runs: CPU 0.995 -> 0.399 ms per frame,
frame 1.331 -> 0.865 ms. 400 sub-draws: unchanged. Images are identical
with and without it (32-bit and 16-bit indices, non-indexed, culled).

Assisted-by: LLM
@RyanTheTide

Copy link
Copy Markdown

Thanks for this, the CPU savings on Sodium-style multi-draws are a clear win and the write-up is very easy to follow.

One request on scope. We select behaviour by a hardware property, not by the set of machines a change was measured on. The fast path is a CPU-side skip of re-validation, yet it is turned off for M5 and G18G without a stated hardware reason:

  • src/gallium/drivers/asahi/agx_state.c:15540-15541: if (indirect || !agx_native_graphics(dev) || agx_chip_g17_m5(dev->chip) || dev->chip == AGX_CHIP_G18G || !debug_get_option_fast_multidraw())

agx_native_graphics(dev) already describes the encoder this relies on. Could you drop the agx_chip_g17_m5() / AGX_CHIP_G18G exclusions so every native-graphics chip takes it, or, if those chips encode the index-list record or state differently, say so in a comment next to the check?

If you make it the default, we can run CTS and a sodiumbench A/B on our A18 Pro and M4 Pro and post them here.

@RyanTheTide

Copy link
Copy Markdown

Hardware test on an A18 Pro (G17P, J700), Fedora 45 arm64 release build, as a user-space overlay over Mesa 26.3.0-30 on kernel 7.3.0-40.

Arms: A = aurora-wip 57df4db. B = A + #417 #418 #419 #428 #429 #430 #435 at their current heads (one clean merge). Same machine, same boot.

  • deqp-vk, 575,809 cases (ray_query, ray_tracing_pipeline.misc, compute, copy_and_blit, glsl, subgroups, spirv compute, draw, renderpasses, query_pool, sync and more), case for case: A 319,552 Pass / 8 ResourceError, B 319,555 Pass / 5 ResourceError, rest NotSupported in both. 7 cases changed: 5 ResourceError->Pass and 2 Pass->ResourceError, all in copy_and_blit use_after_copy 1024x1024x2 msaa and ray_tracing_pipeline.misc.recursiveTraces_AABB, and those families flip both ways between arms. Each of the 7 run alone 3 times per arm: 42/42 Pass. No regression.
  • GLES2/3/31 + KHR-GL46 + KHR-GLES3, 106,812 cases: no new Fail. 3 flush_finish cases went Pass->CompatibilityWarning (a timing check, run with 4 jobs in parallel); all 5 flush_finish cases run alone 3 times per arm: 30/30 Pass.
  • dEQP-VK.wsi.wayland (4,028 cases), hardware and MESA_VK_WSI_DEBUG=sw paths: identical results in A and B.
  • glmark2 (1920x1080 offscreen, 12 scenes, interleaved runs gated on temperature): A 5018 mean (6 runs), B 6356 mean (4 runs), +27%. vkmark: A 8523, B 8577 (noise).
  • ASAHI_MESA_DEBUG=stats (asahi: ASAHI_MESA_DEBUG=stats: time every command and name each render pass #419) prints per-pass timing lines on glmark2 in B.
  • 0 GPU firmware crashes, 0 GPU timeouts, 0 new SELinux denials across the runs.

@RyanTheTide

Copy link
Copy Markdown

A follow-up on the scope question above: looking closer, the M5/G18G exclusion is right. On those chips agx_encode_state() hands all draw state to their own state stream (agx_encode_native_g17g_state() / agx_encode_g17g_state()), so the "append only the index-list record" skip does not apply there. #520 writes that reason next to the check; nothing else needs to change here. Sorry for the noise.

AxxzyWasTaken pushed a commit to AxxzyWasTaken/mesa that referenced this pull request Nov 19, 2026
…w path

Follow-up to aurora-silicon#430 by AxxzyWasTaken.

agx_draw_multi() fell back to util_draw_multi() on
agx_chip_g17_m5() or AGX_CHIP_G18G in addition to !agx_native_graphics().
agx_native_graphics() is agx_chip_native_agx3(), which is already false on
G17G/S/C and G18G, so those terms could never decide anything. Remove them;
the condition that remains is the real one: the fast path writes the HAL200
draw body every native AGX3 chip encodes, and the comment now says so.

AGX_FAST_MULTIDRAW only turned the default path off. It is not a debug
flag of the driver's ASAHI_MESA_DEBUG set and adds a second way to change
driver behaviour from the environment, so remove it too. Behaviour with the
variable unset is unchanged on every chip.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants