ibl_specular.rasm

src/6.pbr/2.2.ibl_specular/ibl_specular.rasm · 374 lines

; 2.2 IBL: specular
;
; The other half of image based light: reflections. The specular integral
; depends on the view direction as well as the surface, which is too much to
; tabulate, so it is split into two parts that can be ("split sum"):
;
;   prefiltered environment   the environment blurred by the GGX lobe, one mip
;                             level per roughness, looked up along the
;                             reflection vector. Built by importance sampling:
;                             1024 directions per texel, put where the lobe is
;   BRDF table                a 2D texture over (n.v, roughness) giving a scale
;                             and a bias for the surface's F0
;
;   specular = prefiltered(R, roughness) * (F0 * scale + bias)
;
; With the diffuse irradiance of 2.1 that is the full lighting model: smooth
; metals now mirror the room, rough ones blur it, and plastics pick up a faint
; reflection at grazing angles. Tab shows five textured materials
; (LearnOpenGL's 2.2.2 ibl_specular_textured).
;
;   Tab   uniform grid / five textured materials
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"
import "../../../includes/learn/pbr.rasm"

; Mirror `cbuffer material` and `cbuffer lights` in pbr.hlsl, and `cbuffer
; prefilter` in prefilter.hlsl.
material_constants: struct = {
    albedo: vec3
    metallic: f32
    roughness: f32
    ao: f32
    textured: u32
    pad: f32
}

lights_constants: struct = {
    positions: [4]vec4
    colors: [4]vec4
}

prefilter_constants: struct = {
    roughness: f32
    env_size: f32                          ; of the environment's top mip, per face
    pad: [2]f32
}

camera_grid: const vec3 = { 0.0, 0.0, 22.0 }    ; far enough back for the grid
camera_row: const vec3 = { 0.0, 0.0, 9.0 }      ; closer, for the row of five
clear_color: [4]const f32 = { 0.1, 0.1, 0.1, 1.0 }

; the five materials of the textured view
material_folders: [5]const *const c8 = {
    "resources/textures/pbr/rusted_iron/",
    "resources/textures/pbr/gold/",
    "resources/textures/pbr/grass/",
    "resources/textures/pbr/plastic/",
    "resources/textures/pbr/wall/",
}

t: transforms = {}
; The lights stand still here, so the whole buffer is the initialiser, sent
; once (masm64-dx11 copied it together at startup).
lights: lights_constants = {
    .positions = {
        { -10.0,  10.0, 10.0, 0.0 },
        {  10.0,  10.0, 10.0, 0.0 },
        { -10.0, -10.0, 10.0, 0.0 },
        {  10.0, -10.0, 10.0, 0.0 },
    },
    .colors = {
        { 300.0, 300.0, 300.0, 0.0 },
        { 300.0, 300.0, 300.0, 0.0 },
        { 300.0, 300.0, 300.0, 0.0 },
        { 300.0, 300.0, 300.0, 0.0 },
    },
}
material: material_constants = { .albedo = { 0.5, 0.0, 0.0 }, .ao = 1.0 }
settings: prefilter_constants = { .env_size = 512.0 }
model_matrix: mat4 = {}
sphere_position: vec3 = {}                 ; z stays 0
pbr_shader: shader = {}
equirect_shader: shader = {}
irradiance_shader: shader = {}
prefilter_shader: shader = {}
brdf_shader: shader = {}
background_shader: shader = {}
cam: camera = {}
ball: sphere = {}
env_cubemap: cube_target = {}
irradiance_map: cube_target = {}
prefilter_map: cube_target = {}
cube_vb: *ID3D11Buffer = {}
transforms_buffer: *ID3D11Buffer = {}
material_buffer: *ID3D11Buffer = {}
lights_buffer: *ID3D11Buffer = {}
prefilter_buffer: *ID3D11Buffer = {}
wrap_sampler: *ID3D11SamplerState = {}
clamp_sampler: *ID3D11SamplerState = {}
hdr_texture: *ID3D11ShaderResourceView = {}
ibl_views: [3]*ID3D11ShaderResourceView = {}    ; irradiance, prefiltered, BRDF table: t5, t6, t7
materials: [25]*ID3D11ShaderResourceView = {}   ; five maps for each of the five materials
framed_mode: u32 = 0xFFFFFFFF              ; the mode the camera was last framed for: none yet

; The sky is drawn last, at the far plane: it passes where nothing else was
; drawn, and leaves the depth buffer alone.
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,
}
sky_depth: *ID3D11DepthStencilState = {}

; the BRDF table: two half floats per texel, scale and bias
lut_desc: D3D11_TEXTURE2D_DESC = {
    .Width = 512,
    .Height = 512,
    .MipLevels = 1,
    .ArraySize = 1,
    .Format = DXGI_FORMAT.DXGI_FORMAT_R16G16_FLOAT,
    .SampleDesc = { .Count = 1 },
    .BindFlags = D3D11_BIND_FLAG.D3D11_BIND_RENDER_TARGET | D3D11_BIND_FLAG.D3D11_BIND_SHADER_RESOURCE,
}
lut_texture: *ID3D11Texture2D = {}
lut_rtv: *ID3D11RenderTargetView = {}

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

main: proc() -> i32 = {
    push rbx
    push rsi
    sub rsp, 40                            ; draw_vertices takes 5: 32 + 8; entry 8 + two pushes 16 + 40 lands on 16

    invoke(window_create, "LearnD3D11  (Tab: uniform grid / textured)")
    call window_create_depth
    call window_use_srgb

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

    ; build and compile shaders
    ; -------------------------
    invoke(shader_create, pbr_shader&, "src/6.pbr/2.2.ibl_specular/pbr.hlsl", cube_layout&, CUBE_LAYOUT_COUNT)
    invoke(shader_create, equirect_shader&, "src/6.pbr/2.2.ibl_specular/equirectangular_to_cubemap.hlsl", cube_layout&, CUBE_LAYOUT_COUNT)
    invoke(shader_create, irradiance_shader&, "src/6.pbr/2.2.ibl_specular/irradiance_convolution.hlsl", cube_layout&, CUBE_LAYOUT_COUNT)
    invoke(shader_create, prefilter_shader&, "src/6.pbr/2.2.ibl_specular/prefilter.hlsl", cube_layout&, CUBE_LAYOUT_COUNT)
    ; the BRDF table's shader makes its own triangle: no input layout
    invoke(shader_create, brdf_shader&, "src/6.pbr/2.2.ibl_specular/brdf.hlsl", 0, 0)
    invoke(shader_create, background_shader&, "src/6.pbr/2.2.ibl_specular/background.hlsl", cube_layout&, CUBE_LAYOUT_COUNT)

    ; pbr: the samplers the precomputation passes and the spheres share
    invoke(create_sampler, D3D11_FILTER.D3D11_FILTER_MIN_MAG_MIP_LINEAR, D3D11_TEXTURE_ADDRESS_MODE.D3D11_TEXTURE_ADDRESS_CLAMP)
    mov clamp_sampler, rax
    vcall(context, ID3D11DeviceContextVtbl.PSSetSamplers, 1, 1, clamp_sampler&)
    vcall(device, ID3D11DeviceVtbl.CreateDepthStencilState, sky_depth_desc&, sky_depth&)

    ; set up vertex data
    ; ------------------
    invoke(create_sphere, ball&, 64)
    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(material_constants))
    mov material_buffer, rax
    invoke(create_constant_buffer, sizeof(lights_constants))
    mov lights_buffer, rax
    vcall(context, ID3D11DeviceContextVtbl.IASetPrimitiveTopology, D3D11_PRIMITIVE_TOPOLOGY.D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST)
    invoke(bind_constants, 0, transforms_buffer)

    ; pbr: load the HDR environment map
    ; ---------------------------------
    invoke(filesystem_path, "resources/textures/hdr/newport_loft.hdr")
    invoke(load_hdr, rax)
    mov hdr_texture, rax

    ; pbr: convert the equirectangular map to a cubemap, with mipmaps (the
    ; prefilter reads them to avoid sparkle from small, very bright spots)
    ; ----------------------------------------------------------------------
    invoke(create_cube_target, env_cubemap&, 512, 0, 1)
    invoke(shader_use, equirect_shader&)
    invoke(bind_texture, 0, hdr_texture)
    invoke(render_to_cube, env_cubemap&, 0, transforms_buffer, cube_vb)
    call unbind_textures
    vcall(context, ID3D11DeviceContextVtbl.GenerateMips, env_cubemap.srv)

    ; pbr: an irradiance cubemap, by convolving the environment
    ; ---------------------------------------------------------
    invoke(create_cube_target, irradiance_map&, 32, 1, 0)
    invoke(shader_use, irradiance_shader&)
    invoke(bind_texture, 0, env_cubemap.srv)
    invoke(render_to_cube, irradiance_map&, 0, transforms_buffer, cube_vb)
    call unbind_textures

    ; pbr: a prefiltered environment cubemap, roughness rising one step per mip
    ; -------------------------------------------------------------------------
    invoke(create_cube_target, prefilter_map&, 128, 5, 0)
    invoke(create_constant_buffer, sizeof(prefilter_constants))
    mov prefilter_buffer, rax
    invoke(shader_use, prefilter_shader&)
    invoke(bind_constants, 1, prefilter_buffer)
    invoke(bind_texture, 0, env_cubemap.srv)
    xor ebx, ebx                           ; mip
  prefilter_mips:
    ; roughness = mip / 4
    cvtsi2ss xmm0, ebx
    mulss xmm0, 0.25
    movss settings.roughness, xmm0
    invoke(update_buffer, prefilter_buffer, settings&, sizeof(prefilter_constants))
    invoke(render_to_cube, prefilter_map&, rbx, transforms_buffer, cube_vb)
    inc ebx
    cmp ebx, 5
    jb prefilter_mips
    call unbind_textures

    ; pbr: the 2D lookup table of the BRDF, drawn with one full-screen triangle
    ; -------------------------------------------------------------------------
    vcall(device, ID3D11DeviceVtbl.CreateTexture2D, lut_desc&, 0, lut_texture&)
    assert_hr()
    vcall(device, ID3D11DeviceVtbl.CreateRenderTargetView, lut_texture, 0, lut_rtv&)
    lea rax, ibl_views[2]                  ; the third view is the table's
    vcall(device, ID3D11DeviceVtbl.CreateShaderResourceView, lut_texture, 0, rax)
    vcall(context, ID3D11DeviceContextVtbl.OMSetRenderTargets, 1, lut_rtv&, 0)
    invoke(set_viewport, 512, 512)
    invoke(shader_use, brdf_shader&)
    vcall(context, ID3D11DeviceContextVtbl.Draw, 3, 0)
    mov rax, irradiance_map.srv
    mov ibl_views[0], rax
    mov rax, prefilter_map.srv
    mov ibl_views[1], rax

    ; the five materials of the textured view
    ; ---------------------------------------
    xor ebx, ebx
    lea rsi, materials
  load_materials:
    lea rax, material_folders
    mov rcx, [rax + rbx * 8]
    invoke(load_material, rcx, rsi)
    add rsi, 5 * 8                         ; five views on
    inc ebx
    cmp ebx, 5
    jb load_materials

    ; lights
    ; ------
    invoke(update_buffer, lights_buffer, lights&, sizeof(lights_constants))

    ; render loop
    ; -----------
  render_loop:
    call window_poll
    test eax, eax
    jz render_loop_done

    ; per-frame time logic
    call get_time
    movss xmm1, xmm0
    subss xmm0, last_frame
    movss delta_time, xmm0
    movss last_frame, xmm1

    ; input: switching views frames the camera anew, back for the grid, close
    ; for the row
    invoke(window_mode, 2)
    mov material.textured, eax
    cmp eax, framed_mode
    je framed
    mov framed_mode, eax
    lea rcx, cam
    lea rdx, camera_grid
    test eax, eax
    jz frame_grid
    lea rdx, camera_row
  frame_grid:
    movss xmm2, -90.0                      ; yaw
    movss xmm3, 0.0                        ; pitch
    call camera_create
  framed:
    invoke(camera_process_input, cam&, delta_time&)

    ; render
    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&)
    invoke(shader_use, pbr_shader&)
    invoke(bind_constants, 1, material_buffer)
    invoke(bind_constants, 2, lights_buffer)
    vcall(context, ID3D11DeviceContextVtbl.PSSetShaderResources, 5, 3, ibl_views&)

    cmp material.textured, 0
    je grid
    ; one sphere per material, in a row: ((i - 2) * 2.5, 0, 0)
    xor ebx, ebx
    lea rsi, materials
  row_of_five:
    vcall(context, ID3D11DeviceContextVtbl.PSSetShaderResources, 0, 5, rsi)
    invoke(update_buffer, material_buffer, material&, sizeof(material_constants))
    lea eax, [ebx - 2]
    cvtsi2ss xmm0, eax
    mulss xmm0, 2.5
    movss sphere_position.x, xmm0
    mov sphere_position.y, 0
    invoke(m4_translate, model_matrix&, sphere_position&)
    invoke(draw_sphere, transforms_buffer, t&, ball&, model_matrix&)
    add rsi, 5 * 8
    inc ebx
    cmp ebx, 5
    jb row_of_five
    jmp drawn
  grid:
    ; render rows*column number of spheres with varying metallic/roughness
    ; values: metallic from bottom to top, roughness from left to right
    xor ebx, ebx                           ; row
  grid_rows:
    xor esi, esi                           ; column
  grid_columns:
    cvtsi2ss xmm0, ebx
    divss xmm0, 6.0
    movss material.metallic, xmm0
    ; perfectly smooth reflects one point of light, which looks wrong for a
    ; point light: start just above zero
    cvtsi2ss xmm0, esi
    divss xmm0, 6.0
    movss xmm1, 0.05                       ; clampf(x, lo, hi): xmm0, xmm1, xmm2
    movss xmm2, math_one
    call clampf
    movss material.roughness, xmm0
    invoke(update_buffer, material_buffer, material&, sizeof(material_constants))
    ; at ((column - 3) * 2.5, (row - 3) * 2.5, 0)
    movss xmm1, 2.5
    lea eax, [esi - 3]
    cvtsi2ss xmm0, eax
    mulss xmm0, xmm1
    movss sphere_position.x, xmm0
    lea eax, [ebx - 3]
    cvtsi2ss xmm0, eax
    mulss xmm0, xmm1
    movss sphere_position.y, xmm0
    invoke(m4_translate, model_matrix&, sphere_position&)
    invoke(draw_sphere, transforms_buffer, t&, ball&, model_matrix&)
    inc esi
    cmp esi, 7
    jb grid_columns
    inc ebx
    cmp ebx, 7
    jb grid_rows
  drawn:

    ; render skybox (render as last to prevent overdraw)
    invoke(shader_use, background_shader&)
    invoke(bind_texture, 0, env_cubemap.srv)
    vcall(context, ID3D11DeviceContextVtbl.OMSetDepthStencilState, sky_depth, 0)
    invoke(m4_identity, model_matrix&)
    invoke(draw_vertices, transforms_buffer, t&, cube_vb, 36, model_matrix&)
    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.