dungeon_crawler

view prototype/src/renderer.cc @ 27:cbf86e5198a9

fized the vbo state creep
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 25 Aug 2012 14:37:51 +0300
parents 0588f8a1a351
children 2fc004802739
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <limits.h>
5 #include <assert.h>
6 #include "opengl.h"
7 #include "renderer.h"
8 #include "level.h"
9 #include "sdr.h"
10 #include "datapath.h"
13 static bool create_fbo(int xsz, int ysz);
14 static unsigned int load_sdr(const char *vfname, const char *pfname);
15 static int round_pow2(int x);
18 #define MRT_COUNT 4
19 static unsigned int mrt_tex[MRT_COUNT];
21 static unsigned int mrt_prog;
22 static unsigned int deferred_omni, deferred_debug;
24 static unsigned int fbo, rbuf_depth;
25 static const char *fbstname[] = {
26 "GL_FRAMEBUFFER_COMPLETE",
27 "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT",
28 "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT",
29 "no such fbo error",
30 "GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS",
31 "GL_FRAMEBUFFER_INCOMPLETE_FORMATS",
32 "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER",
33 "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER",
34 "GL_FRAMEBUFFER_UNSUPPORTED"
35 };
37 static int fb_xsz, fb_ysz, tex_xsz, tex_ysz;
41 bool init_renderer(int xsz, int ysz)
42 {
43 if(!GLEW_ARB_draw_buffers) {
44 fprintf(stderr, "OpenGL implementation doesn't support multiple render targets\n");
45 return false;
46 }
48 int max_draw_buf;
49 glGetIntegerv(GL_MAX_DRAW_BUFFERS_ARB, &max_draw_buf);
50 printf("max draw buffers: %d\n", max_draw_buf);
51 if(max_draw_buf < MRT_COUNT) {
52 fprintf(stderr, "OpenGL implementation doesn't support enough draw buffers\n");
53 return false;
54 }
56 if(!create_fbo(xsz, ysz)) {
57 return false;
58 }
59 CHECKGLERR;
61 if(!(mrt_prog = load_sdr("mrt.v.glsl", "mrt.p.glsl"))) {
62 return false;
63 }
64 set_uniform_int(mrt_prog, "tex_dif", 0);
65 set_uniform_int(mrt_prog, "tex_norm", 1);
67 if(!(deferred_debug = load_sdr("deferred.v.glsl", "deferred.p.glsl"))) {
68 return false;
69 }
70 for(int i=0; i<MRT_COUNT; i++) {
71 char uname[32];
72 sprintf(uname, "mrt%d", i);
73 set_uniform_int(deferred_debug, uname, i);
74 }
76 if(!(deferred_omni = load_sdr("deferred.v.glsl", "deferred_omni.p.glsl"))) {
77 return false;
78 }
79 for(int i=0; i<MRT_COUNT; i++) {
80 char uname[32];
81 sprintf(uname, "mrt%d", i);
82 set_uniform_int(deferred_omni, uname, i);
83 }
84 return true;
85 }
87 void destroy_renderer()
88 {
89 free_program(mrt_prog);
90 free_program(deferred_omni);
92 glDeleteTextures(MRT_COUNT, mrt_tex);
93 glDeleteFramebuffersEXT(1, &fbo);
94 }
96 void resize_renderer(int xsz, int ysz)
97 {
98 fb_xsz = xsz;
99 fb_ysz = ysz;
101 // if we need a bigger rendertarget resize all colorbuffer textures and the depth buffer
102 if(xsz > tex_xsz || ysz > tex_ysz) {
103 tex_xsz = round_pow2(xsz);
104 tex_ysz = round_pow2(ysz);
106 for(int i=0; i<MRT_COUNT; i++) {
107 glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
108 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA,
109 GL_UNSIGNED_BYTE, 0);
110 }
112 glBindRenderbufferEXT(GL_RENDERBUFFER, rbuf_depth);
113 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
114 }
117 // update the texture coordinate scaling factors
118 float tex_scale_x = (float)fb_xsz / tex_xsz;
119 float tex_scale_y = (float)fb_ysz / tex_ysz;
121 set_uniform_float2(deferred_omni, "tex_scale", tex_scale_x, tex_scale_y);
122 set_uniform_float2(deferred_debug, "tex_scale", tex_scale_x, tex_scale_y);
123 }
125 void render_deferred(const Level *level)
126 {
127 glClearColor(0.4, 0.2, 0.1, 0.0);
128 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
129 glPushAttrib(GL_ENABLE_BIT);
130 glDisable(GL_LIGHTING);
131 level->draw_lights();
132 glPopAttrib();
133 #if 0
134 // render into the MRT buffers
135 glUseProgram(mrt_prog);
136 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
138 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
139 level->draw();
141 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
144 // post-process lighting
145 glPushAttrib(GL_ENABLE_BIT);
147 glDisable(GL_LIGHTING);
148 glDisable(GL_DEPTH_TEST);
150 glUseProgram(deferred_omni);
151 for(int i=0; i<MRT_COUNT; i++) {
152 glActiveTexture(GL_TEXTURE0 + i);
153 glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
154 glEnable(GL_TEXTURE_2D);
155 }
157 glDepthMask(0);
158 level->draw_lights();
159 glDepthMask(1);
161 for(int i=0; i<MRT_COUNT; i++) {
162 glActiveTexture(GL_TEXTURE0 + MRT_COUNT - i - 1);
163 glDisable(GL_TEXTURE_2D);
164 }
166 glUseProgram(0);
167 glPopAttrib();
168 #endif
169 }
171 static bool create_fbo(int xsz, int ysz)
172 {
173 unsigned int clamp = GL_ARB_texture_border_clamp ? GL_CLAMP_TO_EDGE : GL_CLAMP;
175 tex_xsz = round_pow2(xsz);
176 tex_ysz = round_pow2(ysz);
177 fb_xsz = xsz;
178 fb_ysz = ysz;
180 if(!glGenFramebuffersEXT) {
181 fprintf(stderr, "FBO support missing\n");
182 return false;
183 }
184 glGenFramebuffersEXT(1, &fbo);
185 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
187 glGenTextures(MRT_COUNT, mrt_tex);
188 for(int i=0; i<MRT_COUNT; i++) {
189 glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
190 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp);
191 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp);
192 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
193 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
194 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
196 // attach to fbo
197 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i,
198 GL_TEXTURE_2D, mrt_tex[i], 0);
199 CHECKGLERR;
200 }
202 static GLenum draw_bufs[] = {
203 GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT,
204 GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT,
205 GL_COLOR_ATTACHMENT4_EXT, GL_COLOR_ATTACHMENT5_EXT,
206 GL_COLOR_ATTACHMENT6_EXT, GL_COLOR_ATTACHMENT7_EXT
207 };
208 glDrawBuffersARB(MRT_COUNT, draw_bufs);
210 glGenRenderbuffersEXT(1, &rbuf_depth);
211 glBindRenderbufferEXT(GL_RENDERBUFFER, rbuf_depth);
212 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
213 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth);
215 int fbst = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER);
216 if(fbst != GL_FRAMEBUFFER_COMPLETE) {
217 fprintf(stderr, "incomplete fbo: %u (%s)\n", fbo, fbstname[fbst - GL_FRAMEBUFFER_COMPLETE]);
218 return false;
219 }
220 CHECKGLERR;
222 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
223 return true;
224 }
226 static unsigned int load_sdr(const char *vfname, const char *pfname)
227 {
228 char vsfile[PATH_MAX], psfile[PATH_MAX];
229 const char *fname;
230 unsigned int prog;
232 if((fname = datafile_path(vfname))) {
233 strcpy(vsfile, fname);
234 } else {
235 vsfile[0] = 0;
236 }
237 if((fname = datafile_path(pfname))) {
238 strcpy(psfile, fname);
239 } else {
240 psfile[0] = 0;
241 }
242 if(!(prog = create_program_load(vsfile, psfile))) {
243 fprintf(stderr, "failed to load shader program (%s, %s)\n", vsfile, psfile);
244 return 0;
245 }
246 return prog;
247 }
249 static int round_pow2(int x)
250 {
251 x--;
252 x = (x >> 1) | x;
253 x = (x >> 2) | x;
254 x = (x >> 4) | x;
255 x = (x >> 8) | x;
256 x = (x >> 16) | x;
257 return x + 1;
258 }