Skip to content

simple_renderer: Implement Vulkan blending, viewport/scissor, load/store ops - #134

Merged
natetrost merged 2 commits into
android:mainfrom
natetrost:main
Nov 23, 2026
Merged

natetrost merged 2 commits into
android:mainfrom
natetrost:main

Conversation

@natetrost

Copy link
Copy Markdown
Contributor

Bring RenderStateVk and RenderPassVk in line with their creation
parameters (and with the GLES implementation), and fix GLES clears:

  • RenderStateVk ignored blend_enabled, blend_source_function and
    blend_dest_function: blending was always off. The blend functions now
    map to VkBlendFactor, used for both color and alpha as glBlendFunc does.
    They are only read (and range checked) when blending is enabled, in
    both RenderStateVk and RenderStateGLES.
  • RenderStateVk ignored the viewport, the scissor rect and scissor_test:
    every draw used the full framebuffer. RendererVk::SetRenderState now
    sets the dynamic viewport and scissor from the render state. Render
    state rects follow GLES (bottom left origin, display orientation), so
    they are mapped (in 64 bits) to the Y flipped, pre-rotated Vulkan
    framebuffer. The scissor is also limited to the viewport: GLES clips
    primitives at the viewport, but Vulkan implementations may draw outside
    it (the Pixel 10 Pro's PowerVR does). Like glViewport, the viewport is
    clamped to the device's maximum viewport size, and kept within its
    viewport bounds; the depth range is clamped to [0, 1] (a NaN becomes 0).
    The pipeline's unused static viewport (from a rect in the wrong
    convention) is gone, and the state's viewport/scissor members, never
    initialized, are now set from the creation parameters.
  • SetViewport and SetScissorRect on the bound render state had no effect
    until another render state was bound, in GLES and Vulkan (setting the
    same state again is a no-op). They now mark the state, and the next
    draw applies them.
  • RenderPassVk ignored the load/store operations and always cleared and
    stored color and cleared depth. They now come from the creation
    parameters, for the aspects in attachment_flags only; the others are
    don't care and their parameters aren't read. RenderPassGLES now also
    only clears the aspects in attachment_flags.
  • RenderPassVk always declared a depth/stencil attachment and reference,
    even for a swapchain without a depth/stencil buffer (format
    VK_FORMAT_UNDEFINED, no image view, one attachment framebuffer):
    VUID-VkAttachmentDescription-format-06698,
    VUID-VkSubpassDescription-pDepthStencilAttachment-02650 and
    VUID-VkFramebufferCreateInfo-attachmentCount-00876, then a SIGSEGV in
    the driver. The attachment is now only declared when the swapchain has
    a depth/stencil image and the pass uses depth or stencil, and the
    framebuffer and clear values follow the render pass's attachments.
    The subpass dependency now orders the color load, reads (blending) and
    writes after earlier color writes, and with a depth/stencil attachment
    its clear after earlier depth/stencil writes. A render pass whose
    creation or framebuffer failed isn't begun (or ended).
  • RendererVk::SetRenderPass did nothing when given the current pass,
    while RendererGLES ends it and begins it again (clearing again). Both
    now do the latter. SetRenderPass(nullptr) and SetRenderState(nullptr)
    dereferenced the null pointer in RendererVk (and SetRenderState in
    RendererGLES); they now end/clear the current pass or state.
  • RenderPassGLES cleared with whatever write masks and scissor test the
    last render state left, so after a depth_write = false state the depth
    buffer wasn't cleared, and a scissor rect limited the clear. It now
    enables the masks it clears and disables the scissor test first, as a
    Vulkan load operation clears the whole attachment.

…ore ops

Bring RenderStateVk and RenderPassVk in line with their creation
parameters (and with the GLES implementation), and fix GLES clears:

- RenderStateVk ignored blend_enabled, blend_source_function and
  blend_dest_function: blending was always off. The blend functions now
  map to VkBlendFactor, used for both color and alpha as glBlendFunc does.
  They are only read (and range checked) when blending is enabled, in
  both RenderStateVk and RenderStateGLES.
- RenderStateVk ignored the viewport, the scissor rect and scissor_test:
  every draw used the full framebuffer. RendererVk::SetRenderState now
  sets the dynamic viewport and scissor from the render state. Render
  state rects follow GLES (bottom left origin, display orientation), so
  they are mapped (in 64 bits) to the Y flipped, pre-rotated Vulkan
  framebuffer. The scissor is also limited to the viewport: GLES clips
  primitives at the viewport, but Vulkan implementations may draw outside
  it (the Pixel 10 Pro's PowerVR does). Like glViewport, the viewport is
  clamped to the device's maximum viewport size, and kept within its
  viewport bounds; the depth range is clamped to [0, 1] (a NaN becomes 0).
  The pipeline's unused static viewport (from a rect in the wrong
  convention) is gone, and the state's viewport/scissor members, never
  initialized, are now set from the creation parameters.
- SetViewport and SetScissorRect on the bound render state had no effect
  until another render state was bound, in GLES and Vulkan (setting the
  same state again is a no-op). They now mark the state, and the next
  draw applies them.
- RenderPassVk ignored the load/store operations and always cleared and
  stored color and cleared depth. They now come from the creation
  parameters, for the aspects in attachment_flags only; the others are
  don't care and their parameters aren't read. RenderPassGLES now also
  only clears the aspects in attachment_flags.
- RenderPassVk always declared a depth/stencil attachment and reference,
  even for a swapchain without a depth/stencil buffer (format
  VK_FORMAT_UNDEFINED, no image view, one attachment framebuffer):
  VUID-VkAttachmentDescription-format-06698,
  VUID-VkSubpassDescription-pDepthStencilAttachment-02650 and
  VUID-VkFramebufferCreateInfo-attachmentCount-00876, then a SIGSEGV in
  the driver. The attachment is now only declared when the swapchain has
  a depth/stencil image and the pass uses depth or stencil, and the
  framebuffer and clear values follow the render pass's attachments.
  The subpass dependency now orders the color load, reads (blending) and
  writes after earlier color writes, and with a depth/stencil attachment
  its clear after earlier depth/stencil writes. A render pass whose
  creation or framebuffer failed isn't begun (or ended).
- RendererVk::SetRenderPass did nothing when given the current pass,
  while RendererGLES ends it and begins it again (clearing again). Both
  now do the latter. SetRenderPass(nullptr) and SetRenderState(nullptr)
  dereferenced the null pointer in RendererVk (and SetRenderState in
  RendererGLES); they now end/clear the current pass or state.
- RenderPassGLES cleared with whatever write masks and scissor test the
  last render state left, so after a depth_write = false state the depth
  buffer wasn't cleared, and a scissor rect limited the clear. It now
  enables the masks it clears and disables the scissor test first, as a
  Vulkan load operation clears the whole attachment.

Tested on a Pixel 10 Pro (display at 90 degree rotation) with agdktunnel
and Vulkan Validation Layers 1.4.363.1 (with synchronization validation
enabled), using a temporary hook, and compared with GLES:
- A half transparent quad drawn with src alpha / 1 - src alpha blending.
  Before: opaque under Vulkan. After: translucent, matching GLES.
- The tunnel's render state with a bottom left quadrant viewport, and the
  HUD's with a top right quadrant scissor rect. Before: both ignored under
  Vulkan. After: matches GLES (the tunnel is drawn only in the bottom
  left quadrant, the HUD only in the top right).
- The tunnel's viewport changed to the bottom left quadrant after its
  render state is bound. Before: ignored in GLES and Vulkan. After: the
  tunnel is drawn in the quadrant in both.
- A viewport of -100000,-100000 300000x300000 with a NaN min depth.
  Before: VUID-VkViewport-x-01774, y-01776, width-01771, height-01773 and
  minDepth-01234 every frame. After: no errors.
- A main render pass without the stencil aspect and out of range stencil
  load/store values. Before: assertion failure. After: renders.
- A swapchain with no depth/stencil buffer. Before: the errors above and
  a SIGSEGV. After: renders, no errors.
- The main render pass set again after a depth_write = false render
  state, and the tunnel redrawn. GLES before: black (depth not cleared).
  After: the tunnel renders in GLES, and in Vulkan (which now begins the
  pass again).
- The 270 (and 180) degree mappings follow from the application's
  pre-rotation matrices but weren't tested on device: agdktunnel is
  sensorLandscape, so the display only rotates to 270 degrees when the
  phone is physically turned.
- No validation errors other than the semaphore reuse already on main
  (finding android#23, PR 12). Debug and release both build.
@natetrost
natetrost merged commit c21f87b into android:main Nov 23, 2026
7 checks passed
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.

1 participant