cubemaps.rasm

src/4.advanced_d3d11/6.1.cubemaps/cubemaps.rasm · 283 lines

; 6.1 Cubemaps
;
; A cubemap is six images on the inside of a cube, read by direction rather
; than by uv: give it a vector and it returns what you would see looking that
; way from the cube's centre. Two uses:
;
;   skybox      a cube around the camera, textured with the sky. Drawn without
;               the view's translation, so it turns with the camera but can
;               never be reached. Its depth is forced to the far plane (z = w),
;               so it only fills what nothing else covered
;   reflection  each pixel of an object looks up the sky in the direction the
;               eye ray bounces off it. Refraction bends the ray instead, by
;               the ratio of the two materials' indices (air to glass)
;
; In D3D a cubemap is a texture array of six slices with the TEXTURECUBE flag,
; faces in the same order as GL: +x, -x, +y, -y, +z, -z. load_cubemap
; (includes/learn/gpu.rasm) makes one.
;
;   Tab   reflect / refract
import "../../../includes/d3d11.rin"
import "../../../includes/calls.rasm"
import "../../../includes/math.rasm"
import "../../../includes/saha.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"

; Mirrors `cbuffer env` in cubemaps.hlsl.
env_constants: struct = {
    refract_mode: u32                      ; 0 reflect, 1 refract
    pad: [3]u32
}

camera_start: const vec3 = { 0.0, 0.5, 4.0 }
cube_position: const vec3 = { -1.0, 0.0, 0.0 }
sphere_position: const vec3 = { 1.2, 0.0, 0.0 }
y_axis: const vec3 = { 0.0, 1.0, 0.0 }
clear_color: [4]const f32 = { 0.1, 0.1, 0.1, 1.0 }

; the six faces, +x, -x, +y, -y, +z, -z: string literals in an initialiser
; are the addresses of pooled strings
face_names: [6]const *const c8 = {
    "resources/textures/skybox/right.jpg",
    "resources/textures/skybox/left.jpg",
    "resources/textures/skybox/top.jpg",
    "resources/textures/skybox/bottom.jpg",
    "resources/textures/skybox/front.jpg",
    "resources/textures/skybox/back.jpg",
}
faces: [6]*const c8 = {}                   ; the same, as full paths

; The sky is drawn where the depth is still the far plane, and must not write
; depth itself.
sky_depth_desc: D3D11_DEPTH_STENCIL_DESC = {
    .DepthEnable = 1,
    .DepthWriteMask = D3D11_DEPTH_WRITE_MASK.D3D11_DEPTH_WRITE_MASK_ZERO,
    .DepthFunc = D3D11_COMPARISON_FUNC.D3D11_COMPARISON_LESS_EQUAL,
}

t: transforms = {}
env: env_constants = {}
model: mat4 = {}
rotation: mat4 = {}
s: shader = {}
skybox_shader: shader = {}
cam: camera = {}
cube_vb: *ID3D11Buffer = {}
sphere_vb: *ID3D11Buffer = {}
sphere_count: u64 = 0
transforms_buffer: *ID3D11Buffer = {}
env_buffer: *ID3D11Buffer = {}
skybox: *ID3D11ShaderResourceView = {}
sampler: *ID3D11SamplerState = {}
sky_depth: *ID3D11DepthStencilState = {}

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

; create_sphere's
; each quad of the sphere: six corners as (x, y) steps, two counter-clockwise triangles
sphere_corners: [12]const u32 = { 0, 0,  1, 0,  1, 1,  0, 0,  1, 1,  0, 1 }
create_sphere_u: f32 = 0.0
create_sphere_v: f32 = 0.0
create_sphere_sin_v: f32 = 0.0
create_sphere_vertex_count: u64 = 0
create_sphere_vertices: *f32 = {}

; ecx: segments -> rax: a vertex buffer of a unit sphere, rdx: its vertex count
;
; A grid of segments x segments quads wrapped around: u goes round the
; equator, v from the north pole (v = 0) to the south. Position and normal are
; the same point on the unit sphere. The vertices are pushed onto the arena,
; which shader_create's file reading has already started.
create_sphere: proc(segments: u32) -> *ID3D11Buffer = {
    push rbx
    push rsi
    push rdi
    push r12
    push r13
    push r14
    sub rsp, 40                            ; 32 shadow; entry 8 + six pushes 48 + 40 lands on 16
    mov r14d, ecx                          ; segments
    mov eax, ecx
    imul eax, eax
    imul eax, eax, 6
    mov create_sphere_vertex_count, rax
    shl rax, 5                             ; 32 bytes a vertex
    invoke(arenaPush, arena&, rax, 16)
    mov create_sphere_vertices, rax
    mov rdi, rax                           ; the next vertex
    xor r12d, r12d                         ; y
  stacks:
    xor r13d, r13d                         ; x
  slices:
    lea rsi, sphere_corners
    xor ebx, ebx
  corners:
    ; u = (x + dx) / segments, v = (y + dy) / segments
    mov eax, [rsi + rbx * 8]
    add eax, r13d
    cvtsi2ss xmm0, eax
    cvtsi2ss xmm1, r14d
    divss xmm0, xmm1
    movss create_sphere_u, xmm0
    mov eax, [rsi + rbx * 8 + 4]
    add eax, r12d
    cvtsi2ss xmm0, eax
    divss xmm0, xmm1
    movss create_sphere_v, xmm0
    ; p = (cos(2 pi u) sin(pi v), cos(pi v), sin(2 pi u) sin(pi v))
    mulss xmm0, math_pi
    call sinf
    movss create_sphere_sin_v, xmm0
    movss xmm0, create_sphere_v
    mulss xmm0, math_pi
    call cosf
    movss dword ptr [rdi + 4], xmm0        ; position.y
    movss dword ptr [rdi + 16], xmm0       ; normal.y
    movss xmm0, create_sphere_u
    addss xmm0, xmm0
    mulss xmm0, math_pi
    call cosf
    mulss xmm0, create_sphere_sin_v
    movss dword ptr [rdi + 0], xmm0        ; position.x
    movss dword ptr [rdi + 12], xmm0       ; normal.x
    movss xmm0, create_sphere_u
    addss xmm0, xmm0
    mulss xmm0, math_pi
    call sinf
    mulss xmm0, create_sphere_sin_v
    movss dword ptr [rdi + 8], xmm0        ; position.z
    movss dword ptr [rdi + 20], xmm0       ; normal.z
    movss xmm0, create_sphere_u
    movss dword ptr [rdi + 24], xmm0       ; uv
    movss xmm0, create_sphere_v
    movss dword ptr [rdi + 28], xmm0
    add rdi, 32
    inc ebx
    cmp ebx, 6
    jb corners
    inc r13d
    cmp r13d, r14d
    jb slices
    inc r12d
    cmp r12d, r14d
    jb stacks
    mov rax, create_sphere_vertex_count
    shl rax, 5
    invoke(create_buffer, D3D11_BIND_FLAG.D3D11_BIND_VERTEX_BUFFER, create_sphere_vertices, rax)
    mov rdx, create_sphere_vertex_count
    add rsp, 40
    pop r14
    pop r13
    pop r12
    pop rdi
    pop rsi
    pop rbx
    ret
}

main: proc() -> i32 = {
    push rbx
    push rsi
    push rdi
    sub rsp, 48                            ; draw_vertices takes 5: 32 + 8, and 8 more: entry 8 + three pushes 24 + 48 lands on 16

    invoke(window_create, "LearnD3D11")
    call window_create_depth

    invoke(create_sampler, D3D11_FILTER.D3D11_FILTER_ANISOTROPIC, D3D11_TEXTURE_ADDRESS_MODE.D3D11_TEXTURE_ADDRESS_WRAP)
    mov sampler, rax
    vcall(context, ID3D11DeviceContextVtbl.PSSetSamplers, 0, 1, sampler&)

    vcall(device, ID3D11DeviceVtbl.CreateDepthStencilState, sky_depth_desc&, sky_depth&)

    invoke(shader_create, s&, "src/4.advanced_d3d11/6.1.cubemaps/cubemaps.hlsl", cube_layout&, CUBE_LAYOUT_COUNT)
    invoke(shader_create, skybox_shader&, "src/4.advanced_d3d11/6.1.cubemaps/skybox.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_sphere, 64)
    mov sphere_vb, rax
    mov sphere_count, rdx
    invoke(create_constant_buffer, sizeof(transforms))
    mov transforms_buffer, rax
    invoke(create_constant_buffer, sizeof(env_constants))
    mov env_buffer, rax

    ; the six faces, as full paths
    lea rsi, face_names
    lea rdi, faces
    xor ebx, ebx
  paths:
    invoke(filesystem_path, [rsi + rbx * 8])
    mov [rdi + rbx * 8], rax
    inc ebx
    cmp ebx, 6
    jb paths
    invoke(load_cubemap, faces&)
    mov skybox, rax

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

  render_loop:
    call window_poll
    test eax, eax
    jz render_loop_done

    call get_time
    movss current_frame, xmm0
    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 env.refract_mode, eax

    invoke(window_begin, clear_color&)
    vcall(context, ID3D11DeviceContextVtbl.IASetPrimitiveTopology, D3D11_PRIMITIVE_TOPOLOGY.D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST)
    invoke(bind_constants, 0, transforms_buffer)
    invoke(transforms_from_camera, t&, cam&)

    ; a turning cube and a sphere, both mirrors (or glass) of the sky
    invoke(update_buffer, env_buffer, env&, sizeof(env_constants))
    invoke(shader_use, s&)
    invoke(bind_constants, 1, env_buffer)
    invoke(bind_texture, 0, skybox)
    movss xmm1, current_frame              ; the angle, the second argument
    mulss xmm1, math_half
    lea rcx, rotation
    lea r8, y_axis
    call m4_rotate
    invoke(m4_translate, model&, cube_position&)
    invoke(m4_mul, model&, model&, rotation&)
    invoke(draw_vertices, transforms_buffer, t&, cube_vb, 36, model&)
    invoke(m4_translate, model&, sphere_position&)
    invoke(draw_vertices, transforms_buffer, t&, sphere_vb, sphere_count, model&)

    ; draw skybox as last, with the translation removed from the view matrix
    invoke(m4_without_translation, t.view&, t.view&)
    invoke(shader_use, skybox_shader&)
    vcall(context, ID3D11DeviceContextVtbl.OMSetDepthStencilState, sky_depth, 0)
    invoke(m4_identity, model&)
    invoke(draw_vertices, transforms_buffer, t&, cube_vb, 36, model&)
    vcall(context, ID3D11DeviceContextVtbl.OMSetDepthStencilState, 0, 0)

    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.