dungeon_crawler

changeset 65:fc2b3d06d07c

made the deferred renderer draw into a texture, so that I can reuse the depth buffer for geometric post effects (particles), and implement other per-pixel post effects eventually
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 02 Oct 2012 13:42:18 +0300
parents 0b130c6e534d
children 6a471c87f9ca 2560a7ab0243
files prototype/sdr/post.p.glsl prototype/sdr/post.v.glsl prototype/src/renderer_deferred.cc prototype/src/renderer_deferred.h
diffstat 4 files changed, 87 insertions(+), 15 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/prototype/sdr/post.p.glsl	Tue Oct 02 13:42:18 2012 +0300
     1.3 @@ -0,0 +1,6 @@
     1.4 +uniform sampler2D fbtex;
     1.5 +
     1.6 +void main()
     1.7 +{
     1.8 +	gl_FragColor = texture2D(fbtex, gl_TexCoord[0].st);
     1.9 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/prototype/sdr/post.v.glsl	Tue Oct 02 13:42:18 2012 +0300
     2.3 @@ -0,0 +1,5 @@
     2.4 +void main()
     2.5 +{
     2.6 +	gl_Position = gl_Vertex;
     2.7 +	gl_TexCoord[0] = gl_MultiTexCoord0;
     2.8 +}
     3.1 --- a/prototype/src/renderer_deferred.cc	Tue Oct 02 05:03:26 2012 +0300
     3.2 +++ b/prototype/src/renderer_deferred.cc	Tue Oct 02 13:42:18 2012 +0300
     3.3 @@ -9,17 +9,18 @@
     3.4  #include "datapath.h"
     3.5  
     3.6  static unsigned int load_sdr(const char *vfname, const char *pfname);
     3.7 +static void attach_color_buffer(unsigned int fbo, int count, const unsigned int *tex);
     3.8  static int round_pow2(int x);
     3.9  
    3.10  DeferredRenderer::DeferredRenderer()
    3.11  {
    3.12 -	fbo = rbuf_depth = 0;
    3.13 +	fbo = rbuf_depth = rend_tex = 0;
    3.14  	for(int i=0; i<MRT_COUNT; i++) {
    3.15  		mrt_tex[i] = 0;
    3.16  	}
    3.17  	tex_xsz = tex_ysz = 0;
    3.18  
    3.19 -	mrt_prog = deferred_debug = deferred_omni = 0;
    3.20 +	mrt_prog = deferred_debug = deferred_omni = post_sdr = 0;
    3.21  
    3.22  	curr_prog = 0;
    3.23  	num_draw_bufs = 1;
    3.24 @@ -36,10 +37,19 @@
    3.25  	if(deferred_debug) {
    3.26  		free_program(deferred_debug);
    3.27  	}
    3.28 +	if(post_sdr) {
    3.29 +		free_program(post_sdr);
    3.30 +	}
    3.31  
    3.32  	if(mrt_tex[0]) {
    3.33  		glDeleteTextures(MRT_COUNT, mrt_tex);
    3.34  	}
    3.35 +	if(rend_tex) {
    3.36 +		glDeleteTextures(1, &rend_tex);
    3.37 +	}
    3.38 +	if(rbuf_depth) {
    3.39 +		glDeleteRenderbuffersEXT(1, &rbuf_depth);
    3.40 +	}
    3.41  	if(fbo) {
    3.42  		glDeleteFramebuffersEXT(1, &fbo);
    3.43  	}
    3.44 @@ -87,6 +97,10 @@
    3.45  		set_uniform_int(deferred_omni, uname, i);
    3.46  	}
    3.47  
    3.48 +	if(!(post_sdr = load_sdr("post.v.glsl", "post.p.glsl"))) {
    3.49 +		return false;
    3.50 +	}
    3.51 +
    3.52  	rend = this;
    3.53  	return true;
    3.54  }
    3.55 @@ -117,6 +131,10 @@
    3.56  			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, tex_xsz, tex_ysz, 0, GL_RGBA, GL_FLOAT, 0);
    3.57  		}
    3.58  
    3.59 +		// ... resize the render texture
    3.60 +		glBindTexture(GL_TEXTURE_2D, rend_tex);
    3.61 +		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA, GL_FLOAT, 0);
    3.62 +
    3.63  		// ... resize the depth buffer
    3.64  		glBindRenderbufferEXT(GL_RENDERBUFFER, rbuf_depth);
    3.65  		glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
    3.66 @@ -139,10 +157,13 @@
    3.67  	curr_prog = mrt_prog;
    3.68  
    3.69  	glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
    3.70 +	attach_color_buffer(fbo, num_draw_bufs, mrt_tex);
    3.71  
    3.72  	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    3.73  	level->draw();
    3.74 -	glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
    3.75 +
    3.76 +	attach_color_buffer(fbo, 1, &rend_tex);
    3.77 +	glClear(GL_COLOR_BUFFER_BIT);
    3.78  
    3.79  	// post-process lighting
    3.80  	light_pass(level);
    3.81 @@ -151,11 +172,30 @@
    3.82  	curr_prog = 0;
    3.83  
    3.84  	render_post(level);
    3.85 +
    3.86 +	glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
    3.87 +
    3.88 +	// XXX this should be moved to render_post
    3.89 +	glBindTexture(GL_TEXTURE_2D, rend_tex);
    3.90 +	glEnable(GL_TEXTURE_2D);
    3.91 +
    3.92 +	glUseProgram(post_sdr);
    3.93 +	glBegin(GL_QUADS);
    3.94 +	glTexCoord2f(0, 0);
    3.95 +	glVertex2f(-1, -1);
    3.96 +	glTexCoord2f((float)width / tex_xsz, 0);
    3.97 +	glVertex2f(1, -1);
    3.98 +	glTexCoord2f((float)width / tex_xsz, (float)height / tex_ysz);
    3.99 +	glVertex2f(1, 1);
   3.100 +	glTexCoord2f(0, (float)height / tex_ysz);
   3.101 +	glVertex2f(-1, 1);
   3.102 +	glEnd();
   3.103 +	glUseProgram(0);
   3.104  }
   3.105  
   3.106  void DeferredRenderer::light_pass(const Level *level) const
   3.107  {
   3.108 -	glPushAttrib(GL_ENABLE_BIT | GL_POLYGON_BIT);
   3.109 +	glPushAttrib(GL_ENABLE_BIT | GL_POLYGON_BIT | GL_TEXTURE_BIT);
   3.110  
   3.111  	glEnable(GL_BLEND);
   3.112  	glBlendFunc(GL_ONE, GL_ONE);
   3.113 @@ -212,6 +252,15 @@
   3.114  	glGenFramebuffersEXT(1, &fbo);
   3.115  	glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
   3.116  
   3.117 +	// create the result render target
   3.118 +	glGenTextures(1, &rend_tex);
   3.119 +	glBindTexture(GL_TEXTURE_2D, rend_tex);
   3.120 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp);
   3.121 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp);
   3.122 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   3.123 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   3.124 +	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA, GL_FLOAT, 0);
   3.125 +
   3.126  	glGenTextures(MRT_COUNT, mrt_tex);
   3.127  	for(int i=0; i<MRT_COUNT; i++) {
   3.128  		glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
   3.129 @@ -227,16 +276,6 @@
   3.130  		CHECKGLERR;
   3.131  	}
   3.132  
   3.133 -	if(num_draw_bufs == MRT_COUNT) {
   3.134 -		static GLenum draw_bufs[] = {
   3.135 -			GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT,
   3.136 -			GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT,
   3.137 -			GL_COLOR_ATTACHMENT4_EXT, GL_COLOR_ATTACHMENT5_EXT,
   3.138 -			GL_COLOR_ATTACHMENT6_EXT, GL_COLOR_ATTACHMENT7_EXT
   3.139 -		};
   3.140 -		glDrawBuffersARB(num_draw_bufs, draw_bufs);
   3.141 -	}
   3.142 -
   3.143  	glGenRenderbuffersEXT(1, &rbuf_depth);
   3.144  	glBindRenderbufferEXT(GL_RENDERBUFFER, rbuf_depth);
   3.145  	glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
   3.146 @@ -270,6 +309,25 @@
   3.147  	return prog;
   3.148  }
   3.149  
   3.150 +static void attach_color_buffer(unsigned int fbo, int count, const unsigned int *tex)
   3.151 +{
   3.152 +	glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
   3.153 +
   3.154 +	for(int i=0; i<count; i++) {
   3.155 +		GLenum color_att = GL_COLOR_ATTACHMENT0 + i;
   3.156 +		glFramebufferTexture2DEXT(GL_FRAMEBUFFER, color_att, GL_TEXTURE_2D, tex[i], 0);
   3.157 +		CHECKGLERR;
   3.158 +	}
   3.159 +
   3.160 +	static GLenum draw_bufs[] = {
   3.161 +		GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT,
   3.162 +		GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT,
   3.163 +		GL_COLOR_ATTACHMENT4_EXT, GL_COLOR_ATTACHMENT5_EXT,
   3.164 +		GL_COLOR_ATTACHMENT6_EXT, GL_COLOR_ATTACHMENT7_EXT
   3.165 +	};
   3.166 +	glDrawBuffersARB(count, draw_bufs);
   3.167 +}
   3.168 +
   3.169  static int round_pow2(int x)
   3.170  {
   3.171  	x--;
     4.1 --- a/prototype/src/renderer_deferred.h	Tue Oct 02 05:03:26 2012 +0300
     4.2 +++ b/prototype/src/renderer_deferred.h	Tue Oct 02 13:42:18 2012 +0300
     4.3 @@ -7,12 +7,15 @@
     4.4  
     4.5  class DeferredRenderer : public Renderer {
     4.6  protected:
     4.7 -	unsigned int fbo, rbuf_depth;
     4.8 +	// render targets
     4.9 +	unsigned int fbo, rend_tex, rbuf_depth;
    4.10  	unsigned int mrt_tex[MRT_COUNT];
    4.11  	int tex_xsz, tex_ysz;
    4.12  
    4.13 +	// shaders
    4.14  	unsigned int mrt_prog;
    4.15  	unsigned int deferred_omni, deferred_debug;
    4.16 +	unsigned int post_sdr;
    4.17  
    4.18  	mutable unsigned int curr_prog;
    4.19