pbr.rasm

includes/learn/pbr.rasm · 394 lines

; pbr: what the PBR lessons (chapter 6) share
;
;   create_sphere         a UV sphere of radius 1, indexed
;   draw_sphere           draw it with a model matrix
;   load_material         the five maps of one PBR material from a folder
;   create_cube_target    a float cubemap that can be drawn into
;   render_to_cube        draw the bound shader over all six faces of one of its mips
;
; It imports srgb.rasm too, since every lesson here draws into the sRGB view
; of the back buffer (window_use_srgb).

import "../d3d11.rin"
import "../calls.rasm"
import "../macros.rasm"
import "../math.rasm"
import "../saha.rasm"
import "../cstr.rasm"
import "window.rasm"
import "gpu.rasm"
import "filesystem.rasm"
import "transforms.rasm"
import "srgb.rasm"

; A UV sphere of radius 1, indexed: `segments` slices around and as many
; stacks from pole to pole. u runs around the equator and v from the top pole
; down, the way an equirectangular texture wraps it.
sphere: struct = {
    vertices: *ID3D11Buffer                ; 32-byte vertices: position, normal, uv
    indices: *ID3D11Buffer                 ; 32-bit indices
    index_count: u32
    pad: u32
}

; A cubemap of half floats that can be drawn into, face by face and mip by mip.
cube_target: struct = {
    texture: *ID3D11Texture2D              ; an array of six
    srv: *ID3D11ShaderResourceView         ; of it as a cube, every mip
    size: u32                              ; of a face at mip 0, in pixels
    pad: u32
}

; the five maps of a material, in their registers' order t0..t4
pbr_map_names: [5]const *const c8 = { "albedo.png", "normal.png", "metallic.png", "roughness.png", "ao.png" }

; the direction each cube face looks along, and which way is up in it: +x, -x, +y, -y, +z, -z
cube_face_targets: [6]const vec3 = {
    {  1.0,  0.0,  0.0 },
    { -1.0,  0.0,  0.0 },
    {  0.0,  1.0,  0.0 },
    {  0.0, -1.0,  0.0 },
    {  0.0,  0.0,  1.0 },
    {  0.0,  0.0, -1.0 },
}
cube_face_ups: [6]const vec3 = {
    {  0.0, -1.0,  0.0 },
    {  0.0, -1.0,  0.0 },
    {  0.0,  0.0,  1.0 },
    {  0.0,  0.0, -1.0 },
    {  0.0, -1.0,  0.0 },
    {  0.0, -1.0,  0.0 },
}
cube_face_eye: const vec3 = {}

; create_sphere's: masm64-dx11's locals
create_sphere_vertices: *u8 = {}
create_sphere_indices: *u8 = {}
create_sphere_segments: f32 = 0.0          ; as a float, the divisor of u and v
create_sphere_u: f32 = 0.0
create_sphere_v: f32 = 0.0
create_sphere_cos_u: f32 = 0.0
create_sphere_sin_u: f32 = 0.0
create_sphere_cos_v: f32 = 0.0
create_sphere_sin_v: f32 = 0.0

; rcx: sphere, rdx: segments
create_sphere: proc(out: *sphere, segments: u64) = {
    push rbx
    push rsi
    push rdi
    push r12
    push r13
    push r14
    push r15
    sub rsp, 32                            ; 32 shadow; entry 8 + seven pushes 56 + 32 lands on 16
    mov rbx, rcx                           ; out
    mov r14, rdx                           ; segments
    lea r15, [rdx + 1]                     ; ring: vertices per stack, and stacks
    cvtsi2ss xmm0, r14
    movss create_sphere_segments, xmm0

    ; the vertices: (segments + 1)^2 of them, the seam and the poles repeated so u and v reach 1
    mov rax, r15
    mul r15
    shl rax, 5
    invoke(arenaPush, arena&, rax, 16)
    mov create_sphere_vertices, rax
    mov rsi, rax
    xor r12, r12                           ; y
  stacks:
    xor r13, r13                           ; x
  slices:
    cvtsi2ss xmm0, r13
    divss xmm0, create_sphere_segments
    movss create_sphere_u, xmm0
    cvtsi2ss xmm0, r12
    divss xmm0, create_sphere_segments
    movss create_sphere_v, xmm0
    ; sinf and cosf use their shadow space, which this frame provides
    movss xmm0, create_sphere_u
    addss xmm0, xmm0
    mulss xmm0, math_pi                    ; 2 pi u
    movss create_sphere_sin_u, xmm0
    call cosf
    movss create_sphere_cos_u, xmm0
    movss xmm0, create_sphere_sin_u
    call sinf
    movss create_sphere_sin_u, xmm0
    movss xmm0, create_sphere_v
    mulss xmm0, math_pi                    ; pi v
    movss create_sphere_sin_v, xmm0
    call cosf
    movss create_sphere_cos_v, xmm0
    movss xmm0, create_sphere_sin_v
    call sinf
    movss create_sphere_sin_v, xmm0
    ; p = (cos(2 pi u) sin(pi v), cos(pi v), sin(2 pi u) sin(pi v)), which is its normal too
    movss xmm0, create_sphere_cos_u
    mulss xmm0, create_sphere_sin_v
    movss dword ptr [rsi + 0], xmm0        ; position.x
    movss dword ptr [rsi + 12], xmm0       ; normal.x
    movss xmm0, create_sphere_cos_v
    movss dword ptr [rsi + 4], xmm0
    movss dword ptr [rsi + 16], xmm0
    movss xmm0, create_sphere_sin_u
    mulss xmm0, create_sphere_sin_v
    movss dword ptr [rsi + 8], xmm0
    movss dword ptr [rsi + 20], xmm0
    movss xmm0, create_sphere_u
    movss dword ptr [rsi + 24], xmm0       ; uv
    movss xmm0, create_sphere_v
    movss dword ptr [rsi + 28], xmm0
    add rsi, 32
    inc r13
    cmp r13, r15
    jb slices
    inc r12
    cmp r12, r15
    jb stacks

    ; two counter-clockwise triangles between each pair of stacks and slices
    mov rax, r14
    mul r14
    imul rax, rax, 6 * 4
    invoke(arenaPush, arena&, rax, 16)
    mov create_sphere_indices, rax
    mov rdi, rax
    xor r12, r12                           ; y
  index_stacks:
    xor r13, r13                           ; x
  index_slices:
    mov rax, r12
    mul r15                                ; (rdx too: no call in this loop)
    add rax, r13                           ; a: this stack, this slice
    lea r8, [rax + r15]                    ; b: the stack below
    mov dword ptr [rdi + 0], eax           ; a, a + 1, b + 1
    lea ecx, [eax + 1]
    mov dword ptr [rdi + 4], ecx
    lea ecx, [r8d + 1]
    mov dword ptr [rdi + 8], ecx
    mov dword ptr [rdi + 12], eax          ; a, b + 1, b
    mov dword ptr [rdi + 16], ecx
    mov dword ptr [rdi + 20], r8d
    add rdi, 24
    inc r13
    cmp r13, r14
    jb index_slices
    inc r12
    cmp r12, r14
    jb index_stacks

    mov rax, rdi
    sub rax, create_sphere_indices
    shr rax, 2
    mov [rbx].sphere.index_count, eax
    mov rax, r15
    mul r15
    shl rax, 5
    invoke(create_buffer, D3D11_BIND_FLAG.D3D11_BIND_VERTEX_BUFFER, create_sphere_vertices, rax)
    mov [rbx].sphere.vertices, rax
    mov rax, rdi
    sub rax, create_sphere_indices
    invoke(create_buffer, D3D11_BIND_FLAG.D3D11_BIND_INDEX_BUFFER, create_sphere_indices, rax)
    mov [rbx].sphere.indices, rax
    add rsp, 32
    pop r15
    pop r14
    pop r13
    pop r12
    pop rdi
    pop rsi
    pop rbx
    ret
}

; draw_sphere's: IASetVertexBuffers takes arrays of one
draw_sphere_stride: u32 = 32
draw_sphere_offset: u32 = 0

; rcx: constant buffer, rdx: transforms, r8: sphere, r9: model matrix
draw_sphere: proc(buffer: *ID3D11Buffer, t: *transforms, s: *const sphere, model: *const mat4) = {
    push rbx
    push rsi
    push rdi
    push r12
    sub rsp, 56                            ; IASetVertexBuffers: 5 past `this`, 2 on the stack: 32 + 16; entry 8 + 32 + 56 lands on 16
    mov r12, rcx                           ; buffer
    mov rsi, rdx                           ; t
    mov rbx, r8                            ; the sphere
    mov rdi, r9                            ; model
    lea rax, [rbx].sphere.vertices
    vcall(context, ID3D11DeviceContextVtbl.IASetVertexBuffers, 0, 1, rax, draw_sphere_stride&, draw_sphere_offset&)
    vcall(context, ID3D11DeviceContextVtbl.IASetIndexBuffer, [rbx].sphere.indices, DXGI_FORMAT.DXGI_FORMAT_R32_UINT, 0)
    m4_copy(rsi, rdi)                      ; t.model is at offset 0
    lea rcx, [rsi].transforms.normal_matrix
    invoke(m4_normal_matrix, rcx, rdi)
    invoke(update_buffer, r12, rsi, sizeof(transforms))
    mov eax, [rbx].sphere.index_count
    vcall(context, ID3D11DeviceContextVtbl.DrawIndexed, rax, 0, 0)
    add rsp, 56
    pop r12
    pop rdi
    pop rsi
    pop rbx
    ret
}

load_material_path: [260]c8 = {}          ; load_material's: folder + map name

; rcx: folder relative to the repository root (ending in a slash), rdx: where
; to put the five views (albedo, normal, metallic, roughness, ao)
;
; Only albedo is a colour, decoded from sRGB when sampled; the other maps are data.
load_material: proc(folder: *const c8, out: **ID3D11ShaderResourceView) = {
    push rbx
    push rsi
    push rdi
    sub rsp, 32                            ; 32 shadow; entry 8 + three pushes 24 + 32 lands on 16
    mov rdi, rdx                           ; out
    invoke(filesystem_path, rcx)
    mov rsi, rax                           ; the folder's full path
    xor ebx, ebx
  maps:
    invoke(cstr_copy, load_material_path&, rsi)
    lea rax, pbr_map_names
    mov rdx, [rax + rbx * 8]
    invoke(cstr_append, load_material_path&, rdx)
    xor edx, edx
    test ebx, ebx
    setz dl                                ; srgb for the first, albedo
    invoke(load_texture, load_material_path&, rdx)
    mov [rdi + rbx * 8], rax
    inc ebx
    cmp ebx, 5
    jb maps
    add rsp, 32
    pop rdi
    pop rsi
    pop rbx
    ret
}

; create_cube_target's: everything but the size, the mip count and whether
; the GPU generates the mips
create_cube_target_desc: D3D11_TEXTURE2D_DESC = {
    .ArraySize = 6,
    .Format = DXGI_FORMAT.DXGI_FORMAT_R16G16B16A16_FLOAT,
    .SampleDesc = { .Count = 1 },
    .BindFlags = D3D11_BIND_FLAG.D3D11_BIND_RENDER_TARGET | D3D11_BIND_FLAG.D3D11_BIND_SHADER_RESOURCE,
}
create_cube_target_view: D3D11_SHADER_RESOURCE_VIEW_DESC = {
    .Format = DXGI_FORMAT.DXGI_FORMAT_R16G16B16A16_FLOAT,
    .ViewDimension = D3D_SRV_DIMENSION.D3D_SRV_DIMENSION_TEXTURECUBE,
    .TextureCube = { .MipLevels = 0xFFFFFFFF },    ; -1: all of them
}

; rcx: cube_target, edx: face size, r8d: mip levels (0: all of them), r9d: 1
; to let the GPU generate the mips
create_cube_target: proc(out: *cube_target, face_size: u32, mips: u32, generate_mips: b32) = {
    push rbx
    sub rsp, 32                            ; 32 shadow; entry 8 + push 8 + 32 lands on 16
    mov rbx, rcx
    mov [rbx].cube_target.size, edx
    mov create_cube_target_desc.Width, edx
    mov create_cube_target_desc.Height, edx
    mov create_cube_target_desc.MipLevels, r8d
    mov create_cube_target_desc.MiscFlags, D3D11_RESOURCE_MISC_FLAG.D3D11_RESOURCE_MISC_TEXTURECUBE
    test r9d, r9d
    jz no_generate
    mov create_cube_target_desc.MiscFlags, D3D11_RESOURCE_MISC_FLAG.D3D11_RESOURCE_MISC_TEXTURECUBE | D3D11_RESOURCE_MISC_FLAG.D3D11_RESOURCE_MISC_GENERATE_MIPS
  no_generate:
    lea rax, [rbx].cube_target.texture
    vcall(device, ID3D11DeviceVtbl.CreateTexture2D, create_cube_target_desc&, 0, rax)
    assert_hr()
    lea rax, [rbx].cube_target.srv
    vcall(device, ID3D11DeviceVtbl.CreateShaderResourceView, [rbx].cube_target.texture, create_cube_target_view&, rax)
    assert_hr()
    add rsp, 32
    pop rbx
    ret
}

; render_to_cube's. The model matrix is the identity, so it is data; the
; transforms' view_position is never read by these shaders and stays 0.
render_to_cube_t: transforms = {}
render_to_cube_model: const mat4 = { .m = { 1.0, 0.0, 0.0, 0.0,  0.0, 1.0, 0.0, 0.0,  0.0, 0.0, 1.0, 0.0,  0.0, 0.0, 0.0, 1.0 } }
render_to_cube_rtv_desc: D3D11_RENDER_TARGET_VIEW_DESC = {
    .Format = DXGI_FORMAT.DXGI_FORMAT_R16G16B16A16_FLOAT,
    .ViewDimension = D3D11_RTV_DIMENSION.D3D11_RTV_DIMENSION_TEXTURE2DARRAY,
    .Texture2DArray = { .ArraySize = 1 },  ; one face at a time
}
render_to_cube_rtv: *ID3D11RenderTargetView = {}

; rcx: cube_target, edx: mip, r8: transforms constant buffer, r9: the unit
; cube's vertex buffer
;
; Draws the unit cube around the origin once per face, through a 90-degree
; view looking out of that face, with whatever shader is bound: each face's
; pixels see the directions that face covers. A cubemap's faces are laid out
; left-handed, which no right-handed look-at produces; flipping the
; projection's y fixes that, as GL's bottom-up framebuffer does for free
; there.
render_to_cube: proc(target: *cube_target, mip: u32, constants: *ID3D11Buffer, cube_vb: *ID3D11Buffer) = {
    push rbx
    push rsi
    push rdi
    push r12
    push r13
    sub rsp, 48                            ; draw_vertices takes 5: 32 + 8; entry 8 + five pushes 40 + 48 lands on 16
    mov rbx, rcx                           ; target
    mov edi, edx                           ; mip
    mov r12, r8                            ; constants
    mov r13, r9                            ; cube_vb

    ; projection = scale(1, -1, 1) * perspective(90 degrees, 1, 0.1, 10)
    movss xmm0, 10.0
    movss dword ptr [rsp + 32], xmm0       ; z_far
    movss xmm0, 90.0
    call radians
    movaps xmm1, xmm0                      ; fovy
    movss xmm2, math_one                   ; aspect
    movss xmm3, 0.1                        ; z_near
    lea rcx, render_to_cube_t.projection
    call m4_perspective
    ; the second row, m[4..7], negated: four floats at once
    movups xmm0, xmmword ptr render_to_cube_t.projection.m[4]
    xorps xmm0, math_sign_mask
    movups xmmword ptr render_to_cube_t.projection.m[4], xmm0

    ; the face's size at this mip
    mov ecx, edi
    mov eax, [rbx].cube_target.size
    shr eax, cl
    invoke(set_viewport, rax, rax)

    ; Texture2DArray is a member of the view's anonymous union, at offset 8
    ; after Format and ViewDimension: named straight through it, as in C.
    mov render_to_cube_rtv_desc.Texture2DArray.MipSlice, edi
    xor esi, esi                           ; face
  faces:
    mov render_to_cube_rtv_desc.Texture2DArray.FirstArraySlice, esi
    vcall(device, ID3D11DeviceVtbl.CreateRenderTargetView, [rbx].cube_target.texture, render_to_cube_rtv_desc&, render_to_cube_rtv&)
    assert_hr()
    vcall(context, ID3D11DeviceContextVtbl.OMSetRenderTargets, 1, render_to_cube_rtv&, 0)
    imul eax, esi, sizeof(vec3)
    lea r8, cube_face_targets
    add r8, rax
    lea r9, cube_face_ups
    add r9, rax
    lea rdx, cube_face_eye
    lea rcx, render_to_cube_t.view
    call m4_look_at
    invoke(draw_vertices, r12, render_to_cube_t&, r13, 36, render_to_cube_model&)
    release(render_to_cube_rtv)
    inc esi
    cmp esi, 6
    jb faces
    add rsp, 48
    pop r13
    pop r12
    pop rdi
    pop rsi
    pop rbx
    ret
}
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.