aboutsummaryrefslogtreecommitdiff
path: root/src/util/ibl.c
blob: 5a799909966eee17d184455d99bf111eb32b0d98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <gfx/util/ibl.h>

#include <gfx/core.h>
#include <gfx/util/geometry.h>
#include <gfx/util/shader.h>
#include <math/mat4.h>

#include <assert.h>
#include <stdlib.h>

typedef struct IBL {
  Geometry*      quad;
  ShaderProgram* brdf_integration_map_shader;
  ShaderProgram* irradiance_map_shader;
  ShaderProgram* prefiltered_environment_map_shader;
  Texture*       brdf_integration_map;
  FrameBuffer*   framebuffer;
  mat4           rotations[6];
} IBL;

static const CubemapFace faces[6] = {
    CubemapFacePosX, // Right.
    CubemapFaceNegX, // Left.
    CubemapFacePosY, // Up.
    CubemapFaceNegY, // Down.
    CubemapFacePosZ, // Back.
    CubemapFaceNegZ, // Front.
};

static const float flips[6] = {
    -1.0f, // Right.
    -1.0f, // Left.
    +1.0f, // Up.
    +1.0f, // Down.
    -1.0f, // Back.
    -1.0f, // Front.
};

IBL* gfx_make_ibl(GfxCore* gfxcore) {
  assert(gfxcore);

  IBL* ibl = calloc(1, sizeof(IBL));
  if (!ibl) {
    return 0;
  }

  if (!(ibl->quad = gfx_make_quad_11(gfxcore))) {
    goto cleanup;
  }

  // We only need the BRDF integration once since we are caching the map, but
  // compiling the shader up front may lead to fewer surprises. Not that the
  // shader is fully compiled up front anyway, since the driver will typically
  // defer full compilation to the first draw call.
  if (!(ibl->brdf_integration_map_shader =
            gfx_make_brdf_integration_map_shader(gfxcore))) {
    goto cleanup;
  }

  if (!(ibl->irradiance_map_shader = gfx_make_irradiance_map_shader(gfxcore))) {
    goto cleanup;
  }

  if (!(ibl->prefiltered_environment_map_shader =
            gfx_make_prefiltered_environment_map_shader(gfxcore))) {
    goto cleanup;
  }

  // Create an empty framebuffer for now. Will attach the colour buffer later
  // as we render the faces of the cube.
  if (!(ibl->framebuffer = gfx_make_framebuffer(
            gfxcore,
            &(FrameBufferDesc){
                .colour =
                    (FrameBufferAttachment){.type = FrameBufferNoAttachment},
                .depth = (FrameBufferAttachment){
                    .type = FrameBufferNoAttachment}}))) {
    goto cleanup;
  }

  // TODO: Debug the camera rotations. Irradiance debug output should appear
  // just like the input cubemap.

  // Right.
  ibl->rotations[0] = mat4_lookat(
      /*position=*/vec3_make(0, 0, 0),
      /*target=*/vec3_make(1, 0, 0),
      /*up=*/vec3_make(0, 1, 0));
  // Left.
  ibl->rotations[1] = mat4_lookat(
      /*position=*/vec3_make(0, 0, 0),
      /*target=*/vec3_make(-1, 0, 0),
      /*up=*/vec3_make(0, 1, 0));
  // Up.
  ibl->rotations[2] = mat4_lookat(
      /*position=*/vec3_make(0, 0, 0),
      /*target=*/vec3_make(0, 1, 0),
      /*up=*/vec3_make(0, 0, 1));
  // Down.
  ibl->rotations[3] = mat4_lookat(
      /*position=*/vec3_make(0, 0, 0),
      /*target=*/vec3_make(0, -1, 0),
      /*up=*/vec3_make(0, 0, -1));
  // Back.
  ibl->rotations[4] = mat4_lookat(
      /*position=*/vec3_make(0, 0, 0),
      /*target=*/vec3_make(0, 0, 1),
      /*up=*/vec3_make(0, 1, 0));
  // Front.
  ibl->rotations[5] = mat4_lookat(
      /*position=*/vec3_make(0, 0, 0),
      /*target=*/vec3_make(0, 0, -1),
      /*up=*/vec3_make(0, 1, 0));

  return ibl;

cleanup:
  gfx_destroy_ibl(gfxcore, &ibl);
  return 0;
}

void gfx_destroy_ibl(GfxCore* gfxcore, IBL** ibl) {
  if (!ibl) {
    return;
  }
  if ((*ibl)->quad) {
    gfx_destroy_geometry(gfxcore, &(*ibl)->quad);
  }
  if ((*ibl)->brdf_integration_map_shader) {
    gfx_destroy_shader_program(gfxcore, &(*ibl)->brdf_integration_map_shader);
  }
  if ((*ibl)->irradiance_map_shader) {
    gfx_destroy_shader_program(gfxcore, &(*ibl)->irradiance_map_shader);
  }
  if ((*ibl)->prefiltered_environment_map_shader) {
    gfx_destroy_shader_program(
        gfxcore, &(*ibl)->prefiltered_environment_map_shader);
  }
  if ((*ibl)->brdf_integration_map) {
    gfx_destroy_texture(gfxcore, &(*ibl)->brdf_integration_map);
  }
  if ((*ibl)->framebuffer) {
    gfx_destroy_framebuffer(gfxcore, &(*ibl)->framebuffer);
  }
  free(*ibl);
  *ibl = 0;
}

Texture* gfx_make_brdf_integration_map(
    IBL* ibl, GfxCore* gfxcore, int width, int height) {
  assert(ibl);
  assert(gfxcore);

  if (ibl->brdf_integration_map) {
    return ibl->brdf_integration_map;
  }

  bool success = false;

  if (!(ibl->brdf_integration_map = gfx_make_texture(
            gfxcore, &(TextureDesc){
                         .width     = width,
                         .height    = height,
                         .depth     = 1,
                         .dimension = Texture2D,
                         .format    = TextureRG16F,
                         .filtering = LinearFiltering,
                         .wrap      = ClampToEdge,
                         .mipmaps   = false}))) {
    goto cleanup;
  }

  gfx_activate_framebuffer(ibl->framebuffer);
  gfx_framebuffer_set_viewport(ibl->framebuffer, 0, 0, width, height);
  gfx_activate_shader_program(ibl->brdf_integration_map_shader);
  if (!gfx_framebuffer_attach_colour(
          ibl->framebuffer, &(FrameBufferAttachment){
                                .type              = FrameBufferTexture,
                                .texture.texture   = ibl->brdf_integration_map,
                                .texture.mip_level = 0})) {
    goto cleanup;
  }
  gfx_render_geometry(ibl->quad);

  success = true;

cleanup:
  gfx_deactivate_shader_program(ibl->brdf_integration_map_shader);
  gfx_deactivate_framebuffer(ibl->framebuffer);
  if (!success && ibl->brdf_integration_map) {
    gfx_destroy_texture(gfxcore, &ibl->brdf_integration_map);
    return 0;
  } else {
    return ibl->brdf_integration_map;
  }
}

Texture* gfx_make_irradiance_map(
    IBL* ibl, GfxCore* gfxcore, const Texture* environment_map, int width,
    int height) {
  assert(ibl);
  assert(gfxcore);
  assert(environment_map);

  bool success = false;

  Texture* irradiance_map = 0;

  // TODO: Could define colour-renderable texture formats separately to make
  // framebuffer creation less error-prone. Or, at the very least, validate the
  // choice at runtime.
  //
  // Make sure to use a float colour format to avoid [0,1] clamping when the
  // irradiance values are computed!
  if (!(irradiance_map = gfx_make_texture(
            gfxcore, &(TextureDesc){
                         .width     = width,
                         .height    = height,
                         .depth     = 1,
                         .dimension = TextureCubeMap,
                         .format    = TextureR11G11B10F,
                         .filtering = LinearFiltering,
                         .mipmaps   = false}))) {
    goto cleanup;
  }

  gfx_activate_framebuffer(ibl->framebuffer);
  gfx_framebuffer_set_viewport(ibl->framebuffer, 0, 0, width, height);
  gfx_activate_shader_program(ibl->irradiance_map_shader);
  gfx_set_texture_uniform(ibl->irradiance_map_shader, "Sky", environment_map);
  for (int i = 0; i < 6; ++i) {
    if (!gfx_framebuffer_attach_colour(
            ibl->framebuffer, &(FrameBufferAttachment){
                                  .type            = FrameBufferCubemapTexture,
                                  .cubemap.face    = faces[i],
                                  .cubemap.texture = irradiance_map})) {
      goto cleanup;
    }
    gfx_set_float_uniform(ibl->irradiance_map_shader, "Flip", flips[i]);
    gfx_set_mat4_uniform(
        ibl->irradiance_map_shader, "CameraRotation", &ibl->rotations[i]);
    gfx_apply_uniforms(ibl->irradiance_map_shader);
    gfx_render_geometry(ibl->quad);
  }

  success = true;

cleanup:
  gfx_deactivate_shader_program(ibl->irradiance_map_shader);
  gfx_deactivate_framebuffer(ibl->framebuffer);
  if (!success && irradiance_map) {
    gfx_destroy_texture(gfxcore, &irradiance_map);
    return 0;
  } else {
    return irradiance_map;
  }
}

Texture* gfx_make_prefiltered_environment_map(
    IBL* ibl, GfxCore* gfxcore, const Texture* environment_map, int width,
    int height, int* max_mip_level) {
  assert(ibl);
  assert(gfxcore);
  assert(environment_map);
  assert(max_mip_level);

  bool success = false;

  Texture* prefiltered_env_map = 0;

  if (!(prefiltered_env_map = gfx_make_texture(
            gfxcore, &(TextureDesc){
                         .width     = width,
                         .height    = height,
                         .depth     = 1,
                         .dimension = TextureCubeMap,
                         .format    = TextureR11G11B10F,
                         .filtering = LinearFiltering,
                         .mipmaps   = true}))) {
    goto cleanup;
  }

  gfx_activate_framebuffer(ibl->framebuffer);
  gfx_activate_shader_program(ibl->prefiltered_environment_map_shader);
  gfx_set_texture_uniform(
      ibl->prefiltered_environment_map_shader, "Sky", environment_map);
  const int max_mip = (int)(rlog2(min(width, height)));
  for (int mip = 0; mip <= max_mip; ++mip) {
    const int   mip_width  = width >> mip;
    const int   mip_height = height >> mip;
    const float roughness  = (float)mip / (float)(max_mip);
    gfx_framebuffer_set_viewport(ibl->framebuffer, 0, 0, mip_width, mip_height);
    gfx_set_float_uniform(
        ibl->prefiltered_environment_map_shader, "Roughness", roughness);

    for (int i = 0; i < 6; ++i) {
      if (!gfx_framebuffer_attach_colour(
              ibl->framebuffer, &(FrameBufferAttachment){
                                    .type         = FrameBufferCubemapTexture,
                                    .cubemap.face = faces[i],
                                    .cubemap.mip_level = mip,
                                    .cubemap.texture = prefiltered_env_map})) {
        goto cleanup;
      }
      gfx_set_float_uniform(
          ibl->prefiltered_environment_map_shader, "Flip", flips[i]);
      gfx_set_mat4_uniform(
          ibl->prefiltered_environment_map_shader, "CameraRotation",
          &ibl->rotations[i]);
      gfx_apply_uniforms(ibl->prefiltered_environment_map_shader);
      gfx_render_geometry(ibl->quad);
    }
  }

  *max_mip_level = max_mip;

  success = true;

cleanup:
  gfx_deactivate_shader_program(ibl->prefiltered_environment_map_shader);
  gfx_deactivate_framebuffer(ibl->framebuffer);
  if (!success && prefiltered_env_map) {
    gfx_destroy_texture(gfxcore, &prefiltered_env_map);
    return 0;
  } else {
    return prefiltered_env_map;
  }
}