anti_aliasing.rasm

src/4.advanced_d3d11/11.1.anti_aliasing/anti_aliasing.rasm · 121 lines

; 11.1 Anti-aliasing
;
; A pixel is either inside a triangle or not, so edges come out as stairs.
; Multisampling (MSAA) tests coverage at several points per pixel (four here)
; while still running the pixel shader once, and stores a colour per sample.
; Resolving averages the samples: a pixel half covered by the cube ends up
; half green, and the stairs soften into a slope.
;
; The swap chain cannot be multisampled itself (flip-model swap chains never
; are), which is LearnOpenGL's offscreen setup anyway: the cube is drawn into
; a 4x MSAA target with a matching 4x depth buffer, then ResolveSubresource
; averages it straight into the back buffer.
;
;   Tab   MSAA on / off
import "../../../includes/d3d11.rin"
import "../../../includes/calls.rasm"
import "../../../includes/math.rasm"
import "../../../includes/learn/window.rasm"
import "../../../includes/learn/shader.rasm"
import "../../../includes/learn/gpu.rasm"
import "../../../includes/learn/cube.rasm"
import "../../../includes/learn/camera.rasm"
import "../../../includes/learn/transforms.rasm"

#define SAMPLES 4

camera_start: const vec3 = { 0.0, 0.0, 3.0 }
tilt_axis: const vec3 = { 0.4, 1.0, 0.2 }
clear_color: [4]const f32 = { 0.1, 0.1, 0.1, 1.0 }

t: transforms = {}
model: mat4 = {}
s: shader = {}
cam: camera = {}
msaa: render_target = {}
msaa_depth: *ID3D11DepthStencilView = {}
target: *ID3D11RenderTargetView = {}           ; this frame's: OMSetRenderTargets takes an array of one
target_depth: *ID3D11DepthStencilView = {}
cube_vb: *ID3D11Buffer = {}
transforms_buffer: *ID3D11Buffer = {}
msaa_off: u32 = 0

; timing
delta_time: f32 = 0.0
last_frame: f32 = 0.0

main: proc() -> i32 = {
    sub rsp, 56                                ; ResolveSubresource takes 6: 32 + 16, plus 8 to land on 16

    invoke(window_create, "LearnD3D11")
    call window_create_depth

    invoke(shader_create, s&, "src/4.advanced_d3d11/11.1.anti_aliasing/anti_aliasing.hlsl", cube_layout&, CUBE_LAYOUT_COUNT)
    invoke(create_buffer, D3D11_BIND_FLAG.D3D11_BIND_VERTEX_BUFFER, cube_vertices&, sizeof(cube_vertices))
    mov cube_vb, rax
    invoke(create_constant_buffer, sizeof(transforms))
    mov transforms_buffer, rax

    lea rcx, cam
    lea rdx, camera_start
    movss xmm2, -90.0                          ; yaw
    movss xmm3, 0.0                            ; pitch
    call camera_create

    ; a 4x multisampled colour target, and a depth buffer with the same sample count
    invoke(create_render_target, msaa&, SCR_WIDTH, SCR_HEIGHT, DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM, SAMPLES)
    invoke(create_depth_buffer, SCR_WIDTH, SCR_HEIGHT, SAMPLES)
    mov msaa_depth, rax

  render_loop:
    call window_poll
    test eax, eax
    jz render_loop_done

    call get_time
    movss xmm1, xmm0
    subss xmm0, last_frame
    movss delta_time, xmm0
    movss last_frame, xmm1

    invoke(camera_process_input, cam&, delta_time&)
    invoke(window_mode, 2)
    mov msaa_off, eax

    ; into the MSAA target, or with it off straight into the window
    mov rax, msaa.rtv
    mov rdx, msaa_depth
    cmp msaa_off, 0
    je chosen
    mov rax, backbuffer_rtv
    mov rdx, depth_dsv
  chosen:
    mov target, rax
    mov target_depth, rdx
    vcall(context, ID3D11DeviceContextVtbl.OMSetRenderTargets, 1, target&, target_depth)
    vcall(context, ID3D11DeviceContextVtbl.RSSetViewports, 1, viewport&)
    vcall(context, ID3D11DeviceContextVtbl.ClearRenderTargetView, target, clear_color&)
    movss xmm3, 1.0                            ; the depth, a float: xmm3
    vcall(context, ID3D11DeviceContextVtbl.ClearDepthStencilView, target_depth, D3D11_CLEAR_FLAG.D3D11_CLEAR_DEPTH, 0, 0)
    vcall(context, ID3D11DeviceContextVtbl.IASetPrimitiveTopology, D3D11_PRIMITIVE_TOPOLOGY.D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST)
    invoke(bind_constants, 0, transforms_buffer)
    invoke(transforms_from_camera, t&, cam&)
    invoke(shader_use, s&)
    movss xmm1, 0.6                            ; the angle, m4_rotate's second argument
    lea rcx, model
    lea r8, tilt_axis
    call m4_rotate
    invoke(draw_vertices, transforms_buffer, t&, cube_vb, 36, model&)

    ; average each pixel's four samples into the back buffer
    cmp msaa_off, 0
    jne no_resolve
    vcall(context, ID3D11DeviceContextVtbl.ResolveSubresource, backbuffer, 0, msaa.texture, 0, DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM)
  no_resolve:

    call window_present
    jmp render_loop
  render_loop_done:

    invoke(ExitProcess, 0)
}
Part of rasm-dx11, LearnOpenGL's lessons rewritten for Direct3D 11 in rasm. Licensed CC BY-NC 4.0, after LearnOpenGL by Joey de Vries.