summaryrefslogtreecommitdiff
path: root/gfx/src/render/framebuffer.c
blob: 323ef566df76d14e4417bb2cd5db16704ec97d54 (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
#include "framebuffer.h"

#include "renderbuffer.h"
#include "texture.h"

#include <gfx/error.h>

#include <assert.h>

static void framebuffer_attach_colour(FrameBuffer* framebuffer,
                                      const FrameBufferAttachment* attachment) {
  assert(framebuffer);
  assert(attachment);

  switch (attachment->type) {
  case FrameBufferNoAttachment:
    break;
  case FrameBufferTexture:
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
                           attachment->texture.texture->id,
                           attachment->texture.mip_level);
    break;
  case FrameBufferCubemapTexture:
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                           to_GL_cubemap_face(attachment->cubemap.face),
                           attachment->cubemap.texture->id,
                           attachment->cubemap.mip_level);
    break;
  case FrameBufferRenderBuffer:
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                              GL_RENDERBUFFER, attachment->renderbuffer->id);
    break;
  }

  ASSERT_GL;
}

static void framebuffer_attach_depth(FrameBuffer* framebuffer,
                                     const FrameBufferAttachment* attachment) {
  assert(framebuffer);
  assert(attachment);

  switch (attachment->type) {
  case FrameBufferNoAttachment:
    break;
  case FrameBufferTexture:
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
                           GL_DEPTH_COMPONENT, attachment->texture.texture->id,
                           attachment->texture.mip_level);
    break;
  // TODO: Could distinguish between colour and depth attachment types to make
  // this a compile-time error.
  case FrameBufferCubemapTexture:
    gfx_set_error(
        "Cannot use a cubemap texture as a depth framebuffer attachment");
    break;
  case FrameBufferRenderBuffer:
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
                              GL_RENDERBUFFER, attachment->renderbuffer->id);
    break;
  }

  ASSERT_GL;
}

bool gfx_init_framebuffer(FrameBuffer* framebuffer,
                          const FrameBufferDesc* desc) {
  assert(framebuffer);
  assert(desc);

  glGenFramebuffers(1, &framebuffer->id);
  if (!framebuffer->id) {
    gfx_set_error("glGenFramebuffers() failed");
    return false;
  }

  // Allow incomplete framebuffers for flexibility.
  // Attach buffers and check the framebuffer status only if buffers are given
  // up front.
  if (desc->colour.type != FrameBufferNoAttachment ||
      desc->depth.type != FrameBufferNoAttachment) {
    // TODO: Could use the "named" API to avoid having to bind the framebuffer.
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer->id);
    framebuffer_attach_colour(framebuffer, &desc->colour);
    framebuffer_attach_depth(framebuffer, &desc->depth);
    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
      gfx_set_error("glCheckFramebufferStatus() failed");
      gfx_del_framebuffer(framebuffer);
      return false;
    }
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
  }

  ASSERT_GL;
  return true;
}

bool gfx_framebuffer_attach_colour(FrameBuffer* framebuffer,
                                   const FrameBufferAttachment* attachment) {
  assert(framebuffer);
  assert(attachment);

  // TODO: Could use the "named" API to avoid having to bind the framebuffer.
  glBindFramebuffer(GL_FRAMEBUFFER, framebuffer->id);
  framebuffer_attach_colour(framebuffer, attachment);
  if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
    gfx_set_error("glCheckFramebufferStatus() failed");
    return false;
  }
  return true;
}

bool gfx_framebuffer_attach_depth(FrameBuffer* framebuffer,
                                  const FrameBufferAttachment* attachment) {
  assert(framebuffer);
  assert(attachment);

  // TODO: Could use the "named" API to avoid having to bind the framebuffer.
  glBindFramebuffer(GL_FRAMEBUFFER, framebuffer->id);
  framebuffer_attach_depth(framebuffer, attachment);
  if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
    gfx_set_error("glCheckFramebufferStatus() failed");
    return false;
  }
  return true;
}

void gfx_del_framebuffer(FrameBuffer* framebuffer) {
  assert(framebuffer);
  if (framebuffer->id) {
    glDeleteFramebuffers(1, &framebuffer->id);
    framebuffer->id = 0;
  }
}

void gfx_activate_framebuffer(const FrameBuffer* framebuffer) {
  assert(framebuffer);
  glBindFramebuffer(GL_FRAMEBUFFER, framebuffer->id);
}

void gfx_deactivate_framebuffer(const FrameBuffer* framebuffer) {
  assert(framebuffer);
  glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

void gfx_framebuffer_set_viewport(FrameBuffer* framebuffer, int x, int y,
                                  int width, int height) {
  assert(framebuffer);
  glViewport(x, y, width, height);
}