summaryrefslogtreecommitdiff
path: root/gfx/src/render/framebuffer.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2023-06-17 20:23:34 -0700
committer3gg <3gg@shellblade.net>2023-06-17 20:23:34 -0700
commit9d1eb251b5d26759e17dd7bee316cf27fdcb28bf (patch)
tree639b6f95cf4e9a00a388ae6c1d26dc4cebee6583 /gfx/src/render/framebuffer.c
parent520e4e67cd9ff53f3c3512c80d07193625e07e3e (diff)
Move and import error library from clib.
Diffstat (limited to 'gfx/src/render/framebuffer.c')
-rw-r--r--gfx/src/render/framebuffer.c67
1 files changed, 34 insertions, 33 deletions
diff --git a/gfx/src/render/framebuffer.c b/gfx/src/render/framebuffer.c
index 323ef56..84ade4a 100644
--- a/gfx/src/render/framebuffer.c
+++ b/gfx/src/render/framebuffer.c
@@ -3,12 +3,12 @@
3#include "renderbuffer.h" 3#include "renderbuffer.h"
4#include "texture.h" 4#include "texture.h"
5 5
6#include <gfx/error.h> 6#include <error.h>
7 7
8#include <assert.h> 8#include <assert.h>
9 9
10static void framebuffer_attach_colour(FrameBuffer* framebuffer, 10static void framebuffer_attach_colour(
11 const FrameBufferAttachment* attachment) { 11 FrameBuffer* framebuffer, const FrameBufferAttachment* attachment) {
12 assert(framebuffer); 12 assert(framebuffer);
13 assert(attachment); 13 assert(attachment);
14 14
@@ -16,27 +16,28 @@ static void framebuffer_attach_colour(FrameBuffer* framebuffer,
16 case FrameBufferNoAttachment: 16 case FrameBufferNoAttachment:
17 break; 17 break;
18 case FrameBufferTexture: 18 case FrameBufferTexture:
19 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 19 glFramebufferTexture2D(
20 attachment->texture.texture->id, 20 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
21 attachment->texture.mip_level); 21 attachment->texture.texture->id, attachment->texture.mip_level);
22 break; 22 break;
23 case FrameBufferCubemapTexture: 23 case FrameBufferCubemapTexture:
24 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, 24 glFramebufferTexture2D(
25 to_GL_cubemap_face(attachment->cubemap.face), 25 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
26 attachment->cubemap.texture->id, 26 to_GL_cubemap_face(attachment->cubemap.face),
27 attachment->cubemap.mip_level); 27 attachment->cubemap.texture->id, attachment->cubemap.mip_level);
28 break; 28 break;
29 case FrameBufferRenderBuffer: 29 case FrameBufferRenderBuffer:
30 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, 30 glFramebufferRenderbuffer(
31 GL_RENDERBUFFER, attachment->renderbuffer->id); 31 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
32 attachment->renderbuffer->id);
32 break; 33 break;
33 } 34 }
34 35
35 ASSERT_GL; 36 ASSERT_GL;
36} 37}
37 38
38static void framebuffer_attach_depth(FrameBuffer* framebuffer, 39static void framebuffer_attach_depth(
39 const FrameBufferAttachment* attachment) { 40 FrameBuffer* framebuffer, const FrameBufferAttachment* attachment) {
40 assert(framebuffer); 41 assert(framebuffer);
41 assert(attachment); 42 assert(attachment);
42 43
@@ -44,33 +45,33 @@ static void framebuffer_attach_depth(FrameBuffer* framebuffer,
44 case FrameBufferNoAttachment: 45 case FrameBufferNoAttachment:
45 break; 46 break;
46 case FrameBufferTexture: 47 case FrameBufferTexture:
47 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, 48 glFramebufferTexture2D(
48 GL_DEPTH_COMPONENT, attachment->texture.texture->id, 49 GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT,
49 attachment->texture.mip_level); 50 attachment->texture.texture->id, attachment->texture.mip_level);
50 break; 51 break;
51 // TODO: Could distinguish between colour and depth attachment types to make 52 // TODO: Could distinguish between colour and depth attachment types to make
52 // this a compile-time error. 53 // this a compile-time error.
53 case FrameBufferCubemapTexture: 54 case FrameBufferCubemapTexture:
54 gfx_set_error( 55 set_error("Cannot use a cubemap texture as a depth framebuffer attachment");
55 "Cannot use a cubemap texture as a depth framebuffer attachment");
56 break; 56 break;
57 case FrameBufferRenderBuffer: 57 case FrameBufferRenderBuffer:
58 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, 58 glFramebufferRenderbuffer(
59 GL_RENDERBUFFER, attachment->renderbuffer->id); 59 GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER,
60 attachment->renderbuffer->id);
60 break; 61 break;
61 } 62 }
62 63
63 ASSERT_GL; 64 ASSERT_GL;
64} 65}
65 66
66bool gfx_init_framebuffer(FrameBuffer* framebuffer, 67bool gfx_init_framebuffer(
67 const FrameBufferDesc* desc) { 68 FrameBuffer* framebuffer, const FrameBufferDesc* desc) {
68 assert(framebuffer); 69 assert(framebuffer);
69 assert(desc); 70 assert(desc);
70 71
71 glGenFramebuffers(1, &framebuffer->id); 72 glGenFramebuffers(1, &framebuffer->id);
72 if (!framebuffer->id) { 73 if (!framebuffer->id) {
73 gfx_set_error("glGenFramebuffers() failed"); 74 set_error("glGenFramebuffers() failed");
74 return false; 75 return false;
75 } 76 }
76 77
@@ -84,7 +85,7 @@ bool gfx_init_framebuffer(FrameBuffer* framebuffer,
84 framebuffer_attach_colour(framebuffer, &desc->colour); 85 framebuffer_attach_colour(framebuffer, &desc->colour);
85 framebuffer_attach_depth(framebuffer, &desc->depth); 86 framebuffer_attach_depth(framebuffer, &desc->depth);
86 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { 87 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
87 gfx_set_error("glCheckFramebufferStatus() failed"); 88 set_error("glCheckFramebufferStatus() failed");
88 gfx_del_framebuffer(framebuffer); 89 gfx_del_framebuffer(framebuffer);
89 return false; 90 return false;
90 } 91 }
@@ -95,8 +96,8 @@ bool gfx_init_framebuffer(FrameBuffer* framebuffer,
95 return true; 96 return true;
96} 97}
97 98
98bool gfx_framebuffer_attach_colour(FrameBuffer* framebuffer, 99bool gfx_framebuffer_attach_colour(
99 const FrameBufferAttachment* attachment) { 100 FrameBuffer* framebuffer, const FrameBufferAttachment* attachment) {
100 assert(framebuffer); 101 assert(framebuffer);
101 assert(attachment); 102 assert(attachment);
102 103
@@ -104,14 +105,14 @@ bool gfx_framebuffer_attach_colour(FrameBuffer* framebuffer,
104 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer->id); 105 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer->id);
105 framebuffer_attach_colour(framebuffer, attachment); 106 framebuffer_attach_colour(framebuffer, attachment);
106 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { 107 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
107 gfx_set_error("glCheckFramebufferStatus() failed"); 108 set_error("glCheckFramebufferStatus() failed");
108 return false; 109 return false;
109 } 110 }
110 return true; 111 return true;
111} 112}
112 113
113bool gfx_framebuffer_attach_depth(FrameBuffer* framebuffer, 114bool gfx_framebuffer_attach_depth(
114 const FrameBufferAttachment* attachment) { 115 FrameBuffer* framebuffer, const FrameBufferAttachment* attachment) {
115 assert(framebuffer); 116 assert(framebuffer);
116 assert(attachment); 117 assert(attachment);
117 118
@@ -119,7 +120,7 @@ bool gfx_framebuffer_attach_depth(FrameBuffer* framebuffer,
119 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer->id); 120 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer->id);
120 framebuffer_attach_depth(framebuffer, attachment); 121 framebuffer_attach_depth(framebuffer, attachment);
121 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { 122 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
122 gfx_set_error("glCheckFramebufferStatus() failed"); 123 set_error("glCheckFramebufferStatus() failed");
123 return false; 124 return false;
124 } 125 }
125 return true; 126 return true;
@@ -143,8 +144,8 @@ void gfx_deactivate_framebuffer(const FrameBuffer* framebuffer) {
143 glBindFramebuffer(GL_FRAMEBUFFER, 0); 144 glBindFramebuffer(GL_FRAMEBUFFER, 0);
144} 145}
145 146
146void gfx_framebuffer_set_viewport(FrameBuffer* framebuffer, int x, int y, 147void gfx_framebuffer_set_viewport(
147 int width, int height) { 148 FrameBuffer* framebuffer, int x, int y, int width, int height) {
148 assert(framebuffer); 149 assert(framebuffer);
149 glViewport(x, y, width, height); 150 glViewport(x, y, width, height);
150} 151}