Skip to content

D3D11: redundant state setters do nothing, a CPU probe, and depth-stencil initial data in one pass - #358

Merged
dttdrv merged 15 commits into
dttdrv:mainfrom
aaf2tbz:split/d3d11-redundant-state
Nov 26, 2026
Merged

dttdrv merged 15 commits into
dttdrv:mainfrom
aaf2tbz:split/d3d11-redundant-state

Conversation

@aaf2tbz

@aaf2tbz aaf2tbz commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

D3D11 state setters that repeat what the context already has no longer invalidate the pipeline or encode again, and a few per-draw lookups lose their locks. A depth-stencil texture's initial data is split into its planes in one pass. Adds tests/perf/d3d11_perf_probe, which times the immediate context's CPU costs.

What changes

  1. tests: d3d11_perf_probe: API, frame and GPU time for draws, state changes, maps, updates, copies, queries and present.
  2. d3d11: blending is hashed and compared by words its blend state makes once.
  3. d3d11: setters of what the context already has do nothing. OMSetBlendState with an unchanged factor, OMSetDepthStencilState of the same state and reference, a stage cleared that has no shader, the same input layout and index buffer, a vertex buffer at the same offset and stride, RSSetState(nullptr) of the default, and SOSetTargets of an empty slot. Every new pass still marks all state dirty, and ClearState, SwapDeviceContextState and ExecuteCommandList end the pass, so these early-outs are safe.
  4. d3d11: a map that renames a resource marks only the bindings it is bound in, not every stage's tables.
  5. d3d11: a draw finds a pipeline it took lately without the device cache's lock.
  6. mullion: a depth-stencil texture's initial data is split into its planes in two whole-buffer uploads, with loops the compiler vectorizes, instead of two calls a texel. Adds d3d11_depth_initial and its faults.
  7. tests: faults that these changes moved are made again over the code.
Scene (d3d11_perf_probe) Before (ms) After (ms) Speedup
create_depth_initial (1080p D24S8 with initial data) 53.37 1.74 30.7x
draws_same_buffers 1.247 0.35 3.6x
draws_cleared_stages 0.777 0.44 1.8x
draws_states (API) 1.29 0.94 1.37x
mixed (API) 0.70 0.525 1.33x
draws_shaders (API) 1.116 0.93 1.2x
draws_cbs 0.916 0.77 1.19x
draws_same_states 1.061 0.96 1.1x

Evidence

Mac mini, Apple M6, macOS 27.3. MetalSharp Wine 8.0 (arm64 with FEX), copied with --clone. Timings are medians of tests/perf/d3d12_perf_probe and tests/perf/d3d11_perf_probe (run-to-run noise is about 20%).

  • The tests ran on our fork, where these commits sit on top of more work: the native D3D10, D3D11 and D3D12 suites and the D3D12 suite on every GPU family show only failures that are there without them, and every fault patch these commits add is caught.
  • This branch, cherry-picked onto main, builds (ARM64EC, -Denable_nvngx=true -Denable_nvapi=true -Denable_tests=true). It merges cleanly with the other two branches of this set (split/d3d12-pass-stalls, split/d3d11-redundant-state, split/texture-compression) in any order.
  • Fault patches that this change moves are made again over main's code; each keeps its changed lines.

aaf2tbz and others added 15 commits November 25, 2026 17:18
a benchmark run by hand, as d3d12_perf_probe is: each scene records a frame of a cost Direct3D 11 games have on the
immediate context, offscreen at 1080p with two frames in flight, and prints the median and 95th percentile of the
recording's CPU time, of the whole frame and of the GPU's (timestamp queries): draws alone, a WRITE_DISCARD map or
UpdateSubresource of a constant buffer per draw, constant buffers, textures, states, shaders, vertex and index buffers
changed per draw, sprites written with NO_OVERWRITE into a ring, instancing, render target switches, small texture
uploads, occlusion queries, a staging readback and a mixed game-like frame.
…d state makes once

a draw after a change of blend state, sample mask or shader looks its pipeline up by its description, whose hash
left the blend state out, so every pipeline of the same shaders and targets fell in one bucket, and comparing two
blend states copied both descriptions (GetDesc1) and compared them field by field. a blend state now makes, when it
is made, a word for each target of the fields a pipeline takes of it (those of blending only where it blends, the
logic op only where it has one) and one of its flags: the hash takes those of the pipeline's targets, and two states
blend alike for a pipeline exactly where they are equal (PipelineEqual), as the fields compared before.

d3d11_blending, d3d11_format_blend, d3d11_logic, d3d11_format_logic, d3d11_coverage_blend, d3d11_om_order,
d3d11_draws, d3d10_blend, d3d11_sample_mask and d3d11_constant_updates pass on native and native-metal3.
d3d11_perf_probe (M6): draws_states recording 1.256 to 1.152 ms. it also gains targets_noclear.
every OMSetBlendState marked the blend factor and stencil reference to be encoded at the next draw, the same factor
or not: an engine changing blend states between draws, with the factor it always has, set the blend color for each
(d3d11_perf_probe draws_states, 10000 a frame: wmtcmd_render_setblendcolor was 5.4% of the encoding thread). the
factor is marked when it differs from the one the context has. the stencil reference needs no mark of its own:
OMSetDepthStencilState marks the depth-stencil state, whose command carries it, and every pass, ClearState,
SwapDeviceContextState and ExecuteCommandList start the next pass with all of it marked. M6, frame: draws_states
1.191 and 1.279 to 1.113 and 1.114 ms.

tests/breaks/d3d11_blending.5.patch, a changed factor never encoded, is caught by d3d11_blending;
d3d11_sample_mask.patch is made again over the new lines and is caught. the d3d10 and d3d11 tests pass on native but
for the failures they have at the base, and the blending, state and command list tests on native and 7-metal3.
…has encodes nothing

an engine that sets every state before every draw had its depth-stencil state encoded again for each, where
RSSetState already returns for the state it has. the state and stencil reference the context has are encoded
already: every pass starts with them marked, and ClearState, SwapDeviceContextState and ExecuteCommandList end the
pass first. d3d11_perf_probe gains draws_same_states, 10000 draws each setting the blend, depth-stencil and
rasterizer states they have. M6, frame, with this and the blend factor's change against neither: draws_same_states
1.061 to 0.957 and 0.961 ms, recording 0.453 to 0.394 and 0.401 ms.

tests/breaks/d3d11_depth_stencil.4.patch, a stencil reference set again with the same state left out, is caught, as
are the other depth-stencil faults. the d3d10 and d3d11 tests pass on native but for the failures they have at the
base, and the depth-stencil, state and command list tests on native and 7-metal3.
XXSetShader(nullptr) on a stage with no shader still invalidated the render pipeline, so a state manager that clears
the hull, domain and geometry stages before every draw had each draw build its pipeline description, look it up and
set it in the encoder again. a stage cleared that has no shader now returns, as one set to the shader it has does.
d3d11_perf_probe gains draws_cleared_stages, 10000 draws each clearing the three. M6: frame 0.777 to 0.454 and
0.435 ms, recording 0.772 to 0.265 and 0.260 ms.

tests/breaks/d3d11_no_vertex_shader.2.patch, a stage cleared of the shader it has keeping it, is caught. the d3d10
and d3d11 tests pass on native but for the failures they have at the base, and the shader stage, stream output,
geometry and tessellation tests on native and 7-metal3.
…again

IASetInputLayout of the layout the context had invalidated the render pipeline, so each draw after it built its
pipeline description, looked it up and set it in the encoder again; IASetIndexBuffer of the buffer, format and offset
it had bound it again on the encoding thread. both now return, as the other setters do for what they have.
d3d11_perf_probe gains draws_same_buffers, 10000 indexed draws each setting the layout, vertex and index buffer they
have. M6, frame: draws_same_buffers 1.247 to 0.344 and 0.350 ms; draws_buffers, whose index buffer is the same for
eight draws at a time, 1.007 to 0.725 and 0.616 ms.

d3d11_ia_draws binds each index buffer at offset 0 before its own offset, so a binding that differs in the offset
alone is taken; tests/breaks/d3d11_ia_draws.4.patch, which keeps the old offset, is caught. the d3d10 and d3d11 tests
pass on native but for the failures they have at the base, and the input assembler, state and command list tests on
native and 7-metal3.
…tState(nullptr) of the default, encode nothing

IASetVertexBuffers of the buffer a slot has, at the offset and stride it has, still bound its offset again on the
encoding thread, and RSSetState(nullptr) with no rasterizer state set marked the rasterizer state to be encoded at the
next draw, where RSSetState of the state the context has returns. both now leave what is encoded as it is. M6,
recording: d3d11_perf_probe draws_same_buffers 0.342 and 0.339 to 0.315, 0.309 and 0.316 ms.

the d3d10 and d3d11 tests pass on native but for the failures they have at the base, and the input assembler,
rasterizer state, viewport, state and command list tests on native and 7-metal3.
…List as it is

FinishCommandList keeps the list it hands back in a variable of its own since 965d855, and the patch's context no
longer applied. the fault is the same, the restore flag reversed, and d3d11_commandlist_state catches it.
…und in

Map(WRITE_DISCARD) of a dynamic buffer or texture marked every stage's constant buffers, shader resources or vertex
buffers to be encoded again, as the bind flags allowed, wherever the resource was bound or not. a stage's table is
encoded whole when any slot its shader uses is marked, so a constant buffer mapped for each draw that only the pixel
shader binds had the vertex shader's constant buffers encoded and tracked again for every draw too. DirtyBindingsOf
marks the slots the resource is bound in, on the immediate and deferred contexts. d3d11_perf_probe gains
draws_map_ps, 10000 draws each mapping a constant buffer only the pixel shader binds while the vertex shader reads
one of its own. M6, frame: draws_map_ps 1.292 to 1.192 and 1.191 ms.

d3d11_dynamic_generations' pixel shader reads the constant buffer too, which it binds as the vertex shader does;
tests/breaks/d3d11_dynamic_generations.9.patch, a rename marking the vertex stage alone, is caught, and the patches
over the lines this replaces (d3d11_dynamic_generations.2, .5 and .6, d3d11_map_contract.3) are made again over
DirtyBindingsOf and are caught. d3d11_dynamic_generations.patch, .4 and .8 are not caught at the base either. the
d3d10 and d3d11 tests pass on native but for the failures they have at the base, and the map and update tests on
native and 7-metal3.
… code as it is

d3d11_commandlist_state.13, d3d11_default_updates.8 and .9, d3d11_queries and d3d11_staging_order.3 applied at
8fe7eb2 and not after this branch: the lines they change moved into the copies' and uploads' prelude paths, the
vertex buffer setter and the query's fields. each is the same fault over the code as it is, the copy offsets' faults
over the renamed copies too, and each is caught by its test. every other patch of tests/breaks that does not apply
here did not apply at 8fe7eb2 either.
SOSetTargets unbound every slot it was not given a target for on the encoding thread, bound or not: a state manager
that clears the stream output targets before every draw encoded four unbindings for each. a slot is unbound only when
it has a target, as a shader resource slot is; the encoder's targets are the context's, as ClearState,
FinishCommandList and ExecuteCommandList clear both. d3d11_perf_probe gains draws_cleared_so. M6, recording:
0.312 to 0.241 and 0.248 ms.

the d3d10 and d3d11 tests pass on native but for the failures they have at the base, and the stream output, state and
command list tests on native and 7-metal3.
…e's lock

each draw after a change of shaders or states looked its pipeline up in the device's cache, under its lock and
through its table. the context keeps the 64 it took last by their description's hash, and takes one of those when the
description is equal, as the device's table would; the device keeps every pipeline it makes, and its description,
for as long as it lives. M6, recording, two runs against one: d3d11_perf_probe draws_states 1.197 to 0.940 and 0.949
ms, draws_shaders 1.116 to 0.935 and 0.925 ms, mixed 0.560 to 0.526 and 0.523 ms.

tests/breaks/d3d11_blending.6.patch, the cache taking the pipeline it holds whatever the description, is caught. the
d3d10 and d3d11 tests pass on native but for the failures they have at the base, and the blending, state object,
sample mask and command list tests on native and 7-metal3.
…nes at once, not two calls a texel

ResourceInitializer::initDepthStencilWithData wrote each texel's depth and stencil into the upload buffers with a
call into the Metal side apiece: two million calls for a 1080p D24S8 texture made with initial data. the planes are
now split in loops the compiler vectorizes (NEON on ARM64EC), into a depth plane and a stencil plane each written at
once. the values are the same: D24's depth is still its 24 bits over 2^24 - 1. d3d11_perf_probe gains
create_depth_initial, a 1080p D24S8 texture made with initial data each frame: M6, 53.37 to 1.74 ms.

d3d11_depth_initial is new: D24S8 and D32S8 textures of odd and multi-tile sizes, made with padded initial data and
read back through staging, give back every depth and stencil as given. tests/breaks/d3d11_depth_initial.patch and .2
(a depth off by its scale, a stencil from the wrong byte) are caught. the d3d10 and d3d11 tests pass on native but for
the failures they have at the base, and the depth and initial-data tests on native and 7-metal3.
# Conflicts:
#	tests/d3d11/meson.build
@dttdrv
dttdrv merged commit c824041 into dttdrv:main Nov 26, 2026
19 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.

2 participants