1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
|
struct GlobalShaderParamState
{
const char *name;
union
{
float fval[32];
int ival[32];
uint uval[32];
uchar buf[32*sizeof(float)];
};
int version;
static int nextversion;
void resetversions();
void changed()
{
if(++nextversion < 0) resetversions();
version = nextversion;
}
};
struct ShaderParamBinding
{
int loc, size;
GLenum format;
};
struct GlobalShaderParamUse : ShaderParamBinding
{
GlobalShaderParamState *param;
int version;
void flush()
{
if(version == param->version) return;
switch(format)
{
case GL_BOOL:
case GL_FLOAT: glUniform1fv_(loc, size, param->fval); break;
case GL_BOOL_VEC2:
case GL_FLOAT_VEC2: glUniform2fv_(loc, size, param->fval); break;
case GL_BOOL_VEC3:
case GL_FLOAT_VEC3: glUniform3fv_(loc, size, param->fval); break;
case GL_BOOL_VEC4:
case GL_FLOAT_VEC4: glUniform4fv_(loc, size, param->fval); break;
case GL_INT: glUniform1iv_(loc, size, param->ival); break;
case GL_INT_VEC2: glUniform2iv_(loc, size, param->ival); break;
case GL_INT_VEC3: glUniform3iv_(loc, size, param->ival); break;
case GL_INT_VEC4: glUniform4iv_(loc, size, param->ival); break;
case GL_FLOAT_MAT2: glUniformMatrix2fv_(loc, 1, GL_FALSE, param->fval); break;
case GL_FLOAT_MAT3: glUniformMatrix3fv_(loc, 1, GL_FALSE, param->fval); break;
case GL_FLOAT_MAT4: glUniformMatrix4fv_(loc, 1, GL_FALSE, param->fval); break;
}
version = param->version;
}
};
struct LocalShaderParamState : ShaderParamBinding
{
const char *name;
};
struct SlotShaderParam
{
const char *name;
int loc;
float val[4];
};
struct SlotShaderParamState : LocalShaderParamState
{
float val[4];
SlotShaderParamState() {}
SlotShaderParamState(const SlotShaderParam &p)
{
name = p.name;
loc = -1;
size = 1;
format = GL_FLOAT_VEC4;
memcpy(val, p.val, sizeof(val));
}
};
enum
{
SHADER_DEFAULT = 0,
SHADER_NORMALSLMS = 1<<0,
SHADER_ENVMAP = 1<<1,
SHADER_OPTION = 1<<3,
SHADER_INVALID = 1<<8,
SHADER_DEFERRED = 1<<9
};
#define MAXSHADERDETAIL 3
#define MAXVARIANTROWS 5
extern int shaderdetail;
struct Slot;
struct VSlot;
struct UniformLoc
{
const char *name, *blockname;
int loc, version, binding, stride, offset, size;
void *data;
UniformLoc(const char *name = NULL, const char *blockname = NULL, int binding = -1, int stride = -1) : name(name), blockname(blockname), loc(-1), version(-1), binding(binding), stride(stride), offset(-1), size(-1), data(NULL) {}
};
struct AttribLoc
{
const char *name;
int loc;
AttribLoc(const char *name = NULL, int loc = -1) : name(name), loc(loc) {}
};
struct Shader
{
static Shader *lastshader;
char *name, *vsstr, *psstr, *defer;
int type;
GLuint program, vsobj, psobj;
vector<SlotShaderParamState> defaultparams;
vector<GlobalShaderParamUse> globalparams;
vector<LocalShaderParamState> localparams;
vector<uchar> localparamremap;
Shader *detailshader, *variantshader, *altshader, *fastshader[MAXSHADERDETAIL];
vector<Shader *> variants;
ushort *variantrows;
bool standard, forced, used;
Shader *reusevs, *reuseps;
vector<UniformLoc> uniformlocs;
vector<AttribLoc> attriblocs;
const void *owner;
Shader() : name(NULL), vsstr(NULL), psstr(NULL), defer(NULL), type(SHADER_DEFAULT), program(0), vsobj(0), psobj(0), detailshader(NULL), variantshader(NULL), altshader(NULL), variantrows(NULL), standard(false), forced(false), used(false), reusevs(NULL), reuseps(NULL), owner(NULL)
{
loopi(MAXSHADERDETAIL) fastshader[i] = this;
}
~Shader()
{
DELETEA(name);
DELETEA(vsstr);
DELETEA(psstr);
DELETEA(defer);
DELETEA(variantrows);
}
void fixdetailshader(bool force = true, bool recurse = true);
void allocparams(Slot *slot = NULL);
void setslotparams(Slot &slot);
void setslotparams(Slot &slot, VSlot &vslot);
void bindprograms();
void flushparams(Slot *slot = NULL)
{
if(!used) { allocparams(slot); used = true; }
loopv(globalparams) globalparams[i].flush();
}
void force();
bool invalid() const { return (type&SHADER_INVALID)!=0; }
bool deferred() const { return (type&SHADER_DEFERRED)!=0; }
bool loaded() const { return detailshader!=NULL; }
static bool isnull(const Shader *s);
bool isnull() const { return isnull(this); }
int numvariants(int row) const
{
if(row < 0 || row >= MAXVARIANTROWS || !variantrows) return 0;
return variantrows[row+1] - variantrows[row];
}
Shader *getvariant(int col, int row) const
{
if(row < 0 || row >= MAXVARIANTROWS || col < 0 || !variantrows) return NULL;
int start = variantrows[row], end = variantrows[row+1];
return col < end - start ? variants[start + col] : NULL;
}
bool hasoption(int row)
{
if(detailshader)
{
Shader *s = getvariant(0, row);
if(s) return (s->type&SHADER_OPTION)!=0;
}
return false;
}
void addvariant(int row, Shader *s)
{
if(row < 0 || row >= MAXVARIANTROWS || variants.length() >= USHRT_MAX) return;
if(!variantrows) { variantrows = new ushort[MAXVARIANTROWS+1]; memset(variantrows, 0, (MAXVARIANTROWS+1)*sizeof(ushort)); }
variants.insert(variantrows[row+1], s);
for(int i = row+1; i <= MAXVARIANTROWS; ++i) ++variantrows[i];
}
void setvariant_(int col, int row, Shader *fallbackshader)
{
Shader *s = fallbackshader;
if(detailshader->variantrows)
{
int start = detailshader->variantrows[row], end = detailshader->variantrows[row+1];
for(col = min(start + col, end-1); col >= start; --col)
{
if(!detailshader->variants[col]->invalid()) { s = detailshader->variants[col]; break; }
}
}
if(lastshader!=s) s->bindprograms();
}
void setvariant(int col, int row, Shader *fallbackshader)
{
if(isnull() || !loaded()) return;
setvariant_(col, row, fallbackshader);
lastshader->flushparams();
}
void setvariant(int col, int row)
{
if(isnull() || !loaded()) return;
setvariant_(col, row, detailshader);
lastshader->flushparams();
}
void setvariant(int col, int row, Slot &slot, VSlot &vslot, Shader *fallbackshader)
{
if(isnull() || !loaded()) return;
setvariant_(col, row, fallbackshader);
lastshader->flushparams(&slot);
lastshader->setslotparams(slot, vslot);
}
void setvariant(int col, int row, Slot &slot, VSlot &vslot)
{
if(isnull() || !loaded()) return;
setvariant_(col, row, detailshader);
lastshader->flushparams(&slot);
lastshader->setslotparams(slot, vslot);
}
void set_()
{
if(lastshader!=detailshader) detailshader->bindprograms();
}
void set()
{
if(isnull() || !loaded()) return;
set_();
lastshader->flushparams();
}
void set(Slot &slot, VSlot &vslot)
{
if(isnull() || !loaded()) return;
set_();
lastshader->flushparams(&slot);
lastshader->setslotparams(slot, vslot);
}
bool compile();
void cleanup(bool invalid = false);
static int uniformlocversion();
};
struct GlobalShaderParam
{
const char *name;
GlobalShaderParamState *param;
GlobalShaderParam(const char *name) : name(name), param(NULL) {}
GlobalShaderParamState *resolve()
{
extern GlobalShaderParamState *getglobalparam(const char *name);
if(!param) param = getglobalparam(name);
param->changed();
return param;
}
void setf(float x = 0, float y = 0, float z = 0, float w = 0)
{
GlobalShaderParamState *g = resolve();
g->fval[0] = x;
g->fval[1] = y;
g->fval[2] = z;
g->fval[3] = w;
}
void set(const vec &v, float w = 0) { setf(v.x, v.y, v.z, w); }
void set(const vec2 &v, float z = 0, float w = 0) { setf(v.x, v.y, z, w); }
void set(const vec4 &v) { setf(v.x, v.y, v.z, v.w); }
void set(const plane &p) { setf(p.x, p.y, p.z, p.offset); }
void set(const matrix2 &m) { memcpy(resolve()->fval, m.a.v, sizeof(m)); }
void set(const matrix3 &m) { memcpy(resolve()->fval, m.a.v, sizeof(m)); }
void set(const matrix4 &m) { memcpy(resolve()->fval, m.a.v, sizeof(m)); }
template<class T>
void setv(const T *v, int n = 1) { memcpy(resolve()->buf, v, n*sizeof(T)); }
void seti(int x = 0, int y = 0, int z = 0, int w = 0)
{
GlobalShaderParamState *g = resolve();
g->ival[0] = x;
g->ival[1] = y;
g->ival[2] = z;
g->ival[3] = w;
}
void set(const ivec &v, int w = 0) { seti(v.x, v.y, v.z, w); }
void set(const ivec2 &v, int z = 0, int w = 0) { seti(v.x, v.y, z, w); }
void set(const ivec4 &v) { seti(v.x, v.y, v.z, v.w); }
void setu(uint x = 0, uint y = 0, uint z = 0, uint w = 0)
{
GlobalShaderParamState *g = resolve();
g->uval[0] = x;
g->uval[1] = y;
g->uval[2] = z;
g->uval[3] = w;
}
template<class T>
T *reserve(int n = 1) { return (T *)resolve()->buf; }
};
struct LocalShaderParam
{
const char *name;
int loc;
LocalShaderParam(const char *name) : name(name), loc(-1) {}
LocalShaderParamState *resolve()
{
Shader *s = Shader::lastshader;
if(!s) return NULL;
if(!s->localparamremap.inrange(loc))
{
extern int getlocalparam(const char *name);
if(loc == -1) loc = getlocalparam(name);
if(!s->localparamremap.inrange(loc)) return NULL;
}
uchar remap = s->localparamremap[loc];
return s->localparams.inrange(remap) ? &s->localparams[remap] : NULL;
}
void setf(float x = 0, float y = 0, float z = 0, float w = 0)
{
ShaderParamBinding *b = resolve();
if(b) switch(b->format)
{
case GL_BOOL:
case GL_FLOAT: glUniform1f_(b->loc, x); break;
case GL_BOOL_VEC2:
case GL_FLOAT_VEC2: glUniform2f_(b->loc, x, y); break;
case GL_BOOL_VEC3:
case GL_FLOAT_VEC3: glUniform3f_(b->loc, x, y, z); break;
case GL_BOOL_VEC4:
case GL_FLOAT_VEC4: glUniform4f_(b->loc, x, y, z, w); break;
case GL_INT: glUniform1i_(b->loc, int(x)); break;
case GL_INT_VEC2: glUniform2i_(b->loc, int(x), int(y)); break;
case GL_INT_VEC3: glUniform3i_(b->loc, int(x), int(y), int(z)); break;
case GL_INT_VEC4: glUniform4i_(b->loc, int(x), int(y), int(z), int(w)); break;
}
}
void set(const vec &v, float w = 0) { setf(v.x, v.y, v.z, w); }
void set(const vec2 &v, float z = 0, float w = 0) { setf(v.x, v.y, z, w); }
void set(const vec4 &v) { setf(v.x, v.y, v.z, v.w); }
void set(const plane &p) { setf(p.x, p.y, p.z, p.offset); }
void setv(const float *f, int n = 1) { ShaderParamBinding *b = resolve(); if(b) glUniform1fv_(b->loc, n, f); }
void setv(const vec *v, int n = 1) { ShaderParamBinding *b = resolve(); if(b) glUniform3fv_(b->loc, n, v->v); }
void setv(const vec2 *v, int n = 1) { ShaderParamBinding *b = resolve(); if(b) glUniform2fv_(b->loc, n, v->v); }
void setv(const vec4 *v, int n = 1) { ShaderParamBinding *b = resolve(); if(b) glUniform4fv_(b->loc, n, v->v); }
void setv(const plane *p, int n = 1) { ShaderParamBinding *b = resolve(); if(b) glUniform4fv_(b->loc, n, p->v); }
void setv(const matrix2 *m, int n = 1) { ShaderParamBinding *b = resolve(); if(b) glUniformMatrix2fv_(b->loc, n, GL_FALSE, m->a.v); }
void setv(const matrix3 *m, int n = 1) { ShaderParamBinding *b = resolve(); if(b) glUniformMatrix3fv_(b->loc, n, GL_FALSE, m->a.v); }
void setv(const matrix4 *m, int n = 1) { ShaderParamBinding *b = resolve(); if(b) glUniformMatrix4fv_(b->loc, n, GL_FALSE, m->a.v); }
void set(const matrix2 &m) { setv(&m); }
void set(const matrix3 &m) { setv(&m); }
void set(const matrix4 &m) { setv(&m); }
template<class T>
void sett(T x, T y, T z, T w)
{
ShaderParamBinding *b = resolve();
if(b) switch(b->format)
{
case GL_FLOAT: glUniform1f_(b->loc, x); break;
case GL_FLOAT_VEC2: glUniform2f_(b->loc, x, y); break;
case GL_FLOAT_VEC3: glUniform3f_(b->loc, x, y, z); break;
case GL_FLOAT_VEC4: glUniform4f_(b->loc, x, y, z, w); break;
case GL_BOOL:
case GL_INT: glUniform1i_(b->loc, x); break;
case GL_BOOL_VEC2:
case GL_INT_VEC2: glUniform2i_(b->loc, x, y); break;
case GL_BOOL_VEC3:
case GL_INT_VEC3: glUniform3i_(b->loc, x, y, z); break;
case GL_BOOL_VEC4:
case GL_INT_VEC4: glUniform4i_(b->loc, x, y, z, w); break;
}
}
void seti(int x = 0, int y = 0, int z = 0, int w = 0) { sett<int>(x, y, z, w); }
void set(const ivec &v, int w = 0) { seti(v.x, v.y, v.z, w); }
void set(const ivec2 &v, int z = 0, int w = 0) { seti(v.x, v.y, z, w); }
void set(const ivec4 &v) { seti(v.x, v.y, v.z, v.w); }
void setv(const int *i, int n = 1) { ShaderParamBinding *b = resolve(); if(b) glUniform1iv_(b->loc, n, i); }
void setv(const ivec *v, int n = 1) { ShaderParamBinding *b = resolve(); if(b) glUniform3iv_(b->loc, n, v->v); }
void setv(const ivec2 *v, int n = 1) { ShaderParamBinding *b = resolve(); if(b) glUniform2iv_(b->loc, n, v->v); }
void setv(const ivec4 *v, int n = 1) { ShaderParamBinding *b = resolve(); if(b) glUniform4iv_(b->loc, n, v->v); }
};
#define LOCALPARAM(name, vals) do { static LocalShaderParam param( #name ); param.set(vals); } while(0)
#define LOCALPARAMF(name, ...) do { static LocalShaderParam param( #name ); param.setf(__VA_ARGS__); } while(0)
#define LOCALPARAMI(name, ...) do { static LocalShaderParam param( #name ); param.seti(__VA_ARGS__); } while(0)
#define LOCALPARAMV(name, vals, num) do { static LocalShaderParam param( #name ); param.setv(vals, num); } while(0)
#define GLOBALPARAM(name, vals) do { static GlobalShaderParam param( #name ); param.set(vals); } while(0)
#define GLOBALPARAMF(name, ...) do { static GlobalShaderParam param( #name ); param.setf(__VA_ARGS__); } while(0)
#define GLOBALPARAMI(name, ...) do { static GlobalShaderParam param( #name ); param.seti(__VA_ARGS__); } while(0)
#define GLOBALPARAMV(name, vals, num) do { static GlobalShaderParam param( #name ); param.setv(vals, num); } while(0)
#define SETSHADER(name, ...) \
do { \
static Shader *name##shader = NULL; \
if(!name##shader) name##shader = lookupshaderbyname(#name); \
name##shader->set(__VA_ARGS__); \
} while(0)
#define SETVARIANT(name, ...) \
do { \
static Shader *name##shader = NULL; \
if(!name##shader) name##shader = lookupshaderbyname(#name); \
name##shader->setvariant(__VA_ARGS__); \
} while(0)
struct ImageData
{
int w, h, bpp, levels, align, pitch;
GLenum compressed;
uchar *data;
void *owner;
void (*freefunc)(void *);
ImageData()
: data(NULL), owner(NULL), freefunc(NULL)
{}
ImageData(int nw, int nh, int nbpp, int nlevels = 1, int nalign = 0, GLenum ncompressed = GL_FALSE)
{
setdata(NULL, nw, nh, nbpp, nlevels, nalign, ncompressed);
}
ImageData(int nw, int nh, int nbpp, uchar *data)
: owner(NULL), freefunc(NULL)
{
setdata(data, nw, nh, nbpp);
}
ImageData(SDL_Surface *s) { wrap(s); }
~ImageData() { cleanup(); }
void setdata(uchar *ndata, int nw, int nh, int nbpp, int nlevels = 1, int nalign = 0, GLenum ncompressed = GL_FALSE)
{
w = nw;
h = nh;
bpp = nbpp;
levels = nlevels;
align = nalign;
pitch = align ? 0 : w*bpp;
compressed = ncompressed;
data = ndata ? ndata : new uchar[calcsize()];
if(!ndata) { owner = this; freefunc = NULL; }
}
int calclevelsize(int level) const { return ((max(w>>level, 1)+align-1)/align)*((max(h>>level, 1)+align-1)/align)*bpp; }
int calcsize() const
{
if(!align) return w*h*bpp;
int lw = w, lh = h,
size = 0;
loopi(levels)
{
if(lw<=0) lw = 1;
if(lh<=0) lh = 1;
size += ((lw+align-1)/align)*((lh+align-1)/align)*bpp;
if(lw*lh==1) break;
lw >>= 1;
lh >>= 1;
}
return size;
}
void disown()
{
data = NULL;
owner = NULL;
freefunc = NULL;
}
void cleanup()
{
if(owner==this) delete[] data;
else if(freefunc) (*freefunc)(owner);
disown();
}
void replace(ImageData &d)
{
cleanup();
*this = d;
if(owner == &d) owner = this;
d.disown();
}
void wrap(SDL_Surface *s)
{
setdata((uchar *)s->pixels, s->w, s->h, s->format->BytesPerPixel);
pitch = s->pitch;
owner = s;
freefunc = (void (*)(void *))SDL_FreeSurface;
}
};
// management of texture slots
// each texture slot can have multiple texture frames, of which currently only the first is used
// additional frames can be used for various shaders
struct Texture
{
enum
{
IMAGE = 0,
CUBEMAP = 1,
TYPE = 0xFF,
STUB = 1<<8,
TRANSIENT = 1<<9,
COMPRESSED = 1<<10,
ALPHA = 1<<11,
MIRROR = 1<<12,
FLAGS = 0xFF00
};
char *name;
int type, w, h, xs, ys, bpp, clamp;
bool mipmap, canreduce;
GLuint id;
uchar *alphamask;
Texture() : alphamask(NULL) {}
};
enum
{
TEX_DIFFUSE = 0,
TEX_UNKNOWN,
TEX_DECAL,
TEX_NORMAL,
TEX_GLOW,
TEX_SPEC,
TEX_DEPTH,
TEX_ALPHA,
TEX_ENVMAP
};
enum
{
VSLOT_SHPARAM = 0,
VSLOT_SCALE,
VSLOT_ROTATION,
VSLOT_OFFSET,
VSLOT_SCROLL,
VSLOT_LAYER,
VSLOT_ALPHA,
VSLOT_COLOR,
VSLOT_NUM
};
struct VSlot
{
Slot *slot;
VSlot *next;
int index, changed;
vector<SlotShaderParam> params;
bool linked;
float scale;
int rotation;
ivec2 offset;
vec2 scroll;
int layer;
float alphafront, alphaback;
vec colorscale;
vec glowcolor;
VSlot(Slot *slot = NULL, int index = -1) : slot(slot), next(NULL), index(index), changed(0)
{
reset();
if(slot) addvariant(slot);
}
void addvariant(Slot *slot);
void reset()
{
params.shrink(0);
linked = false;
scale = 1;
rotation = 0;
offset = ivec2(0, 0);
scroll = vec2(0, 0);
layer = 0;
alphafront = 0.5f;
alphaback = 0;
colorscale = vec(1, 1, 1);
glowcolor = vec(1, 1, 1);
}
void cleanup()
{
linked = false;
}
};
struct Slot
{
struct Tex
{
int type;
Texture *t;
string name;
int combined;
};
int index;
vector<Tex> sts;
Shader *shader;
vector<SlotShaderParam> params;
VSlot *variants;
bool loaded;
uint texmask;
char *autograss;
Texture *grasstex, *thumbnail;
char *layermaskname;
int layermaskmode;
float layermaskscale;
ImageData *layermask;
Slot(int index = -1) : index(index), variants(NULL), autograss(NULL), layermaskname(NULL), layermask(NULL) { reset(); }
void reset()
{
sts.shrink(0);
shader = NULL;
params.shrink(0);
loaded = false;
texmask = 0;
DELETEA(autograss);
grasstex = NULL;
thumbnail = NULL;
DELETEA(layermaskname);
layermaskmode = 0;
layermaskscale = 1;
if(layermask) DELETEP(layermask);
}
void cleanup()
{
loaded = false;
grasstex = NULL;
thumbnail = NULL;
loopv(sts)
{
Tex &t = sts[i];
t.t = NULL;
t.combined = -1;
}
}
};
inline void VSlot::addvariant(Slot *slot)
{
if(!slot->variants) slot->variants = this;
else
{
VSlot *prev = slot->variants;
while(prev->next) prev = prev->next;
prev->next = this;
}
}
struct MSlot : Slot, VSlot
{
MSlot() : VSlot(this) {}
void reset()
{
Slot::reset();
VSlot::reset();
}
void cleanup()
{
Slot::cleanup();
VSlot::cleanup();
}
};
struct texrotation
{
bool flipx, flipy, swapxy;
};
struct cubemapside
{
GLenum target;
const char *name;
bool flipx, flipy, swapxy;
};
extern const texrotation texrotations[8];
extern const cubemapside cubemapsides[6];
extern Texture *notexture;
extern Shader *nullshader, *hudshader, *hudnotextureshader, *textureshader, *notextureshader, *nocolorshader, *foggedshader, *foggednotextureshader, *stdworldshader;
extern int reservevpparams, maxvsuniforms, maxfsuniforms;
extern Shader *lookupshaderbyname(const char *name);
extern Shader *useshaderbyname(const char *name);
extern Shader *generateshader(const char *name, const char *cmd, ...);
extern Texture *loadthumbnail(Slot &slot);
extern void resetslotshader();
extern void setslotshader(Slot &s);
extern void linkslotshader(Slot &s, bool load = true);
extern void linkvslotshader(VSlot &s, bool load = true);
extern void linkslotshaders();
extern const char *getshaderparamname(const char *name, bool insert = true);
extern void setupshaders();
extern void reloadshaders();
extern void cleanupshaders();
#define MAXDYNLIGHTS 5
#define DYNLIGHTBITS 6
#define DYNLIGHTMASK ((1<<DYNLIGHTBITS)-1)
#define MAXBLURRADIUS 7
extern void setupblurkernel(int radius, float sigma, float *weights, float *offsets);
extern void setblurshader(int pass, int size, int radius, float *weights, float *offsets);
extern void savepng(const char *filename, ImageData &image, bool flip = false);
extern void savetga(const char *filename, ImageData &image, bool flip = false);
extern bool loaddds(const char *filename, ImageData &image, int force = 0);
extern bool loadimage(const char *filename, ImageData &image);
extern MSlot &lookupmaterialslot(int slot, bool load = true);
extern Slot &lookupslot(int slot, bool load = true);
extern VSlot &lookupvslot(int slot, bool load = true);
extern VSlot *findvslot(Slot &slot, const VSlot &src, const VSlot &delta);
extern VSlot *editvslot(const VSlot &src, const VSlot &delta);
extern void mergevslot(VSlot &dst, const VSlot &src, const VSlot &delta);
extern void packvslot(vector<uchar> &buf, const VSlot &src);
extern bool unpackvslot(ucharbuf &buf, VSlot &dst, bool delta);
extern Slot dummyslot;
extern VSlot dummyvslot;
extern vector<Slot *> slots;
extern vector<VSlot *> vslots;
|