multiple_lights.rasm

src/2.lighting/6.multiple_lights/multiple_lights.rasm · 188 lines

; 6. Multiple lights
;
; Light adds up. Each source is worked out on its own and the results are
; summed: one directional light, four coloured point lights and the camera's
; flashlight, all from the functions of 5.1.
;
; The point lights travel to the shader as an array inside a constant
; buffer. HLSL starts every array element on a 16-byte boundary, so
; `point_light` pads each vec3 to sixteen bytes to match.
;
;   W A S D, Space, Shift   move
;   right mouse + drag      look
;   wheel                   zoom
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 POINT_LIGHT_COUNT 4

point_light: struct = {
    position: vec3
    pad0: f32
    color: vec3
    pad1: f32
}

; Mirrors `cbuffer lights` in multiple_lights.hlsl.
lights_constants: struct = {
    sun_direction: vec3
    shininess: f32
    sun_color: vec3
    pad0: f32
    spot_position: vec3
    spot_inner_cos: f32
    spot_direction: vec3
    spot_outer_cos: f32
    points: [POINT_LIGHT_COUNT]point_light
}

camera_start: const vec3 = { 0.0, 0.5, 4.0 }
clear_color: [4]const f32 = { 0.05, 0.05, 0.05, 1.0 }

t: transforms = {}
; The sun, and the four point lights with where they are and their colour.
; (masm64-dx11 copied them in at startup: MASM cannot initialise an array of
; structures inside a structure. C can.) The flashlight is set at run time.
lights: lights_constants = {
    .sun_direction = { -0.2, -1.0, -0.3 },
    .shininess = 32.0,
    .sun_color = { 0.4, 0.4, 0.45 },
    .points = {
        { .position = {  0.7,  0.2,   2.0 }, .color = { 1.0, 0.6, 0.0 } },
        { .position = {  2.3, -3.3,  -4.0 }, .color = { 1.0, 0.0, 0.0 } },
        { .position = { -4.0,  2.0, -12.0 }, .color = { 1.0, 1.0, 0.0 } },
        { .position = {  0.0,  0.0,  -3.0 }, .color = { 0.2, 0.2, 1.0 } },
    },
}
lamp_color: vec4 = { 0.0, 0.0, 0.0, 1.0 }
model: mat4 = {}
lighting_shader: shader = {}
lamp_shader: shader = {}
cam: camera = {}
cube_vb: *ID3D11Buffer = {}
transforms_buffer: *ID3D11Buffer = {}
light_buffer: *ID3D11Buffer = {}
lamp_buffer: *ID3D11Buffer = {}
sampler: *ID3D11SamplerState = {}
maps: [2]*ID3D11ShaderResourceView = {}
stride: u32 = CUBE_STRIDE
offset: u32 = 0

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

main: proc() -> i32 = {
    push rbx
    push rsi
    sub rsp, 56                            ; IASetVertexBuffers: 5 past `this`, 2 on the stack: 32 + 16; entry 8 + two pushes 16 + 56 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&)

    invoke(shader_create, lighting_shader&, "src/2.lighting/6.multiple_lights/multiple_lights.hlsl", cube_layout&, CUBE_LAYOUT_COUNT)
    invoke(shader_create, lamp_shader&, "src/2.lighting/6.multiple_lights/light_cube.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
    invoke(create_constant_buffer, sizeof(lights_constants))
    mov light_buffer, rax
    invoke(create_constant_buffer, sizeof(vec4))
    mov lamp_buffer, rax
    invoke(filesystem_path, "resources/textures/container2.png")
    invoke(load_texture, rax, 0)
    mov maps[0], rax
    invoke(filesystem_path, "resources/textures/container2_specular.png")
    invoke(load_texture, rax, 0)
    mov maps[1], rax

    ; the flashlight's cone: 12.5 degrees fully lit, fading out by 15
    movss xmm0, 12.5
    call radians
    call cosf
    movss lights.spot_inner_cos, xmm0
    movss xmm0, 15.0
    call radians
    call cosf
    movss lights.spot_outer_cos, xmm0

    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 xmm1, xmm0
    subss xmm0, last_frame
    movss delta_time, xmm0
    movss last_frame, xmm1

    invoke(camera_process_input, cam&, delta_time&)

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

    ; the flashlight goes where the camera does
    v3load_var(xmm0, cam.position)
    v3store_var(lights.spot_position, xmm0)
    v3load_var(xmm0, cam.front)
    v3store_var(lights.spot_direction, xmm0)
    invoke(update_buffer, light_buffer, lights&, sizeof(lights_constants))
    invoke(shader_use, lighting_shader&)
    invoke(bind_constants, 1, light_buffer)
    vcall(context, ID3D11DeviceContextVtbl.PSSetShaderResources, 0, 2, maps&)
    xor ebx, ebx
  cubes:
    invoke(cube_model, model&, rbx)
    invoke(draw_cube, transforms_buffer, t&, model&)
    inc ebx
    cmp ebx, 10
    jb cubes

    ; a lamp at each point light, in its colour
    invoke(shader_use, lamp_shader&)
    invoke(bind_constants, 1, lamp_buffer)
    lea rsi, lights.points
    xor ebx, ebx
  lamps:
    lea rax, [rsi].point_light.color
    v3load(xmm0, rax)
    v3store_var(lamp_color, xmm0)
    invoke(update_buffer, lamp_buffer, lamp_color&, sizeof(vec4))
    lea rcx, model
    lea rdx, [rsi].point_light.position
    movss xmm2, 0.2                        ; the scale, the third argument
    call m4_translate_scale
    invoke(draw_cube, transforms_buffer, t&, model&)
    add rsi, sizeof(point_light)
    inc ebx
    cmp ebx, POINT_LIGHT_COUNT
    jb lamps

    call window_present
    jmp render_loop
  render_loop_done:

    invoke(ExitProcess, 0)                 ; does not return: no epilogue
}
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.