| 1 |
root |
1.12 |
#include <cassert> |
| 2 |
|
|
|
| 3 |
root |
1.1 |
#include "shader.h" |
| 4 |
|
|
#include "shader_vars.h" |
| 5 |
|
|
|
| 6 |
root |
1.17 |
#include <sstream> |
| 7 |
|
|
#include <iomanip> |
| 8 |
|
|
|
| 9 |
root |
1.18 |
namespace shader { |
| 10 |
root |
1.17 |
|
| 11 |
root |
1.18 |
using namespace std; |
| 12 |
root |
1.1 |
|
| 13 |
|
|
refcounted::~refcounted () |
| 14 |
|
|
{ |
| 15 |
|
|
if (refcnt) |
| 16 |
|
|
abort (); |
| 17 |
|
|
} |
| 18 |
|
|
|
| 19 |
root |
1.26 |
void refcounted::refcnt_destroy () const |
| 20 |
root |
1.6 |
{ |
| 21 |
|
|
if (!--refcnt) |
| 22 |
|
|
delete this; // quite a bit of code... |
| 23 |
|
|
} |
| 24 |
|
|
|
| 25 |
root |
1.1 |
const char str_float [] = "float"; |
| 26 |
|
|
const char str_vec2 [] = "vec2"; |
| 27 |
|
|
const char str_vec3 [] = "vec3"; |
| 28 |
|
|
const char str_vec4 [] = "vec4"; |
| 29 |
|
|
const char str_mat2 [] = "mat2"; |
| 30 |
|
|
const char str_mat3 [] = "mat3"; |
| 31 |
|
|
const char str_mat4 [] = "mat4"; |
| 32 |
|
|
|
| 33 |
|
|
const char str_sampler_1d [] = "sampler1D"; |
| 34 |
|
|
const char str_sampler_1d_shadow [] = "sampler1DShadow"; |
| 35 |
|
|
const char str_sampler_2d [] = "sampler2D"; |
| 36 |
|
|
const char str_sampler_2d_shadow [] = "sampler2DShadow"; |
| 37 |
|
|
const char str_sampler_2d_rect [] = "sampler2DRect"; |
| 38 |
|
|
const char str_sampler_2d_rect_shadow [] = "sampler2DRectShadow"; |
| 39 |
|
|
const char str_sampler_3d [] = "sampler3D"; |
| 40 |
|
|
const char str_sampler_3d_rect [] = "sampler3DRect"; |
| 41 |
|
|
const char str_sampler_cube [] = "samplerCube"; |
| 42 |
|
|
|
| 43 |
|
|
unsigned int var_i::next_id = 0; |
| 44 |
|
|
|
| 45 |
root |
1.26 |
var_i::var_i (const char *domainstr, const char *typestr) |
| 46 |
|
|
: domainstr (domainstr), typestr (typestr) |
| 47 |
root |
1.1 |
{ |
| 48 |
|
|
} |
| 49 |
|
|
|
| 50 |
|
|
var_i::~var_i () |
| 51 |
|
|
{ |
| 52 |
|
|
} |
| 53 |
|
|
|
| 54 |
root |
1.24 |
#if 0 |
| 55 |
root |
1.4 |
stream_i::stream_i (const char *strtype) |
| 56 |
|
|
: var_i (strtype) |
| 57 |
|
|
{ |
| 58 |
root |
1.19 |
sprintf (name, "V%d", ++next_id); |
| 59 |
root |
1.4 |
} |
| 60 |
root |
1.24 |
#endif |
| 61 |
root |
1.4 |
|
| 62 |
root |
1.1 |
temporary_i::temporary_i (const char *strtype) |
| 63 |
root |
1.26 |
: var_i (0, strtype) |
| 64 |
root |
1.1 |
{ |
| 65 |
root |
1.19 |
sprintf (name, "T%d", ++next_id); |
| 66 |
root |
1.1 |
} |
| 67 |
|
|
|
| 68 |
root |
1.24 |
varying_i::varying_i (const char *strtype) |
| 69 |
root |
1.26 |
: var_i ("varying", strtype) |
| 70 |
root |
1.24 |
{ |
| 71 |
|
|
sprintf (name, "V%d", ++next_id); |
| 72 |
|
|
} |
| 73 |
|
|
|
| 74 |
root |
1.1 |
uniform_i::uniform_i (const char *strtype) |
| 75 |
root |
1.26 |
: var_i ("uniform", strtype) |
| 76 |
root |
1.1 |
{ |
| 77 |
root |
1.19 |
sprintf (name, "U%d", ++next_id); |
| 78 |
root |
1.1 |
} |
| 79 |
|
|
|
| 80 |
|
|
//////////////////////////////////////////////////////////////////////////// |
| 81 |
|
|
|
| 82 |
root |
1.26 |
void var_i::operator ()() const |
| 83 |
root |
1.1 |
{ |
| 84 |
root |
1.28 |
shader_builder::cur->code << name; |
| 85 |
root |
1.1 |
|
| 86 |
root |
1.28 |
if (shader_builder::cur->first (this)) |
| 87 |
root |
1.26 |
{ |
| 88 |
|
|
if (domainstr) |
| 89 |
root |
1.28 |
shader_builder::cur->global << domainstr << ' ' << typestr << ' ' << name << ";\n"; |
| 90 |
root |
1.26 |
else |
| 91 |
root |
1.28 |
shader_builder::cur->local << " " << typestr << ' ' << name << ";\n"; |
| 92 |
root |
1.26 |
} |
| 93 |
root |
1.1 |
} |
| 94 |
|
|
|
| 95 |
|
|
//////////////////////////////////////////////////////////////////////////// |
| 96 |
|
|
|
| 97 |
root |
1.20 |
int texture_units::unit_count = 8; |
| 98 |
root |
1.21 |
int texture_units::units[8] = { 7, 6, 5, 4, 3, 2, 1, 0 }; |
| 99 |
root |
1.20 |
|
| 100 |
root |
1.1 |
shader_object_i *cur = 0; |
| 101 |
|
|
|
| 102 |
|
|
shader_object_i::shader_object_i (GLenum type) |
| 103 |
|
|
: type (type) |
| 104 |
|
|
{ |
| 105 |
|
|
id = glCreateShaderObjectARB (type); |
| 106 |
root |
1.12 |
assert (id); |
| 107 |
root |
1.1 |
} |
| 108 |
|
|
|
| 109 |
|
|
shader_object_i::~shader_object_i () |
| 110 |
|
|
{ |
| 111 |
|
|
glDeleteObjectARB (id); |
| 112 |
|
|
} |
| 113 |
|
|
|
| 114 |
root |
1.17 |
static string linify (const string &s) |
| 115 |
|
|
{ |
| 116 |
|
|
ostringstream o; |
| 117 |
|
|
|
| 118 |
|
|
int b = 0, e; |
| 119 |
|
|
int l = 1; |
| 120 |
|
|
do { |
| 121 |
|
|
o << setw (3) << l << ": "; |
| 122 |
|
|
e = s.find ('\n', b); |
| 123 |
|
|
if (e == string::npos) |
| 124 |
|
|
e = s.size (); |
| 125 |
|
|
|
| 126 |
|
|
o << s.substr (b, e - b + 1); |
| 127 |
|
|
b = e + 1; |
| 128 |
|
|
l++; |
| 129 |
|
|
} while (b < s.size ()); |
| 130 |
|
|
|
| 131 |
|
|
return o.str (); |
| 132 |
|
|
} |
| 133 |
|
|
|
| 134 |
root |
1.19 |
//////////////////////////////////////////////////////////////////////////// |
| 135 |
|
|
|
| 136 |
|
|
GLint uniform_i::location () |
| 137 |
|
|
{ |
| 138 |
root |
1.27 |
assert (program_object_i::cur); |
| 139 |
root |
1.19 |
|
| 140 |
root |
1.27 |
GLint &rid = program_object_i::cur->uloc[this]; |
| 141 |
root |
1.19 |
|
| 142 |
|
|
if (!rid) |
| 143 |
root |
1.27 |
rid = glGetUniformLocationARB (program_object_i::cur->id, name); |
| 144 |
root |
1.19 |
|
| 145 |
|
|
return rid; |
| 146 |
|
|
} |
| 147 |
|
|
|
| 148 |
root |
1.28 |
program_object_i *program_object_i::cur; |
| 149 |
root |
1.27 |
|
| 150 |
|
|
program_object_i::program_object_i () |
| 151 |
root |
1.12 |
{ |
| 152 |
|
|
id = glCreateProgramObjectARB (); |
| 153 |
|
|
assert (id); |
| 154 |
root |
1.15 |
|
| 155 |
|
|
glAttachObjectARB (id, vsh->id); |
| 156 |
|
|
glAttachObjectARB (id, fsh->id); |
| 157 |
root |
1.12 |
} |
| 158 |
|
|
|
| 159 |
root |
1.27 |
program_object_i::~program_object_i () |
| 160 |
root |
1.12 |
{ |
| 161 |
|
|
glDeleteProgramsARB (1, &id); |
| 162 |
|
|
} |
| 163 |
|
|
|
| 164 |
root |
1.27 |
void program_object_i::link () |
| 165 |
root |
1.12 |
{ |
| 166 |
|
|
glLinkProgramARB (id); |
| 167 |
|
|
|
| 168 |
|
|
GLint linked; |
| 169 |
|
|
glGetObjectParameterivARB (id, GL_OBJECT_LINK_STATUS_ARB, &linked); |
| 170 |
|
|
|
| 171 |
|
|
if (!linked) |
| 172 |
|
|
{ |
| 173 |
|
|
char infolog[8192]; |
| 174 |
|
|
glGetInfoLogARB (id, 8192, NULL, infolog); |
| 175 |
|
|
printf ("LINK-INFOLOG<%s>\n", infolog); |
| 176 |
root |
1.1 |
abort (); |
| 177 |
|
|
} |
| 178 |
root |
1.19 |
|
| 179 |
|
|
uloc.clear (); |
| 180 |
root |
1.22 |
} |
| 181 |
|
|
|
| 182 |
root |
1.27 |
void program_object_i::enable () |
| 183 |
root |
1.22 |
{ |
| 184 |
|
|
glUseProgramObjectARB (id); |
| 185 |
|
|
//TODO: set samplers here |
| 186 |
|
|
cur = this; |
| 187 |
|
|
} |
| 188 |
|
|
|
| 189 |
root |
1.27 |
void program_object_i::disable () |
| 190 |
root |
1.22 |
{ |
| 191 |
|
|
//TODO: clear samplers here |
| 192 |
|
|
glUseProgramObjectARB (0); |
| 193 |
|
|
cur = 0; |
| 194 |
root |
1.1 |
} |
| 195 |
|
|
|
| 196 |
root |
1.26 |
const sl_expr< sl_string<60> > sl_convert< ::vec2 >::convert (const ::vec2 &v) |
| 197 |
root |
1.6 |
{ |
| 198 |
|
|
sl_string<60> s; |
| 199 |
|
|
sprintf (s.str, "vec2 (%g, %g)", v.x, v.y); |
| 200 |
|
|
return s; |
| 201 |
|
|
} |
| 202 |
|
|
|
| 203 |
root |
1.26 |
const sl_expr< sl_string<80> > sl_convert< ::vec3 >::convert (const ::vec3 &v) |
| 204 |
root |
1.6 |
{ |
| 205 |
|
|
sl_string<80> s; |
| 206 |
|
|
sprintf (s.str, "vec3 (%g, %g, %g)", v.x, v.y, v.z); |
| 207 |
|
|
return s; |
| 208 |
|
|
} |
| 209 |
|
|
|
| 210 |
root |
1.26 |
const sl_expr< sl_string<100> > sl_convert< ::vec4 >::convert (const ::vec4 &v) |
| 211 |
root |
1.6 |
{ |
| 212 |
|
|
sl_string<100> s; |
| 213 |
|
|
sprintf (s.str, "vec4 (%g, %g, %g, %g)", v.x, v.y, v.z, v.w); |
| 214 |
|
|
return s; |
| 215 |
|
|
} |
| 216 |
|
|
|
| 217 |
root |
1.28 |
shader_builder *shader_builder::cur = 0; |
| 218 |
root |
1.5 |
|
| 219 |
root |
1.28 |
bool shader_builder::first (const void *p) |
| 220 |
|
|
{ |
| 221 |
|
|
if (seen.find (p) == seen.end ()) |
| 222 |
|
|
{ |
| 223 |
|
|
seen.insert (p); |
| 224 |
|
|
return true; |
| 225 |
|
|
} |
| 226 |
root |
1.26 |
|
| 227 |
root |
1.28 |
return false; |
| 228 |
|
|
} |
| 229 |
root |
1.26 |
|
| 230 |
root |
1.28 |
void shader_builder::start () |
| 231 |
|
|
{ |
| 232 |
|
|
cur = new shader_builder; |
| 233 |
root |
1.5 |
} |
| 234 |
|
|
|
| 235 |
root |
1.28 |
string shader_builder::stop () |
| 236 |
root |
1.1 |
{ |
| 237 |
root |
1.28 |
ostringstream os; |
| 238 |
root |
1.20 |
|
| 239 |
root |
1.28 |
os << cur->global.str () |
| 240 |
|
|
<< "\nvoid main (void)\n" |
| 241 |
|
|
<< "{\n" |
| 242 |
|
|
<< cur->local.str () |
| 243 |
|
|
<< "\n" |
| 244 |
|
|
<< cur->code.str () |
| 245 |
|
|
<< "}\n"; |
| 246 |
root |
1.23 |
|
| 247 |
root |
1.28 |
delete cur; |
| 248 |
|
|
cur = 0; |
| 249 |
root |
1.1 |
|
| 250 |
root |
1.28 |
return os.str (); |
| 251 |
|
|
} |
| 252 |
root |
1.1 |
|
| 253 |
root |
1.28 |
void shader_object_i::compile (const string &source) |
| 254 |
|
|
{ |
| 255 |
|
|
const char *sptr = source.data (); |
| 256 |
|
|
const int slen = source.size (); |
| 257 |
root |
1.1 |
|
| 258 |
root |
1.28 |
printf ("%s\n", linify (source).c_str ()); |
| 259 |
|
|
glShaderSourceARB (id, 1, &sptr, &slen); |
| 260 |
|
|
glCompileShaderARB (id); |
| 261 |
root |
1.1 |
|
| 262 |
root |
1.28 |
GLint compiled; |
| 263 |
|
|
glGetObjectParameterivARB (id, GL_OBJECT_COMPILE_STATUS_ARB, &compiled); |
| 264 |
root |
1.10 |
|
| 265 |
root |
1.28 |
if (!compiled) |
| 266 |
|
|
{ |
| 267 |
|
|
char infolog[8192]; |
| 268 |
|
|
glGetInfoLogARB (id, 8192, NULL, infolog); |
| 269 |
|
|
printf ("%s\n", linify (source).c_str ()); |
| 270 |
|
|
printf ("%s\n", infolog); |
| 271 |
|
|
abort (); |
| 272 |
|
|
} |
| 273 |
root |
1.1 |
} |
| 274 |
|
|
|
| 275 |
|
|
} |
| 276 |
|
|
|