goat3dgfx

diff src/shader.cc @ 24:dc5918c62a64

broken: converting to resman
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Mar 2014 22:04:29 +0200
parents 6f82b9b6d6c3
children 3ba80928b530
line diff
     1.1 --- a/src/shader.cc	Tue Feb 25 23:47:48 2014 +0200
     1.2 +++ b/src/shader.cc	Sat Mar 01 22:04:29 2014 +0200
     1.3 @@ -6,7 +6,6 @@
     1.4  #include "shader.h"
     1.5  #include "logger.h"
     1.6  #include "unistate.h"
     1.7 -#include "mesh.h"
     1.8  
     1.9  #ifdef _MSC_VER
    1.10  #include <malloc.h>
    1.11 @@ -21,8 +20,7 @@
    1.12  #define HAVE_TESSELATION_SHADER
    1.13  #endif
    1.14  
    1.15 -static void bind_standard_attr(const ShaderProg *prog);
    1.16 -static const char *strtype(unsigned int type);
    1.17 +static std::string read_source(const char *fname);
    1.18  
    1.19  ShaderProg *ShaderProg::current;
    1.20  
    1.21 @@ -43,11 +41,9 @@
    1.22  	return ShaderProg::current;
    1.23  }
    1.24  
    1.25 -
    1.26  Shader::Shader()
    1.27  {
    1.28  	sdr = type = 0;
    1.29 -	name = 0;
    1.30  }
    1.31  
    1.32  Shader::~Shader()
    1.33 @@ -62,14 +58,12 @@
    1.34  
    1.35  void Shader::set_name(const char *name)
    1.36  {
    1.37 -	delete [] this->name;
    1.38 -	this->name = new char[strlen(name) + 1];
    1.39 -	strcpy(this->name, name);
    1.40 +	this->name = std::string(name);
    1.41  }
    1.42  
    1.43  const char *Shader::get_name() const
    1.44  {
    1.45 -	return name;
    1.46 +	return name.c_str();
    1.47  }
    1.48  
    1.49  bool Shader::create(const char *src, unsigned int type)
    1.50 @@ -80,11 +74,14 @@
    1.51  	const char *src_arr[] = { "precision mediump float; ", src };
    1.52  #endif
    1.53  
    1.54 +	// keep a copy of the source code
    1.55 +	this->src = std::string(src);
    1.56 +
    1.57  	if(!sdr) {
    1.58  		sdr = glCreateShader(type);
    1.59  	}
    1.60  
    1.61 -	info_log("compiling shader: %s... ", name ? name : "");
    1.62 +	info_log("compiling shader: %s... ", name.c_str());
    1.63  
    1.64  	glShaderSource(sdr, sizeof src_arr / sizeof *src_arr, src_arr, 0);
    1.65  	glCompileShader(sdr);
    1.66 @@ -118,17 +115,17 @@
    1.67  	}
    1.68  	sdr = type = 0;
    1.69  
    1.70 -	delete [] name;
    1.71 -	name = 0;
    1.72 +	src.clear();
    1.73 +	name.clear();
    1.74  }
    1.75  
    1.76 -bool Shader::load(const char *fname, unsigned int type)
    1.77 +static std::string read_source(const char *fname)
    1.78  {
    1.79  	FILE *fp;
    1.80  
    1.81 -	if(!(fp = fopen(fname, "rb"))) {
    1.82 -		error_log("failed to load %s shader: %s: %s\n", strtype(type), fname, strerror(errno));
    1.83 -		return false;
    1.84 +	if(!(fp = fopen(datafile_path(fname).c_str(), "rb"))) {
    1.85 +		error_log("failed to load shader: %s: %s\n", fname, strerror(errno));
    1.86 +		return std::string();
    1.87  	}
    1.88  
    1.89  	fseek(fp, 0, SEEK_END);
    1.90 @@ -137,15 +134,25 @@
    1.91  
    1.92  	char *src = (char*)alloca(sz + 1);
    1.93  	if(fread(src, 1, sz, fp) < (size_t)sz) {
    1.94 -		error_log("failed to load %s shader: %s: %s\n", strtype(type), fname, strerror(errno));
    1.95 +		error_log("failed to load shader: %s: %s\n", fname, strerror(errno));
    1.96  		fclose(fp);
    1.97 -		return false;
    1.98 +		delete [] src;
    1.99 +		return std::string();
   1.100  	}
   1.101  	src[sz] = 0;
   1.102  	fclose(fp);
   1.103  
   1.104 +	return std::string(src);
   1.105 +}
   1.106 +
   1.107 +bool Shader::load(const char *fname, unsigned int type)
   1.108 +{
   1.109 +	std::string src = read_source(fname);
   1.110 +	if(src.empty()) {
   1.111 +		return false;
   1.112 +	}
   1.113  	set_name(fname);
   1.114 -	return create(src, type);
   1.115 +	return create(src.c_str(), type);
   1.116  }
   1.117  
   1.118  // ---- shader program ----
   1.119 @@ -191,6 +198,7 @@
   1.120  		src = va_arg(ap, const char*);
   1.121  		type = va_arg(ap, unsigned int);
   1.122  	}
   1.123 +
   1.124  	return link();
   1.125  }
   1.126  
   1.127 @@ -309,7 +317,6 @@
   1.128  	if(status) {
   1.129  		must_link = false;
   1.130  		cache_state_uniforms();
   1.131 -
   1.132  		return true;
   1.133  	}
   1.134  	return false;
   1.135 @@ -524,78 +531,89 @@
   1.136  	}
   1.137  }
   1.138  
   1.139 -// ---- ShaderSet ----
   1.140 -static Shader *load_shader(const char *fname, unsigned int type)
   1.141 +// Static functions to use with ShaderSet
   1.142 +Shader *Shader::create_shader()
   1.143  {
   1.144 -	Shader *sdr = new Shader;
   1.145 -	if(!sdr->load(fname, type)) {
   1.146 -		delete sdr;
   1.147 -		return 0;
   1.148 -	}
   1.149 -	return sdr;
   1.150 +	return new Shader;
   1.151  }
   1.152  
   1.153 -static Shader *load_vertex_shader(const char *fname)
   1.154 +bool Shader::load_shader(Shader *sdr, const char *fname)
   1.155  {
   1.156 -	return load_shader(fname, GL_VERTEX_SHADER);
   1.157 +	std::string src = read_source(fname);
   1.158 +	if(src.empty()) {
   1.159 +		return false;
   1.160 +	}
   1.161 +
   1.162 +	sdr->src = src;
   1.163 +	return true;
   1.164  }
   1.165  
   1.166 -static Shader *load_pixel_shader(const char *fname)
   1.167 +bool Shader::done_shader(Shader *sdr, unsigned int type)
   1.168  {
   1.169 -	return load_shader(fname, GL_FRAGMENT_SHADER);
   1.170 +	return sdr->create(sdr->src.c_str(), type);
   1.171 +}
   1.172 +
   1.173 +static bool done_vertex_shader(Shader *sdr)
   1.174 +{
   1.175 +	return Shader::done_shader(sdr, GL_VERTEX_SHADER);
   1.176 +}
   1.177 +
   1.178 +static bool done_pixel_shader(Shader *sdr)
   1.179 +{
   1.180 +	return Shader::done_shader(sdr, GL_FRAGMENT_SHADER);
   1.181  }
   1.182  
   1.183  #ifdef HAVE_GEOMETRY_SHADER
   1.184 -static Shader *load_geom_shader(const char *fname)
   1.185 +static bool done_geom_shader(Shader *sdr)
   1.186  {
   1.187 -	return load_shader(fname, GL_GEOMETRY_SHADER);
   1.188 +	return Shader::done_shader(sdr, GL_GEOMETRY_SHADER);
   1.189  }
   1.190  #endif
   1.191  
   1.192  #ifdef HAVE_TESSELATION_SHADER
   1.193 -static Shader *load_tc_shader(const char *fname)
   1.194 +static bool done_tc_shader(Shader *sdr)
   1.195  {
   1.196 -	return load_shader(fname, GL_TESS_CONTROL_SHADER);
   1.197 +	return Shader::done_shader(sdr, GL_TESS_CONTROL_SHADER);
   1.198  }
   1.199  
   1.200 -static Shader *load_te_shader(const char *fname)
   1.201 +static bool done_te_shader(Shader *sdr)
   1.202  {
   1.203 -	return load_shader(fname, GL_TESS_EVALUATION_SHADER);
   1.204 +	return Shader::done_shader(sdr, GL_TESS_EVALUATION_SHADER);
   1.205  }
   1.206  #endif
   1.207  
   1.208 -static void destroy_shader(Shader *sdr)
   1.209 +void Shader::destroy_shader(Shader *sdr)
   1.210  {
   1.211  	delete sdr;
   1.212  }
   1.213  
   1.214 +// ---- ShaderSet ----
   1.215 +
   1.216  ShaderSet::ShaderSet(unsigned int type)
   1.217 -	: DataSet<Shader*>(0, destroy_shader)
   1.218 +	: DataSet<Shader*>(Shader::create_shader, Shader::load_shader, 0, Shader::destroy_shader)
   1.219  {
   1.220 -	this->type = type;
   1.221 -
   1.222  	switch(type) {
   1.223  	case GL_VERTEX_SHADER:
   1.224 -		load = load_vertex_shader;
   1.225 +		done = done_vertex_shader;
   1.226  		break;
   1.227  
   1.228  	case GL_FRAGMENT_SHADER:
   1.229 -		load = load_pixel_shader;
   1.230 +		done = done_pixel_shader;
   1.231  		break;
   1.232  
   1.233  #ifdef HAVE_GEOMETRY_SHADER
   1.234  	case GL_GEOMETRY_SHADER:
   1.235 -		load = load_geom_shader;
   1.236 +		done = done_geom_shader;
   1.237  		break;
   1.238  #endif
   1.239  
   1.240  #ifdef HAVE_TESSELATION_SHADER
   1.241  	case GL_TESS_CONTROL_SHADER:
   1.242 -		load = load_tc_shader;
   1.243 +		done = done_tc_shader;
   1.244  		break;
   1.245  
   1.246  	case GL_TESS_EVALUATION_SHADER:
   1.247 -		load = load_te_shader;
   1.248 +		done = done_te_shader;
   1.249  		break;
   1.250  #endif
   1.251  
   1.252 @@ -637,6 +655,7 @@
   1.253  }
   1.254  
   1.255  
   1.256 +/*
   1.257  static const char *strtype(unsigned int type)
   1.258  {
   1.259  	switch(type) {
   1.260 @@ -659,3 +678,4 @@
   1.261  	}
   1.262  	return "<unknown>";
   1.263  }
   1.264 +*/