From 8e0e21f40dc8639254d7b08a0461f8cc0ac769bf Mon Sep 17 00:00:00 2001 From: bkaradzic <branimirkaradzic@gmail.com> Date: Thu, 21 Feb 2013 21:07:31 -0800 Subject: [PATCH] Normalized line endings. --- 3rdparty/edtaa3/LICENSE.md | 32 +- 3rdparty/edtaa3/edtaa3func.cpp | 1128 ++++++++--------- 3rdparty/edtaa3/edtaa3func.h | 14 +- 3rdparty/forsyth-too/LICENSE.md | 26 +- .../forsythtriangleorderoptimizer.cpp | 700 +++++----- .../forsythtriangleorderoptimizer.h | 88 +- 3rdparty/glext/gl/GRemedyGLExtensions.h | 134 +- README.md | 602 ++++----- examples/00-helloworld/helloworld.cpp | 80 +- examples/01-cubes/cubes.cpp | 30 +- examples/01-cubes/fs_cubes.sc | 26 +- examples/01-cubes/varying.def.sc | 8 +- examples/01-cubes/vs_cubes.sc | 30 +- examples/02-metaballs/fs_metaballs.sc | 32 +- examples/02-metaballs/metaballs.cpp | 10 +- examples/02-metaballs/varying.def.sc | 12 +- examples/02-metaballs/vs_metaballs.sc | 32 +- examples/03-raymarch/fs_raymarching.sc | 262 ++-- examples/03-raymarch/iq_sdf.sh | 164 +-- examples/03-raymarch/raymarch.cpp | 36 +- examples/03-raymarch/varying.def.sc | 12 +- examples/03-raymarch/vs_raymarching.sc | 32 +- examples/04-mesh/fs_mesh.sc | 108 +- examples/04-mesh/varying.def.sc | 20 +- examples/04-mesh/vs_mesh.sc | 64 +- examples/05-instancing/fs_instancing.sc | 26 +- examples/05-instancing/instancing.cpp | 30 +- examples/05-instancing/varying.def.sc | 18 +- examples/05-instancing/vs_instancing.sc | 44 +- examples/06-bump/bump.cpp | 52 +- examples/06-bump/fs_bump.sc | 158 +-- examples/06-bump/varying.def.sc | 30 +- examples/06-bump/vs_bump.sc | 86 +- examples/07-callback/fs_callback.sc | 34 +- examples/07-callback/varying.def.sc | 10 +- examples/07-callback/vs_callback.sc | 32 +- examples/08-update/fs_update.sc | 30 +- examples/08-update/update.cpp | 30 +- examples/08-update/varying.def.sc | 8 +- examples/08-update/vs_update.sc | 30 +- examples/common/aviwriter.h | 458 +++---- examples/common/common.sh | 14 +- examples/common/dbg.cpp | 218 ++-- examples/common/dbg.h | 42 +- examples/common/entry.h | 332 ++--- examples/common/entry_emscripten.cpp | 110 +- examples/common/entry_nacl.cpp | 270 ++-- examples/common/entry_windows.cpp | 10 +- examples/common/shaderlib.sh | 516 ++++---- src/bgfx_shader.sh | 322 ++--- src/common.sh | 38 +- src/config.h | 400 +++--- src/dds.h | 94 +- src/fs_clear.sc | 26 +- src/fs_debugfont.sc | 40 +- src/renderer_d3d.h | 144 +-- src/renderer_d3d11.cpp | 16 +- src/renderer_d3d9.cpp | 18 +- src/renderer_gl.cpp | 4 +- src/varying.def.sc | 20 +- src/vs_clear.sc | 30 +- src/vs_debugfont.sc | 34 +- tools/geometryc/bounds.cpp | 474 +++---- tools/geometryc/bounds.h | 94 +- tools/geometryc/math.h | 62 +- tools/geometryc/tokenizecmd.cpp | 402 +++--- tools/geometryc/tokenizecmd.h | 22 +- tools/makedisttex.cpp | 386 +++--- tools/shaderc/shaderc.cpp | 48 +- tools/texturec/texturec.cpp | 376 +++--- 70 files changed, 4660 insertions(+), 4660 deletions(-) diff --git a/3rdparty/edtaa3/LICENSE.md b/3rdparty/edtaa3/LICENSE.md index 2125ab61..1675de6b 100644 --- a/3rdparty/edtaa3/LICENSE.md +++ b/3rdparty/edtaa3/LICENSE.md @@ -1,16 +1,16 @@ -Anti-aliased Euclidean distance transform -http://webstaff.itn.liu.se/~stegu/edtaa/ - -Copyright (C) 2009 Stefan Gustavson (stefan.gustavson@gmail.com) - -This program is free software; you can redistribute it and/or modify it -under the terms of the GNU General Public License as published by the -Free Software Foundation; either version 3 of the License, or (at your -option) any later version. - -This program is distributed in the hope that it will be useful, but WITHOUT -ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -for more details. - -The GNU General Public License is available on <http://www.gnu.org/licenses/>. +Anti-aliased Euclidean distance transform +http://webstaff.itn.liu.se/~stegu/edtaa/ + +Copyright (C) 2009 Stefan Gustavson (stefan.gustavson@gmail.com) + +This program is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 3 of the License, or (at your +option) any later version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +The GNU General Public License is available on <http://www.gnu.org/licenses/>. diff --git a/3rdparty/edtaa3/edtaa3func.cpp b/3rdparty/edtaa3/edtaa3func.cpp index 4fdbb761..c9fd5df7 100644 --- a/3rdparty/edtaa3/edtaa3func.cpp +++ b/3rdparty/edtaa3/edtaa3func.cpp @@ -1,564 +1,564 @@ -/* - * edtaa3() - * - * Sweep-and-update Euclidean distance transform of an - * image. Positive pixels are treated as object pixels, - * zero or negative pixels are treated as background. - * An attempt is made to treat antialiased edges correctly. - * The input image must have pixels in the range [0,1], - * and the antialiased image should be a box-filter - * sampling of the ideal, crisp edge. - * If the antialias region is more than 1 pixel wide, - * the result from this transform will be inaccurate. - * - * By Stefan Gustavson (stefan.gustavson@gmail.com). - * - * Originally written in 1994, based on a verbal - * description of the SSED8 algorithm published in the - * PhD dissertation of Ingemar Ragnemalm. This is his - * algorithm, I only implemented it in C. - * - * Updated in 2004 to treat border pixels correctly, - * and cleaned up the code to improve readability. - * - * Updated in 2009 to handle anti-aliased edges. - * - * Updated in 2011 to avoid a corner case infinite loop. - * -*/ - -/* - Copyright (C) 2009 Stefan Gustavson (stefan.gustavson@gmail.com) - -This program is free software; you can redistribute it and/or modify it -under the terms of the GNU General Public License as published by the -Free Software Foundation; either version 3 of the License, or (at your -option) any later version. - -This program is distributed in the hope that it will be useful, but WITHOUT -ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -for more details. - -The GNU General Public License is available on <http://www.gnu.org/licenses/>. - */ - -#include <math.h> - -/* - * Compute the local gradient at edge pixels using convolution filters. - * The gradient is computed only at edge pixels. At other places in the - * image, it is never used, and it's mostly zero anyway. - */ -void computegradient(double *img, int w, int h, double *gx, double *gy) -{ - int i,j,k; - double glength; -#define SQRT2 1.4142136 - for(i = 1; i < h-1; i++) { // Avoid edges where the kernels would spill over - for(j = 1; j < w-1; j++) { - k = i*w + j; - if((img[k]>0.0) && (img[k]<1.0)) { // Compute gradient for edge pixels only - gx[k] = -img[k-w-1] - SQRT2*img[k-1] - img[k+w-1] + img[k-w+1] + SQRT2*img[k+1] + img[k+w+1]; - gy[k] = -img[k-w-1] - SQRT2*img[k-w] - img[k+w-1] + img[k-w+1] + SQRT2*img[k+w] + img[k+w+1]; - glength = gx[k]*gx[k] + gy[k]*gy[k]; - if(glength > 0.0) { // Avoid division by zero - glength = sqrt(glength); - gx[k]=gx[k]/glength; - gy[k]=gy[k]/glength; - } - } - } - } - // TODO: Compute reasonable values for gx, gy also around the image edges. - // (These are zero now, which reduces the accuracy for a 1-pixel wide region - // around the image edge.) 2x2 kernels would be suitable for this. -} - -/* - * A somewhat tricky function to approximate the distance to an edge in a - * certain pixel, with consideration to either the local gradient (gx,gy) - * or the direction to the pixel (dx,dy) and the pixel greyscale value a. - * The latter alternative, using (dx,dy), is the metric used by edtaa2(). - * Using a local estimate of the edge gradient (gx,gy) yields much better - * accuracy at and near edges, and reduces the error even at distant pixels - * provided that the gradient direction is accurately estimated. - */ -double edgedf(double gx, double gy, double a) -{ - double df, glength, temp, a1; - - if ((gx == 0) || (gy == 0)) { // Either A) gu or gv are zero, or B) both - df = 0.5-a; // Linear approximation is A) correct or B) a fair guess - } else { - glength = sqrt(gx*gx + gy*gy); - if(glength>0) { - gx = gx/glength; - gy = gy/glength; - } - /* Everything is symmetric wrt sign and transposition, - * so move to first octant (gx>=0, gy>=0, gx>=gy) to - * avoid handling all possible edge directions. - */ - gx = fabs(gx); - gy = fabs(gy); - if(gx<gy) { - temp = gx; - gx = gy; - gy = temp; - } - a1 = 0.5*gy/gx; - if (a < a1) { // 0 <= a < a1 - df = 0.5*(gx + gy) - sqrt(2.0*gx*gy*a); - } else if (a < (1.0-a1)) { // a1 <= a <= 1-a1 - df = (0.5-a)*gx; - } else { // 1-a1 < a <= 1 - df = -0.5*(gx + gy) + sqrt(2.0*gx*gy*(1.0-a)); - } - } - return df; -} - -double distaa3(double *img, double *gximg, double *gyimg, int w, int c, int xc, int yc, int xi, int yi) -{ - double di, df, dx, dy, gx, gy, a; - int closest; - - closest = c-xc-yc*w; // Index to the edge pixel pointed to from c - a = img[closest]; // Grayscale value at the edge pixel - gx = gximg[closest]; // X gradient component at the edge pixel - gy = gyimg[closest]; // Y gradient component at the edge pixel - - if(a > 1.0) a = 1.0; - if(a < 0.0) a = 0.0; // Clip grayscale values outside the range [0,1] - if(a == 0.0) return 1000000.0; // Not an object pixel, return "very far" ("don't know yet") - - dx = (double)xi; - dy = (double)yi; - di = sqrt(dx*dx + dy*dy); // Length of integer vector, like a traditional EDT - if(di==0) { // Use local gradient only at edges - // Estimate based on local gradient only - df = edgedf(gx, gy, a); - } else { - // Estimate gradient based on direction to edge (accurate for large di) - df = edgedf(dx, dy, a); - } - return di + df; // Same metric as edtaa2, except at edges (where di=0) -} - -// Shorthand macro: add ubiquitous parameters dist, gx, gy, img and w and call distaa3() -#define DISTAA(c,xc,yc,xi,yi) (distaa3(img, gx, gy, w, c, xc, yc, xi, yi)) - -void edtaa3(double *img, double *gx, double *gy, int w, int h, short *distx, short *disty, double *dist) -{ - int x, y, i, c; - int offset_u, offset_ur, offset_r, offset_rd, - offset_d, offset_dl, offset_l, offset_lu; - double olddist, newdist; - int cdistx, cdisty, newdistx, newdisty; - int changed; - double epsilon = 1e-3; - - /* Initialize index offsets for the current image width */ - offset_u = -w; - offset_ur = -w+1; - offset_r = 1; - offset_rd = w+1; - offset_d = w; - offset_dl = w-1; - offset_l = -1; - offset_lu = -w-1; - - /* Initialize the distance images */ - for(i=0; i<w*h; i++) { - distx[i] = 0; // At first, all pixels point to - disty[i] = 0; // themselves as the closest known. - if(img[i] <= 0.0) - { - dist[i]= 1000000.0; // Big value, means "not set yet" - } - else if (img[i]<1.0) { - dist[i] = edgedf(gx[i], gy[i], img[i]); // Gradient-assisted estimate - } - else { - dist[i]= 0.0; // Inside the object - } - } - - /* Perform the transformation */ - do - { - changed = 0; - - /* Scan rows, except first row */ - for(y=1; y<h; y++) - { - - /* move index to leftmost pixel of current row */ - i = y*w; - - /* scan right, propagate distances from above & left */ - - /* Leftmost pixel is special, has no left neighbors */ - olddist = dist[i]; - if(olddist > 0) // If non-zero distance or not set yet - { - c = i + offset_u; // Index of candidate for testing - cdistx = distx[c]; - cdisty = disty[c]; - newdistx = cdistx; - newdisty = cdisty+1; - newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); - if(newdist < olddist-epsilon) - { - distx[i]=newdistx; - disty[i]=newdisty; - dist[i]=newdist; - olddist=newdist; - changed = 1; - } - - c = i+offset_ur; - cdistx = distx[c]; - cdisty = disty[c]; - newdistx = cdistx-1; - newdisty = cdisty+1; - newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); - if(newdist < olddist-epsilon) - { - distx[i]=newdistx; - disty[i]=newdisty; - dist[i]=newdist; - changed = 1; - } - } - i++; - - /* Middle pixels have all neighbors */ - for(x=1; x<w-1; x++, i++) - { - olddist = dist[i]; - if(olddist <= 0) continue; // No need to update further - - c = i+offset_l; - cdistx = distx[c]; - cdisty = disty[c]; - newdistx = cdistx+1; - newdisty = cdisty; - newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); - if(newdist < olddist-epsilon) - { - distx[i]=newdistx; - disty[i]=newdisty; - dist[i]=newdist; - olddist=newdist; - changed = 1; - } - - c = i+offset_lu; - cdistx = distx[c]; - cdisty = disty[c]; - newdistx = cdistx+1; - newdisty = cdisty+1; - newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); - if(newdist < olddist-epsilon) - { - distx[i]=newdistx; - disty[i]=newdisty; - dist[i]=newdist; - olddist=newdist; - changed = 1; - } - - c = i+offset_u; - cdistx = distx[c]; - cdisty = disty[c]; - newdistx = cdistx; - newdisty = cdisty+1; - newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); - if(newdist < olddist-epsilon) - { - distx[i]=newdistx; - disty[i]=newdisty; - dist[i]=newdist; - olddist=newdist; - changed = 1; - } - - c = i+offset_ur; - cdistx = distx[c]; - cdisty = disty[c]; - newdistx = cdistx-1; - newdisty = cdisty+1; - newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); - if(newdist < olddist-epsilon) - { - distx[i]=newdistx; - disty[i]=newdisty; - dist[i]=newdist; - changed = 1; - } - } - - /* Rightmost pixel of row is special, has no right neighbors */ - olddist = dist[i]; - if(olddist > 0) // If not already zero distance - { - c = i+offset_l; - cdistx = distx[c]; - cdisty = disty[c]; - newdistx = cdistx+1; - newdisty = cdisty; - newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); - if(newdist < olddist-epsilon) - { - distx[i]=newdistx; - disty[i]=newdisty; - dist[i]=newdist; - olddist=newdist; - changed = 1; - } - - c = i+offset_lu; - cdistx = distx[c]; - cdisty = disty[c]; - newdistx = cdistx+1; - newdisty = cdisty+1; - newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); - if(newdist < olddist-epsilon) - { - distx[i]=newdistx; - disty[i]=newdisty; - dist[i]=newdist; - olddist=newdist; - changed = 1; - } - - c = i+offset_u; - cdistx = distx[c]; - cdisty = disty[c]; - newdistx = cdistx; - newdisty = cdisty+1; - newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); - if(newdist < olddist-epsilon) - { - distx[i]=newdistx; - disty[i]=newdisty; - dist[i]=newdist; - changed = 1; - } - } - - /* Move index to second rightmost pixel of current row. */ - /* Rightmost pixel is skipped, it has no right neighbor. */ - i = y*w + w-2; - - /* scan left, propagate distance from right */ - for(x=w-2; x>=0; x--, i--) - { - olddist = dist[i]; - if(olddist <= 0) continue; // Already zero distance - - c = i+offset_r; - cdistx = distx[c]; - cdisty = disty[c]; - newdistx = cdistx-1; - newdisty = cdisty; - newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); - if(newdist < olddist-epsilon) - { - distx[i]=newdistx; - disty[i]=newdisty; - dist[i]=newdist; - changed = 1; - } - } - } - - /* Scan rows in reverse order, except last row */ - for(y=h-2; y>=0; y--) - { - /* move index to rightmost pixel of current row */ - i = y*w + w-1; - - /* Scan left, propagate distances from below & right */ - - /* Rightmost pixel is special, has no right neighbors */ - olddist = dist[i]; - if(olddist > 0) // If not already zero distance - { - c = i+offset_d; - cdistx = distx[c]; - cdisty = disty[c]; - newdistx = cdistx; - newdisty = cdisty-1; - newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); - if(newdist < olddist-epsilon) - { - distx[i]=newdistx; - disty[i]=newdisty; - dist[i]=newdist; - olddist=newdist; - changed = 1; - } - - c = i+offset_dl; - cdistx = distx[c]; - cdisty = disty[c]; - newdistx = cdistx+1; - newdisty = cdisty-1; - newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); - if(newdist < olddist-epsilon) - { - distx[i]=newdistx; - disty[i]=newdisty; - dist[i]=newdist; - changed = 1; - } - } - i--; - - /* Middle pixels have all neighbors */ - for(x=w-2; x>0; x--, i--) - { - olddist = dist[i]; - if(olddist <= 0) continue; // Already zero distance - - c = i+offset_r; - cdistx = distx[c]; - cdisty = disty[c]; - newdistx = cdistx-1; - newdisty = cdisty; - newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); - if(newdist < olddist-epsilon) - { - distx[i]=newdistx; - disty[i]=newdisty; - dist[i]=newdist; - olddist=newdist; - changed = 1; - } - - c = i+offset_rd; - cdistx = distx[c]; - cdisty = disty[c]; - newdistx = cdistx-1; - newdisty = cdisty-1; - newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); - if(newdist < olddist-epsilon) - { - distx[i]=newdistx; - disty[i]=newdisty; - dist[i]=newdist; - olddist=newdist; - changed = 1; - } - - c = i+offset_d; - cdistx = distx[c]; - cdisty = disty[c]; - newdistx = cdistx; - newdisty = cdisty-1; - newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); - if(newdist < olddist-epsilon) - { - distx[i]=newdistx; - disty[i]=newdisty; - dist[i]=newdist; - olddist=newdist; - changed = 1; - } - - c = i+offset_dl; - cdistx = distx[c]; - cdisty = disty[c]; - newdistx = cdistx+1; - newdisty = cdisty-1; - newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); - if(newdist < olddist-epsilon) - { - distx[i]=newdistx; - disty[i]=newdisty; - dist[i]=newdist; - changed = 1; - } - } - /* Leftmost pixel is special, has no left neighbors */ - olddist = dist[i]; - if(olddist > 0) // If not already zero distance - { - c = i+offset_r; - cdistx = distx[c]; - cdisty = disty[c]; - newdistx = cdistx-1; - newdisty = cdisty; - newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); - if(newdist < olddist-epsilon) - { - distx[i]=newdistx; - disty[i]=newdisty; - dist[i]=newdist; - olddist=newdist; - changed = 1; - } - - c = i+offset_rd; - cdistx = distx[c]; - cdisty = disty[c]; - newdistx = cdistx-1; - newdisty = cdisty-1; - newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); - if(newdist < olddist-epsilon) - { - distx[i]=newdistx; - disty[i]=newdisty; - dist[i]=newdist; - olddist=newdist; - changed = 1; - } - - c = i+offset_d; - cdistx = distx[c]; - cdisty = disty[c]; - newdistx = cdistx; - newdisty = cdisty-1; - newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); - if(newdist < olddist-epsilon) - { - distx[i]=newdistx; - disty[i]=newdisty; - dist[i]=newdist; - changed = 1; - } - } - - /* Move index to second leftmost pixel of current row. */ - /* Leftmost pixel is skipped, it has no left neighbor. */ - i = y*w + 1; - for(x=1; x<w; x++, i++) - { - /* scan right, propagate distance from left */ - olddist = dist[i]; - if(olddist <= 0) continue; // Already zero distance - - c = i+offset_l; - cdistx = distx[c]; - cdisty = disty[c]; - newdistx = cdistx+1; - newdisty = cdisty; - newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); - if(newdist < olddist-epsilon) - { - distx[i]=newdistx; - disty[i]=newdisty; - dist[i]=newdist; - changed = 1; - } - } - } - } - while(changed); // Sweep until no more updates are made - - /* The transformation is completed. */ - -} +/* + * edtaa3() + * + * Sweep-and-update Euclidean distance transform of an + * image. Positive pixels are treated as object pixels, + * zero or negative pixels are treated as background. + * An attempt is made to treat antialiased edges correctly. + * The input image must have pixels in the range [0,1], + * and the antialiased image should be a box-filter + * sampling of the ideal, crisp edge. + * If the antialias region is more than 1 pixel wide, + * the result from this transform will be inaccurate. + * + * By Stefan Gustavson (stefan.gustavson@gmail.com). + * + * Originally written in 1994, based on a verbal + * description of the SSED8 algorithm published in the + * PhD dissertation of Ingemar Ragnemalm. This is his + * algorithm, I only implemented it in C. + * + * Updated in 2004 to treat border pixels correctly, + * and cleaned up the code to improve readability. + * + * Updated in 2009 to handle anti-aliased edges. + * + * Updated in 2011 to avoid a corner case infinite loop. + * +*/ + +/* + Copyright (C) 2009 Stefan Gustavson (stefan.gustavson@gmail.com) + +This program is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 3 of the License, or (at your +option) any later version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +The GNU General Public License is available on <http://www.gnu.org/licenses/>. + */ + +#include <math.h> + +/* + * Compute the local gradient at edge pixels using convolution filters. + * The gradient is computed only at edge pixels. At other places in the + * image, it is never used, and it's mostly zero anyway. + */ +void computegradient(double *img, int w, int h, double *gx, double *gy) +{ + int i,j,k; + double glength; +#define SQRT2 1.4142136 + for(i = 1; i < h-1; i++) { // Avoid edges where the kernels would spill over + for(j = 1; j < w-1; j++) { + k = i*w + j; + if((img[k]>0.0) && (img[k]<1.0)) { // Compute gradient for edge pixels only + gx[k] = -img[k-w-1] - SQRT2*img[k-1] - img[k+w-1] + img[k-w+1] + SQRT2*img[k+1] + img[k+w+1]; + gy[k] = -img[k-w-1] - SQRT2*img[k-w] - img[k+w-1] + img[k-w+1] + SQRT2*img[k+w] + img[k+w+1]; + glength = gx[k]*gx[k] + gy[k]*gy[k]; + if(glength > 0.0) { // Avoid division by zero + glength = sqrt(glength); + gx[k]=gx[k]/glength; + gy[k]=gy[k]/glength; + } + } + } + } + // TODO: Compute reasonable values for gx, gy also around the image edges. + // (These are zero now, which reduces the accuracy for a 1-pixel wide region + // around the image edge.) 2x2 kernels would be suitable for this. +} + +/* + * A somewhat tricky function to approximate the distance to an edge in a + * certain pixel, with consideration to either the local gradient (gx,gy) + * or the direction to the pixel (dx,dy) and the pixel greyscale value a. + * The latter alternative, using (dx,dy), is the metric used by edtaa2(). + * Using a local estimate of the edge gradient (gx,gy) yields much better + * accuracy at and near edges, and reduces the error even at distant pixels + * provided that the gradient direction is accurately estimated. + */ +double edgedf(double gx, double gy, double a) +{ + double df, glength, temp, a1; + + if ((gx == 0) || (gy == 0)) { // Either A) gu or gv are zero, or B) both + df = 0.5-a; // Linear approximation is A) correct or B) a fair guess + } else { + glength = sqrt(gx*gx + gy*gy); + if(glength>0) { + gx = gx/glength; + gy = gy/glength; + } + /* Everything is symmetric wrt sign and transposition, + * so move to first octant (gx>=0, gy>=0, gx>=gy) to + * avoid handling all possible edge directions. + */ + gx = fabs(gx); + gy = fabs(gy); + if(gx<gy) { + temp = gx; + gx = gy; + gy = temp; + } + a1 = 0.5*gy/gx; + if (a < a1) { // 0 <= a < a1 + df = 0.5*(gx + gy) - sqrt(2.0*gx*gy*a); + } else if (a < (1.0-a1)) { // a1 <= a <= 1-a1 + df = (0.5-a)*gx; + } else { // 1-a1 < a <= 1 + df = -0.5*(gx + gy) + sqrt(2.0*gx*gy*(1.0-a)); + } + } + return df; +} + +double distaa3(double *img, double *gximg, double *gyimg, int w, int c, int xc, int yc, int xi, int yi) +{ + double di, df, dx, dy, gx, gy, a; + int closest; + + closest = c-xc-yc*w; // Index to the edge pixel pointed to from c + a = img[closest]; // Grayscale value at the edge pixel + gx = gximg[closest]; // X gradient component at the edge pixel + gy = gyimg[closest]; // Y gradient component at the edge pixel + + if(a > 1.0) a = 1.0; + if(a < 0.0) a = 0.0; // Clip grayscale values outside the range [0,1] + if(a == 0.0) return 1000000.0; // Not an object pixel, return "very far" ("don't know yet") + + dx = (double)xi; + dy = (double)yi; + di = sqrt(dx*dx + dy*dy); // Length of integer vector, like a traditional EDT + if(di==0) { // Use local gradient only at edges + // Estimate based on local gradient only + df = edgedf(gx, gy, a); + } else { + // Estimate gradient based on direction to edge (accurate for large di) + df = edgedf(dx, dy, a); + } + return di + df; // Same metric as edtaa2, except at edges (where di=0) +} + +// Shorthand macro: add ubiquitous parameters dist, gx, gy, img and w and call distaa3() +#define DISTAA(c,xc,yc,xi,yi) (distaa3(img, gx, gy, w, c, xc, yc, xi, yi)) + +void edtaa3(double *img, double *gx, double *gy, int w, int h, short *distx, short *disty, double *dist) +{ + int x, y, i, c; + int offset_u, offset_ur, offset_r, offset_rd, + offset_d, offset_dl, offset_l, offset_lu; + double olddist, newdist; + int cdistx, cdisty, newdistx, newdisty; + int changed; + double epsilon = 1e-3; + + /* Initialize index offsets for the current image width */ + offset_u = -w; + offset_ur = -w+1; + offset_r = 1; + offset_rd = w+1; + offset_d = w; + offset_dl = w-1; + offset_l = -1; + offset_lu = -w-1; + + /* Initialize the distance images */ + for(i=0; i<w*h; i++) { + distx[i] = 0; // At first, all pixels point to + disty[i] = 0; // themselves as the closest known. + if(img[i] <= 0.0) + { + dist[i]= 1000000.0; // Big value, means "not set yet" + } + else if (img[i]<1.0) { + dist[i] = edgedf(gx[i], gy[i], img[i]); // Gradient-assisted estimate + } + else { + dist[i]= 0.0; // Inside the object + } + } + + /* Perform the transformation */ + do + { + changed = 0; + + /* Scan rows, except first row */ + for(y=1; y<h; y++) + { + + /* move index to leftmost pixel of current row */ + i = y*w; + + /* scan right, propagate distances from above & left */ + + /* Leftmost pixel is special, has no left neighbors */ + olddist = dist[i]; + if(olddist > 0) // If non-zero distance or not set yet + { + c = i + offset_u; // Index of candidate for testing + cdistx = distx[c]; + cdisty = disty[c]; + newdistx = cdistx; + newdisty = cdisty+1; + newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); + if(newdist < olddist-epsilon) + { + distx[i]=newdistx; + disty[i]=newdisty; + dist[i]=newdist; + olddist=newdist; + changed = 1; + } + + c = i+offset_ur; + cdistx = distx[c]; + cdisty = disty[c]; + newdistx = cdistx-1; + newdisty = cdisty+1; + newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); + if(newdist < olddist-epsilon) + { + distx[i]=newdistx; + disty[i]=newdisty; + dist[i]=newdist; + changed = 1; + } + } + i++; + + /* Middle pixels have all neighbors */ + for(x=1; x<w-1; x++, i++) + { + olddist = dist[i]; + if(olddist <= 0) continue; // No need to update further + + c = i+offset_l; + cdistx = distx[c]; + cdisty = disty[c]; + newdistx = cdistx+1; + newdisty = cdisty; + newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); + if(newdist < olddist-epsilon) + { + distx[i]=newdistx; + disty[i]=newdisty; + dist[i]=newdist; + olddist=newdist; + changed = 1; + } + + c = i+offset_lu; + cdistx = distx[c]; + cdisty = disty[c]; + newdistx = cdistx+1; + newdisty = cdisty+1; + newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); + if(newdist < olddist-epsilon) + { + distx[i]=newdistx; + disty[i]=newdisty; + dist[i]=newdist; + olddist=newdist; + changed = 1; + } + + c = i+offset_u; + cdistx = distx[c]; + cdisty = disty[c]; + newdistx = cdistx; + newdisty = cdisty+1; + newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); + if(newdist < olddist-epsilon) + { + distx[i]=newdistx; + disty[i]=newdisty; + dist[i]=newdist; + olddist=newdist; + changed = 1; + } + + c = i+offset_ur; + cdistx = distx[c]; + cdisty = disty[c]; + newdistx = cdistx-1; + newdisty = cdisty+1; + newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); + if(newdist < olddist-epsilon) + { + distx[i]=newdistx; + disty[i]=newdisty; + dist[i]=newdist; + changed = 1; + } + } + + /* Rightmost pixel of row is special, has no right neighbors */ + olddist = dist[i]; + if(olddist > 0) // If not already zero distance + { + c = i+offset_l; + cdistx = distx[c]; + cdisty = disty[c]; + newdistx = cdistx+1; + newdisty = cdisty; + newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); + if(newdist < olddist-epsilon) + { + distx[i]=newdistx; + disty[i]=newdisty; + dist[i]=newdist; + olddist=newdist; + changed = 1; + } + + c = i+offset_lu; + cdistx = distx[c]; + cdisty = disty[c]; + newdistx = cdistx+1; + newdisty = cdisty+1; + newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); + if(newdist < olddist-epsilon) + { + distx[i]=newdistx; + disty[i]=newdisty; + dist[i]=newdist; + olddist=newdist; + changed = 1; + } + + c = i+offset_u; + cdistx = distx[c]; + cdisty = disty[c]; + newdistx = cdistx; + newdisty = cdisty+1; + newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); + if(newdist < olddist-epsilon) + { + distx[i]=newdistx; + disty[i]=newdisty; + dist[i]=newdist; + changed = 1; + } + } + + /* Move index to second rightmost pixel of current row. */ + /* Rightmost pixel is skipped, it has no right neighbor. */ + i = y*w + w-2; + + /* scan left, propagate distance from right */ + for(x=w-2; x>=0; x--, i--) + { + olddist = dist[i]; + if(olddist <= 0) continue; // Already zero distance + + c = i+offset_r; + cdistx = distx[c]; + cdisty = disty[c]; + newdistx = cdistx-1; + newdisty = cdisty; + newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); + if(newdist < olddist-epsilon) + { + distx[i]=newdistx; + disty[i]=newdisty; + dist[i]=newdist; + changed = 1; + } + } + } + + /* Scan rows in reverse order, except last row */ + for(y=h-2; y>=0; y--) + { + /* move index to rightmost pixel of current row */ + i = y*w + w-1; + + /* Scan left, propagate distances from below & right */ + + /* Rightmost pixel is special, has no right neighbors */ + olddist = dist[i]; + if(olddist > 0) // If not already zero distance + { + c = i+offset_d; + cdistx = distx[c]; + cdisty = disty[c]; + newdistx = cdistx; + newdisty = cdisty-1; + newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); + if(newdist < olddist-epsilon) + { + distx[i]=newdistx; + disty[i]=newdisty; + dist[i]=newdist; + olddist=newdist; + changed = 1; + } + + c = i+offset_dl; + cdistx = distx[c]; + cdisty = disty[c]; + newdistx = cdistx+1; + newdisty = cdisty-1; + newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); + if(newdist < olddist-epsilon) + { + distx[i]=newdistx; + disty[i]=newdisty; + dist[i]=newdist; + changed = 1; + } + } + i--; + + /* Middle pixels have all neighbors */ + for(x=w-2; x>0; x--, i--) + { + olddist = dist[i]; + if(olddist <= 0) continue; // Already zero distance + + c = i+offset_r; + cdistx = distx[c]; + cdisty = disty[c]; + newdistx = cdistx-1; + newdisty = cdisty; + newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); + if(newdist < olddist-epsilon) + { + distx[i]=newdistx; + disty[i]=newdisty; + dist[i]=newdist; + olddist=newdist; + changed = 1; + } + + c = i+offset_rd; + cdistx = distx[c]; + cdisty = disty[c]; + newdistx = cdistx-1; + newdisty = cdisty-1; + newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); + if(newdist < olddist-epsilon) + { + distx[i]=newdistx; + disty[i]=newdisty; + dist[i]=newdist; + olddist=newdist; + changed = 1; + } + + c = i+offset_d; + cdistx = distx[c]; + cdisty = disty[c]; + newdistx = cdistx; + newdisty = cdisty-1; + newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); + if(newdist < olddist-epsilon) + { + distx[i]=newdistx; + disty[i]=newdisty; + dist[i]=newdist; + olddist=newdist; + changed = 1; + } + + c = i+offset_dl; + cdistx = distx[c]; + cdisty = disty[c]; + newdistx = cdistx+1; + newdisty = cdisty-1; + newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); + if(newdist < olddist-epsilon) + { + distx[i]=newdistx; + disty[i]=newdisty; + dist[i]=newdist; + changed = 1; + } + } + /* Leftmost pixel is special, has no left neighbors */ + olddist = dist[i]; + if(olddist > 0) // If not already zero distance + { + c = i+offset_r; + cdistx = distx[c]; + cdisty = disty[c]; + newdistx = cdistx-1; + newdisty = cdisty; + newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); + if(newdist < olddist-epsilon) + { + distx[i]=newdistx; + disty[i]=newdisty; + dist[i]=newdist; + olddist=newdist; + changed = 1; + } + + c = i+offset_rd; + cdistx = distx[c]; + cdisty = disty[c]; + newdistx = cdistx-1; + newdisty = cdisty-1; + newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); + if(newdist < olddist-epsilon) + { + distx[i]=newdistx; + disty[i]=newdisty; + dist[i]=newdist; + olddist=newdist; + changed = 1; + } + + c = i+offset_d; + cdistx = distx[c]; + cdisty = disty[c]; + newdistx = cdistx; + newdisty = cdisty-1; + newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); + if(newdist < olddist-epsilon) + { + distx[i]=newdistx; + disty[i]=newdisty; + dist[i]=newdist; + changed = 1; + } + } + + /* Move index to second leftmost pixel of current row. */ + /* Leftmost pixel is skipped, it has no left neighbor. */ + i = y*w + 1; + for(x=1; x<w; x++, i++) + { + /* scan right, propagate distance from left */ + olddist = dist[i]; + if(olddist <= 0) continue; // Already zero distance + + c = i+offset_l; + cdistx = distx[c]; + cdisty = disty[c]; + newdistx = cdistx+1; + newdisty = cdisty; + newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty); + if(newdist < olddist-epsilon) + { + distx[i]=newdistx; + disty[i]=newdisty; + dist[i]=newdist; + changed = 1; + } + } + } + } + while(changed); // Sweep until no more updates are made + + /* The transformation is completed. */ + +} diff --git a/3rdparty/edtaa3/edtaa3func.h b/3rdparty/edtaa3/edtaa3func.h index bcee4383..47822546 100644 --- a/3rdparty/edtaa3/edtaa3func.h +++ b/3rdparty/edtaa3/edtaa3func.h @@ -1,7 +1,7 @@ -#ifndef __EDTAA3_H__ -#define __EDTAA3_H__ - -extern void computegradient(double *img, int w, int h, double *gx, double *gy); -extern void edtaa3(double *img, double *gx, double *gy, int w, int h, short *distx, short *disty, double *dist); - -#endif // __EDTAA3_H__ +#ifndef __EDTAA3_H__ +#define __EDTAA3_H__ + +extern void computegradient(double *img, int w, int h, double *gx, double *gy); +extern void edtaa3(double *img, double *gx, double *gy, int w, int h, short *distx, short *disty, double *dist); + +#endif // __EDTAA3_H__ diff --git a/3rdparty/forsyth-too/LICENSE.md b/3rdparty/forsyth-too/LICENSE.md index 073748ae..91cc365d 100644 --- a/3rdparty/forsyth-too/LICENSE.md +++ b/3rdparty/forsyth-too/LICENSE.md @@ -1,13 +1,13 @@ -This is an implementation of Tom Forsyth's "Linear-Speed Vertex Cache -Optimization" algorithm as described here: -http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html - -This code was authored and released into the public domain by -Adrian Stone (stone@gameangst.com). - -THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT -SHALL ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER -LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR -IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +This is an implementation of Tom Forsyth's "Linear-Speed Vertex Cache +Optimization" algorithm as described here: +http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html + +This code was authored and released into the public domain by +Adrian Stone (stone@gameangst.com). + +THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +SHALL ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER +LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/3rdparty/forsyth-too/forsythtriangleorderoptimizer.cpp b/3rdparty/forsyth-too/forsythtriangleorderoptimizer.cpp index 70d5fd48..e627dbb1 100644 --- a/3rdparty/forsyth-too/forsythtriangleorderoptimizer.cpp +++ b/3rdparty/forsyth-too/forsythtriangleorderoptimizer.cpp @@ -1,350 +1,350 @@ -//----------------------------------------------------------------------------- -// This is an implementation of Tom Forsyth's "Linear-Speed Vertex Cache -// Optimization" algorithm as described here: -// http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html -// -// This code was authored and released into the public domain by -// Adrian Stone (stone@gameangst.com). -// -// THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT -// SHALL ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER -// LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR -// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -//----------------------------------------------------------------------------- - -#include <assert.h> -#include <math.h> -#include <vector> -#include <limits> -#include <algorithm> - -namespace Forsyth -{ - typedef unsigned int uint; - typedef unsigned short uint16; - typedef unsigned char byte; - - //----------------------------------------------------------------------------- - // OptimizeFaces - //----------------------------------------------------------------------------- - // Parameters: - // indexList - // input index list - // indexCount - // the number of indices in the list - // vertexCount - // the largest index value in indexList - // newIndexList - // a pointer to a preallocated buffer the same size as indexList to - // hold the optimized index list - // lruCacheSize - // the size of the simulated post-transform cache (max:64) - //----------------------------------------------------------------------------- - void OptimizeFaces(const uint16* indexList, uint indexCount, uint vertexCount, uint16* newIndexList, uint16 lruCacheSize); - - namespace - { - // code for computing vertex score was taken, as much as possible - // directly from the original publication. - float ComputeVertexCacheScore(int cachePosition, int vertexCacheSize) - { - const float FindVertexScore_CacheDecayPower = 1.5f; - const float FindVertexScore_LastTriScore = 0.75f; - - float score = 0.0f; - if ( cachePosition < 0 ) - { - // Vertex is not in FIFO cache - no score. - } - else - { - if ( cachePosition < 3 ) - { - // This vertex was used in the last triangle, - // so it has a fixed score, whichever of the three - // it's in. Otherwise, you can get very different - // answers depending on whether you add - // the triangle 1,2,3 or 3,1,2 - which is silly. - score = FindVertexScore_LastTriScore; - } - else - { - assert ( cachePosition < vertexCacheSize ); - // Points for being high in the cache. - const float scaler = 1.0f / ( vertexCacheSize - 3 ); - score = 1.0f - ( cachePosition - 3 ) * scaler; - score = powf ( score, FindVertexScore_CacheDecayPower ); - } - } - - return score; - } - - float ComputeVertexValenceScore(uint numActiveFaces) - { - const float FindVertexScore_ValenceBoostScale = 2.0f; - const float FindVertexScore_ValenceBoostPower = 0.5f; - - float score = 0.f; - - // Bonus points for having a low number of tris still to - // use the vert, so we get rid of lone verts quickly. - float valenceBoost = powf ( static_cast<float>(numActiveFaces), - -FindVertexScore_ValenceBoostPower ); - score += FindVertexScore_ValenceBoostScale * valenceBoost; - - return score; - } - - - const int kMaxVertexCacheSize = 64; - const uint kMaxPrecomputedVertexValenceScores = 64; - float s_vertexCacheScores[kMaxVertexCacheSize+1][kMaxVertexCacheSize]; - float s_vertexValenceScores[kMaxPrecomputedVertexValenceScores]; - - bool ComputeVertexScores() - { - for (int cacheSize=0; cacheSize<=kMaxVertexCacheSize; ++cacheSize) - { - for (int cachePos=0; cachePos<cacheSize; ++cachePos) - { - s_vertexCacheScores[cacheSize][cachePos] = ComputeVertexCacheScore(cachePos, cacheSize); - } - } - - for (uint valence=0; valence<kMaxPrecomputedVertexValenceScores; ++valence) - { - s_vertexValenceScores[valence] = ComputeVertexValenceScore(valence); - } - - return true; - } - bool s_vertexScoresComputed = ComputeVertexScores(); - - inline float FindVertexCacheScore(uint cachePosition, uint maxSizeVertexCache) - { - return s_vertexCacheScores[maxSizeVertexCache][cachePosition]; - } - - inline float FindVertexValenceScore(uint numActiveTris) - { - return s_vertexValenceScores[numActiveTris]; - } - - float FindVertexScore(uint numActiveFaces, uint cachePosition, uint vertexCacheSize) - { - assert(s_vertexScoresComputed); - - if ( numActiveFaces == 0 ) - { - // No tri needs this vertex! - return -1.0f; - } - - float score = 0.f; - if (cachePosition < vertexCacheSize) - { - score += s_vertexCacheScores[vertexCacheSize][cachePosition]; - } - - if (numActiveFaces < kMaxPrecomputedVertexValenceScores) - { - score += s_vertexValenceScores[numActiveFaces]; - } - else - { - score += ComputeVertexValenceScore(numActiveFaces); - } - - return score; - } - - struct OptimizeVertexData - { - float score; - uint activeFaceListStart; - uint activeFaceListSize; - uint16 cachePos0; - uint16 cachePos1; - OptimizeVertexData() : score(0.f), activeFaceListStart(0), activeFaceListSize(0), cachePos0(0), cachePos1(0) { } - }; - } - - void OptimizeFaces(const uint16* indexList, uint indexCount, uint vertexCount, uint16* newIndexList, uint16 lruCacheSize) - { - std::vector<OptimizeVertexData> vertexDataList; - vertexDataList.resize(vertexCount); - - // compute face count per vertex - for (uint i=0; i<indexCount; ++i) - { - uint16 index = indexList[i]; - assert(index < vertexCount); - OptimizeVertexData& vertexData = vertexDataList[index]; - vertexData.activeFaceListSize++; - } - - std::vector<uint> activeFaceList; - - const uint16 kEvictedCacheIndex = std::numeric_limits<uint16>::max(); - - { - // allocate face list per vertex - uint curActiveFaceListPos = 0; - for (uint i=0; i<vertexCount; ++i) - { - OptimizeVertexData& vertexData = vertexDataList[i]; - vertexData.cachePos0 = kEvictedCacheIndex; - vertexData.cachePos1 = kEvictedCacheIndex; - vertexData.activeFaceListStart = curActiveFaceListPos; - curActiveFaceListPos += vertexData.activeFaceListSize; - vertexData.score = FindVertexScore(vertexData.activeFaceListSize, vertexData.cachePos0, lruCacheSize); - vertexData.activeFaceListSize = 0; - } - activeFaceList.resize(curActiveFaceListPos); - } - - // fill out face list per vertex - for (uint i=0; i<indexCount; i+=3) - { - for (uint j=0; j<3; ++j) - { - uint16 index = indexList[i+j]; - OptimizeVertexData& vertexData = vertexDataList[index]; - activeFaceList[vertexData.activeFaceListStart + vertexData.activeFaceListSize] = i; - vertexData.activeFaceListSize++; - } - } - - std::vector<byte> processedFaceList; - processedFaceList.resize(indexCount); - - uint16 vertexCacheBuffer[(kMaxVertexCacheSize+3)*2]; - uint16* cache0 = vertexCacheBuffer; - uint16* cache1 = vertexCacheBuffer+(kMaxVertexCacheSize+3); - uint16 entriesInCache0 = 0; - - uint bestFace = 0; - float bestScore = -1.f; - - const float maxValenceScore = FindVertexScore(1, kEvictedCacheIndex, lruCacheSize) * 3.f; - - for (uint i = 0; i < indexCount; i += 3) - { - if (bestScore < 0.f) - { - // no verts in the cache are used by any unprocessed faces so - // search all unprocessed faces for a new starting point - for (uint j = 0; j < indexCount; j += 3) - { - if (processedFaceList[j] == 0) - { - uint face = j; - float faceScore = 0.f; - for (uint k=0; k<3; ++k) - { - uint16 index = indexList[face+k]; - OptimizeVertexData& vertexData = vertexDataList[index]; - assert(vertexData.activeFaceListSize > 0); - assert(vertexData.cachePos0 >= lruCacheSize); - faceScore += vertexData.score; - } - - if (faceScore > bestScore) - { - bestScore = faceScore; - bestFace = face; - - assert(bestScore <= maxValenceScore); - if (bestScore >= maxValenceScore) - { - break; - } - } - } - } - assert(bestScore >= 0.f); - } - - processedFaceList[bestFace] = 1; - uint16 entriesInCache1 = 0; - - // add bestFace to LRU cache and to newIndexList - for (uint v = 0; v < 3; ++v) - { - uint16 index = indexList[bestFace+v]; - newIndexList[i+v] = index; - - OptimizeVertexData& vertexData = vertexDataList[index]; - - if (vertexData.cachePos1 >= entriesInCache1) - { - vertexData.cachePos1 = entriesInCache1; - cache1[entriesInCache1++] = index; - - if (vertexData.activeFaceListSize == 1) - { - --vertexData.activeFaceListSize; - continue; - } - } - - assert(vertexData.activeFaceListSize > 0); - uint* begin = &activeFaceList[vertexData.activeFaceListStart]; - uint* end = &activeFaceList[vertexData.activeFaceListStart + vertexData.activeFaceListSize]; - uint* it = std::find(begin, end, bestFace); - assert(it != end); - std::swap(*it, *(end-1)); - --vertexData.activeFaceListSize; - vertexData.score = FindVertexScore(vertexData.activeFaceListSize, vertexData.cachePos1, lruCacheSize); - - } - - // move the rest of the old verts in the cache down and compute their new scores - for (uint c0 = 0; c0 < entriesInCache0; ++c0) - { - uint16 index = cache0[c0]; - OptimizeVertexData& vertexData = vertexDataList[index]; - - if (vertexData.cachePos1 >= entriesInCache1) - { - vertexData.cachePos1 = entriesInCache1; - cache1[entriesInCache1++] = index; - vertexData.score = FindVertexScore(vertexData.activeFaceListSize, vertexData.cachePos1, lruCacheSize); - } - } - - // find the best scoring triangle in the current cache (including up to 3 that were just evicted) - bestScore = -1.f; - for (uint c1 = 0; c1 < entriesInCache1; ++c1) - { - uint16 index = cache1[c1]; - OptimizeVertexData& vertexData = vertexDataList[index]; - vertexData.cachePos0 = vertexData.cachePos1; - vertexData.cachePos1 = kEvictedCacheIndex; - for (uint j=0; j<vertexData.activeFaceListSize; ++j) - { - uint face = activeFaceList[vertexData.activeFaceListStart+j]; - float faceScore = 0.f; - for (uint v=0; v<3; v++) - { - uint16 faceIndex = indexList[face+v]; - OptimizeVertexData& faceVertexData = vertexDataList[faceIndex]; - faceScore += faceVertexData.score; - } - if (faceScore > bestScore) - { - bestScore = faceScore; - bestFace = face; - } - } - } - - std::swap(cache0, cache1); - entriesInCache0 = std::min(entriesInCache1, lruCacheSize); - } - } - -} // namespace Forsyth +//----------------------------------------------------------------------------- +// This is an implementation of Tom Forsyth's "Linear-Speed Vertex Cache +// Optimization" algorithm as described here: +// http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html +// +// This code was authored and released into the public domain by +// Adrian Stone (stone@gameangst.com). +// +// THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER +// LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +//----------------------------------------------------------------------------- + +#include <assert.h> +#include <math.h> +#include <vector> +#include <limits> +#include <algorithm> + +namespace Forsyth +{ + typedef unsigned int uint; + typedef unsigned short uint16; + typedef unsigned char byte; + + //----------------------------------------------------------------------------- + // OptimizeFaces + //----------------------------------------------------------------------------- + // Parameters: + // indexList + // input index list + // indexCount + // the number of indices in the list + // vertexCount + // the largest index value in indexList + // newIndexList + // a pointer to a preallocated buffer the same size as indexList to + // hold the optimized index list + // lruCacheSize + // the size of the simulated post-transform cache (max:64) + //----------------------------------------------------------------------------- + void OptimizeFaces(const uint16* indexList, uint indexCount, uint vertexCount, uint16* newIndexList, uint16 lruCacheSize); + + namespace + { + // code for computing vertex score was taken, as much as possible + // directly from the original publication. + float ComputeVertexCacheScore(int cachePosition, int vertexCacheSize) + { + const float FindVertexScore_CacheDecayPower = 1.5f; + const float FindVertexScore_LastTriScore = 0.75f; + + float score = 0.0f; + if ( cachePosition < 0 ) + { + // Vertex is not in FIFO cache - no score. + } + else + { + if ( cachePosition < 3 ) + { + // This vertex was used in the last triangle, + // so it has a fixed score, whichever of the three + // it's in. Otherwise, you can get very different + // answers depending on whether you add + // the triangle 1,2,3 or 3,1,2 - which is silly. + score = FindVertexScore_LastTriScore; + } + else + { + assert ( cachePosition < vertexCacheSize ); + // Points for being high in the cache. + const float scaler = 1.0f / ( vertexCacheSize - 3 ); + score = 1.0f - ( cachePosition - 3 ) * scaler; + score = powf ( score, FindVertexScore_CacheDecayPower ); + } + } + + return score; + } + + float ComputeVertexValenceScore(uint numActiveFaces) + { + const float FindVertexScore_ValenceBoostScale = 2.0f; + const float FindVertexScore_ValenceBoostPower = 0.5f; + + float score = 0.f; + + // Bonus points for having a low number of tris still to + // use the vert, so we get rid of lone verts quickly. + float valenceBoost = powf ( static_cast<float>(numActiveFaces), + -FindVertexScore_ValenceBoostPower ); + score += FindVertexScore_ValenceBoostScale * valenceBoost; + + return score; + } + + + const int kMaxVertexCacheSize = 64; + const uint kMaxPrecomputedVertexValenceScores = 64; + float s_vertexCacheScores[kMaxVertexCacheSize+1][kMaxVertexCacheSize]; + float s_vertexValenceScores[kMaxPrecomputedVertexValenceScores]; + + bool ComputeVertexScores() + { + for (int cacheSize=0; cacheSize<=kMaxVertexCacheSize; ++cacheSize) + { + for (int cachePos=0; cachePos<cacheSize; ++cachePos) + { + s_vertexCacheScores[cacheSize][cachePos] = ComputeVertexCacheScore(cachePos, cacheSize); + } + } + + for (uint valence=0; valence<kMaxPrecomputedVertexValenceScores; ++valence) + { + s_vertexValenceScores[valence] = ComputeVertexValenceScore(valence); + } + + return true; + } + bool s_vertexScoresComputed = ComputeVertexScores(); + + inline float FindVertexCacheScore(uint cachePosition, uint maxSizeVertexCache) + { + return s_vertexCacheScores[maxSizeVertexCache][cachePosition]; + } + + inline float FindVertexValenceScore(uint numActiveTris) + { + return s_vertexValenceScores[numActiveTris]; + } + + float FindVertexScore(uint numActiveFaces, uint cachePosition, uint vertexCacheSize) + { + assert(s_vertexScoresComputed); + + if ( numActiveFaces == 0 ) + { + // No tri needs this vertex! + return -1.0f; + } + + float score = 0.f; + if (cachePosition < vertexCacheSize) + { + score += s_vertexCacheScores[vertexCacheSize][cachePosition]; + } + + if (numActiveFaces < kMaxPrecomputedVertexValenceScores) + { + score += s_vertexValenceScores[numActiveFaces]; + } + else + { + score += ComputeVertexValenceScore(numActiveFaces); + } + + return score; + } + + struct OptimizeVertexData + { + float score; + uint activeFaceListStart; + uint activeFaceListSize; + uint16 cachePos0; + uint16 cachePos1; + OptimizeVertexData() : score(0.f), activeFaceListStart(0), activeFaceListSize(0), cachePos0(0), cachePos1(0) { } + }; + } + + void OptimizeFaces(const uint16* indexList, uint indexCount, uint vertexCount, uint16* newIndexList, uint16 lruCacheSize) + { + std::vector<OptimizeVertexData> vertexDataList; + vertexDataList.resize(vertexCount); + + // compute face count per vertex + for (uint i=0; i<indexCount; ++i) + { + uint16 index = indexList[i]; + assert(index < vertexCount); + OptimizeVertexData& vertexData = vertexDataList[index]; + vertexData.activeFaceListSize++; + } + + std::vector<uint> activeFaceList; + + const uint16 kEvictedCacheIndex = std::numeric_limits<uint16>::max(); + + { + // allocate face list per vertex + uint curActiveFaceListPos = 0; + for (uint i=0; i<vertexCount; ++i) + { + OptimizeVertexData& vertexData = vertexDataList[i]; + vertexData.cachePos0 = kEvictedCacheIndex; + vertexData.cachePos1 = kEvictedCacheIndex; + vertexData.activeFaceListStart = curActiveFaceListPos; + curActiveFaceListPos += vertexData.activeFaceListSize; + vertexData.score = FindVertexScore(vertexData.activeFaceListSize, vertexData.cachePos0, lruCacheSize); + vertexData.activeFaceListSize = 0; + } + activeFaceList.resize(curActiveFaceListPos); + } + + // fill out face list per vertex + for (uint i=0; i<indexCount; i+=3) + { + for (uint j=0; j<3; ++j) + { + uint16 index = indexList[i+j]; + OptimizeVertexData& vertexData = vertexDataList[index]; + activeFaceList[vertexData.activeFaceListStart + vertexData.activeFaceListSize] = i; + vertexData.activeFaceListSize++; + } + } + + std::vector<byte> processedFaceList; + processedFaceList.resize(indexCount); + + uint16 vertexCacheBuffer[(kMaxVertexCacheSize+3)*2]; + uint16* cache0 = vertexCacheBuffer; + uint16* cache1 = vertexCacheBuffer+(kMaxVertexCacheSize+3); + uint16 entriesInCache0 = 0; + + uint bestFace = 0; + float bestScore = -1.f; + + const float maxValenceScore = FindVertexScore(1, kEvictedCacheIndex, lruCacheSize) * 3.f; + + for (uint i = 0; i < indexCount; i += 3) + { + if (bestScore < 0.f) + { + // no verts in the cache are used by any unprocessed faces so + // search all unprocessed faces for a new starting point + for (uint j = 0; j < indexCount; j += 3) + { + if (processedFaceList[j] == 0) + { + uint face = j; + float faceScore = 0.f; + for (uint k=0; k<3; ++k) + { + uint16 index = indexList[face+k]; + OptimizeVertexData& vertexData = vertexDataList[index]; + assert(vertexData.activeFaceListSize > 0); + assert(vertexData.cachePos0 >= lruCacheSize); + faceScore += vertexData.score; + } + + if (faceScore > bestScore) + { + bestScore = faceScore; + bestFace = face; + + assert(bestScore <= maxValenceScore); + if (bestScore >= maxValenceScore) + { + break; + } + } + } + } + assert(bestScore >= 0.f); + } + + processedFaceList[bestFace] = 1; + uint16 entriesInCache1 = 0; + + // add bestFace to LRU cache and to newIndexList + for (uint v = 0; v < 3; ++v) + { + uint16 index = indexList[bestFace+v]; + newIndexList[i+v] = index; + + OptimizeVertexData& vertexData = vertexDataList[index]; + + if (vertexData.cachePos1 >= entriesInCache1) + { + vertexData.cachePos1 = entriesInCache1; + cache1[entriesInCache1++] = index; + + if (vertexData.activeFaceListSize == 1) + { + --vertexData.activeFaceListSize; + continue; + } + } + + assert(vertexData.activeFaceListSize > 0); + uint* begin = &activeFaceList[vertexData.activeFaceListStart]; + uint* end = &activeFaceList[vertexData.activeFaceListStart + vertexData.activeFaceListSize]; + uint* it = std::find(begin, end, bestFace); + assert(it != end); + std::swap(*it, *(end-1)); + --vertexData.activeFaceListSize; + vertexData.score = FindVertexScore(vertexData.activeFaceListSize, vertexData.cachePos1, lruCacheSize); + + } + + // move the rest of the old verts in the cache down and compute their new scores + for (uint c0 = 0; c0 < entriesInCache0; ++c0) + { + uint16 index = cache0[c0]; + OptimizeVertexData& vertexData = vertexDataList[index]; + + if (vertexData.cachePos1 >= entriesInCache1) + { + vertexData.cachePos1 = entriesInCache1; + cache1[entriesInCache1++] = index; + vertexData.score = FindVertexScore(vertexData.activeFaceListSize, vertexData.cachePos1, lruCacheSize); + } + } + + // find the best scoring triangle in the current cache (including up to 3 that were just evicted) + bestScore = -1.f; + for (uint c1 = 0; c1 < entriesInCache1; ++c1) + { + uint16 index = cache1[c1]; + OptimizeVertexData& vertexData = vertexDataList[index]; + vertexData.cachePos0 = vertexData.cachePos1; + vertexData.cachePos1 = kEvictedCacheIndex; + for (uint j=0; j<vertexData.activeFaceListSize; ++j) + { + uint face = activeFaceList[vertexData.activeFaceListStart+j]; + float faceScore = 0.f; + for (uint v=0; v<3; v++) + { + uint16 faceIndex = indexList[face+v]; + OptimizeVertexData& faceVertexData = vertexDataList[faceIndex]; + faceScore += faceVertexData.score; + } + if (faceScore > bestScore) + { + bestScore = faceScore; + bestFace = face; + } + } + } + + std::swap(cache0, cache1); + entriesInCache0 = std::min(entriesInCache1, lruCacheSize); + } + } + +} // namespace Forsyth diff --git a/3rdparty/forsyth-too/forsythtriangleorderoptimizer.h b/3rdparty/forsyth-too/forsythtriangleorderoptimizer.h index a3d59885..2711f628 100644 --- a/3rdparty/forsyth-too/forsythtriangleorderoptimizer.h +++ b/3rdparty/forsyth-too/forsythtriangleorderoptimizer.h @@ -1,44 +1,44 @@ -//----------------------------------------------------------------------------- -// This is an implementation of Tom Forsyth's "Linear-Speed Vertex Cache -// Optimization" algorithm as described here: -// http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html -// -// This code was authored and released into the public domain by -// Adrian Stone (stone@gameangst.com). -// -// THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT -// SHALL ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER -// LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR -// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -//----------------------------------------------------------------------------- - -#ifndef __FORSYTH_TRIANGLE_REORDER__ -#define __FORSYTH_TRIANGLE_REORDER__ - -#include <stdint.h> - -namespace Forsyth -{ - //----------------------------------------------------------------------------- - // OptimizeFaces - //----------------------------------------------------------------------------- - // Parameters: - // indexList - // input index list - // indexCount - // the number of indices in the list - // vertexCount - // the largest index value in indexList - // newIndexList - // a pointer to a preallocated buffer the same size as indexList to - // hold the optimized index list - // lruCacheSize - // the size of the simulated post-transform cache (max:64) - //----------------------------------------------------------------------------- - void OptimizeFaces(const uint16_t* indexList, uint32_t indexCount, uint32_t vertexCount, uint16_t* newIndexList, uint16_t lruCacheSize); - -} // namespace Forsyth - -#endif // __FORSYTH_TRIANGLE_REORDER__ +//----------------------------------------------------------------------------- +// This is an implementation of Tom Forsyth's "Linear-Speed Vertex Cache +// Optimization" algorithm as described here: +// http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html +// +// This code was authored and released into the public domain by +// Adrian Stone (stone@gameangst.com). +// +// THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER +// LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +//----------------------------------------------------------------------------- + +#ifndef __FORSYTH_TRIANGLE_REORDER__ +#define __FORSYTH_TRIANGLE_REORDER__ + +#include <stdint.h> + +namespace Forsyth +{ + //----------------------------------------------------------------------------- + // OptimizeFaces + //----------------------------------------------------------------------------- + // Parameters: + // indexList + // input index list + // indexCount + // the number of indices in the list + // vertexCount + // the largest index value in indexList + // newIndexList + // a pointer to a preallocated buffer the same size as indexList to + // hold the optimized index list + // lruCacheSize + // the size of the simulated post-transform cache (max:64) + //----------------------------------------------------------------------------- + void OptimizeFaces(const uint16_t* indexList, uint32_t indexCount, uint32_t vertexCount, uint16_t* newIndexList, uint16_t lruCacheSize); + +} // namespace Forsyth + +#endif // __FORSYTH_TRIANGLE_REORDER__ diff --git a/3rdparty/glext/gl/GRemedyGLExtensions.h b/3rdparty/glext/gl/GRemedyGLExtensions.h index 919ac258..9777b0c8 100644 --- a/3rdparty/glext/gl/GRemedyGLExtensions.h +++ b/3rdparty/glext/gl/GRemedyGLExtensions.h @@ -1,67 +1,67 @@ -// ------------------------------ GRemdeyGLExtensions.h ------------------------------ - -// ----------------------------------------------------------------- -// � 2004 - 2012 Advanced Micro Devices, Inc. All rights reserved. -// ----------------------------------------------------------------- - -#ifndef __GREMDEYGLEXTENSIONS -#define __GREMDEYGLEXTENSIONS - - -#if defined(_WIN32) && !defined(APIENTRY) && !defined(__CYGWIN__) && !defined(__SCITECH_SNAP__) -#define WIN32_LEAN_AND_MEAN 1 -#include <windows.h> -#endif - -#ifndef APIENTRY -#define APIENTRY -#endif - -#ifndef APIENTRYP -#define APIENTRYP APIENTRY * -#endif - -#ifndef GLAPI -#define GLAPI extern -#endif - -#ifdef __cplusplus -extern "C" -{ -#endif - - /* ----------------------- GL_GREMEDY_string_marker ----------------------- */ - -#ifndef GL_GREMEDY_string_marker -#define GL_GREMEDY_string_marker 1 - -#ifdef GL_GLEXT_PROTOTYPES - GLAPI void APIENTRY glStringMarkerGREMEDY(GLsizei len, const GLvoid *string); -#endif /* GL_GLEXT_PROTOTYPES */ - - typedef void (APIENTRYP PFNGLSTRINGMARKERGREMEDYPROC)(GLsizei len, const GLvoid *string); - -#endif /* GL_GREMEDY_string_marker */ - - - - /* ----------------------- GL_GREMEDY_frame_terminator ----------------------- */ - -#ifndef GL_GREMEDY_frame_terminator -#define GL_GREMEDY_frame_terminator 1 - -#ifdef GL_GLEXT_PROTOTYPES - GLAPI void APIENTRY glFrameTerminatorGREMEDY(void); -#endif /* GL_GLEXT_PROTOTYPES */ - - typedef void (APIENTRYP PFNGLFRAMETERMINATORGREMEDYPROC)(void); - -#endif /* GL_GREMEDY_frame_terminator */ - - -#ifdef __cplusplus -} -#endif - - -#endif /* __GREMDEYGLEXTENSIONS */ +// ------------------------------ GRemdeyGLExtensions.h ------------------------------ + +// ----------------------------------------------------------------- +// � 2004 - 2012 Advanced Micro Devices, Inc. All rights reserved. +// ----------------------------------------------------------------- + +#ifndef __GREMDEYGLEXTENSIONS +#define __GREMDEYGLEXTENSIONS + + +#if defined(_WIN32) && !defined(APIENTRY) && !defined(__CYGWIN__) && !defined(__SCITECH_SNAP__) +#define WIN32_LEAN_AND_MEAN 1 +#include <windows.h> +#endif + +#ifndef APIENTRY +#define APIENTRY +#endif + +#ifndef APIENTRYP +#define APIENTRYP APIENTRY * +#endif + +#ifndef GLAPI +#define GLAPI extern +#endif + +#ifdef __cplusplus +extern "C" +{ +#endif + + /* ----------------------- GL_GREMEDY_string_marker ----------------------- */ + +#ifndef GL_GREMEDY_string_marker +#define GL_GREMEDY_string_marker 1 + +#ifdef GL_GLEXT_PROTOTYPES + GLAPI void APIENTRY glStringMarkerGREMEDY(GLsizei len, const GLvoid *string); +#endif /* GL_GLEXT_PROTOTYPES */ + + typedef void (APIENTRYP PFNGLSTRINGMARKERGREMEDYPROC)(GLsizei len, const GLvoid *string); + +#endif /* GL_GREMEDY_string_marker */ + + + + /* ----------------------- GL_GREMEDY_frame_terminator ----------------------- */ + +#ifndef GL_GREMEDY_frame_terminator +#define GL_GREMEDY_frame_terminator 1 + +#ifdef GL_GLEXT_PROTOTYPES + GLAPI void APIENTRY glFrameTerminatorGREMEDY(void); +#endif /* GL_GLEXT_PROTOTYPES */ + + typedef void (APIENTRYP PFNGLFRAMETERMINATORGREMEDYPROC)(void); + +#endif /* GL_GREMEDY_frame_terminator */ + + +#ifdef __cplusplus +} +#endif + + +#endif /* __GREMDEYGLEXTENSIONS */ diff --git a/README.md b/README.md index 8fe7c329..74f7ff40 100644 --- a/README.md +++ b/README.md @@ -1,301 +1,301 @@ -bgfx -==== - -What is it? ------------ - -Cross-platform rendering library. - -Supported rendering backends: - - * OpenGL 2.1 - * OpenGL ES 2 - * OpenGL ES 3 - * Direct3D 9 - * Direct3D 11 - -Platforms: - - * Windows - * Linux - * Android - * Native Client - * JavaScript (via Emscripten) - * OSX - -Dependencies ------------- - -[https://github.com/bkaradzic/bx](https://github.com/bkaradzic/bx) - -Optional: -[https://github.com/mendsley/tinystl](https://github.com/mendsley/tinystl) - -Building --------- - -### Prerequisites - -Premake 4.4 beta4 -[http://industriousone.com/premake/download](http://industriousone.com/premake/download) - -GNU make -Windows users download GNU make utility from: -[http://gnuwin32.sourceforge.net/packages/make.htm](http://gnuwin32.sourceforge.net/packages/make.htm) - -### Getting source - - git clone git://github.com/bkaradzic/bx.git - git clone git://github.com/bkaradzic/bgfx.git - cd bgfx - make - -After calling make, .build/projects/* directory will be generated. All -intermediate files generated by compiler will be inside .build directory -structure. Deleting .build directory at any time is safe. - -### Prerequisites for Linux - - sudo apt-get install libgl1-mesa-dev - -### Prerequisites for Windows - -When building on Windows, you have to set DXSDK_DIR environment variable to -point to DirectX SDK directory. - - setx DXSDK_DIR <path to DirectX SDK directory> - -If you're building with Visual Studio 2008, you'll need TR1 support from: -[Visual C++ 2008 Feature Pack Release](https://www.microsoft.com/en-us/download/details.aspx?id=6922) - -If you're building with MinGW/TDM compiler on Windows make DirectX SDK -directory link to directory without spaces in the path. - - mklink /D <path to DirectX SDK directory> c:\dxsdk - setx DXSDK_DIR c:\dxsdk - -### Prerequisites for Native Client (Pepper 22) on Windows - -Download Native Client SDK from: -[https://developers.google.com/native-client/sdk/download](https://developers.google.com/native-client/sdk/download) - - setx NACL <path to Native Client SDK directory>\toolchain\win_x86_newlib - -### Building - -Visual Studio 2008 command line: - - make vs2008-release64 - -Visual Studio 2008 IDE: - - start .build/projects/vs2008/bgfx.sln - -Linux 64-bit: - - make linux-release64 - -Other platforms: - - make <configuration> - -Configuration is `<platform>-<debug/release><32/64>`. For example: - - linux-release32, nacl-debug64, android-release32, etc. - -Examples --------- - -### 00-helloworld -Initialization and debug text. - -### 01-cubes -Rendering simple static mesh. - - - -### 02-metaballs -Rendering with transient buffers. - - - -### 03-raymarch -Updating shader uniforms. - - - -### 04-mesh -Loading meshes. - - - -### 05-instancing -Geometry instancing. - - - -### 06-bump -Loading textures. - - - -### 07-callback -Implementing application specific callbacks for taking screen shots, caching -OpenGL binary shaders, and video capture. - -### 08-update -Updating textures. - -Internals ---------- - -bgfx is using sort-based draw call bucketing. This means that submition order -doesn't necessarily matches the rendering order, but on the low-level they -will be sorted and ordered correctly. On the high level this allows -more optimal way of submitting draw calls for all passes at one place, and on -the low-level this allows better optimization of rendering order. This sometimes -creates undesired results usually for GUI rendering, where draw order should -usually match submit order. bgfx provides way to enable sequential rendering for -these cases (see `bgfx::setViewSeq`). - -Internally all low-level rendering draw calls are issued inside single function -`Context::rendererSubmit`. This function exist inside each renderer backend -implementation. - -More detailed description of sort-based draw call bucketing can be found at: -[Order your graphics draw calls around!](http://realtimecollisiondetection.net/blog/?p=86) - -Customization -------------- - -By default each platform has sane default values. For example on Windows default -renderer is DirectX9, and on Linux it is OpenGL 2.1. On Windows platform all -rendering backends are available. For OpenGL ES on desktop you can find more -information at:- -[OpenGL ES 2.0 and EGL on desktop](http://www.g-truc.net/post-0457.html) - -If you're targeting specific mobile hardware, you can find GLES support in their -official SDKs: -[Adreno SDK](http://developer.qualcomm.com/mobile-development/mobile-technologies/gaming-graphics-optimization-adreno/tools-and-resources), -[Mali SDK](http://www.malideveloper.com/), -[PowerVR SDK](http://www.imgtec.com/powervr/insider/sdkdownloads/). - -All configuration settings are located inside [src/config.h](https://github.com/bkaradzic/bgfx/blob/master/src/config.h). - -Every `BGFX_CONFIG_*` setting can be changed by passing defines thru compiler -switches. For example setting preprocessor define `BGFX_CONFIG_RENDERER_OPENGL=1` -on Windows will change backend renderer to OpenGL 2.1. on Windows. Since -rendering APIs are platform specific, this obviously won't work nor make sense -in all cases. Certain platforms have only single choice, for example the Native -Client works only with OpenGL ES 2.0 renderer, using anything other than that -will result in build errors. - -Tools ------ - -### Shader Compiler (shaderc) - -bgfx cross-platform shader language is based on GLSL syntax. It's uses ANSI C -preprocessor to transform GLSL like language syntax into HLSL. This technique -has certain drawbacks, but overall it's simple and allows quick authoring of -cross-platform shaders. - -### Texture Compiler (texturec) - -This tool doesn't currently exist. Use nvdxt, or any other tool that produces -DDS textures for now. - -### Geometry Compiler (geometryc) - -Converts Wavefront .obj mesh file to format optimal for using with bgfx. - -Todo ----- - - - Multiple render targets. - - BlendFuncSeparate and BlendEquationSeparate. - - Blit between textures. - - Occlusion queries. - - iOS platforms. - - DX11: MSAA. - - GL: MSAA. - - Fullscreen mode. - -Notice ------- - -This is alpha software, and it lacks documentation and examples. If you're -interested to use it in your project, please let me know. - -Contact -------- - -[@bkaradzic](https://twitter.com/bkaradzic) -http://www.stuckingeometry.com - -Project page -https://github.com/bkaradzic/bgfx - -3rd Party Libraries -------------------- - -All required 3rd party libraries are included in bgfx repository in [3rdparty/](https://github.com/bkaradzic/bgfx/tree/master/3rdparty) -directory. - -### edtaa3 (MIT) - -Contour Rendering by Distance Fields - -https://github.com/OpenGLInsights/OpenGLInsightsCode/tree/master/Chapter%2012%202D%20Shape%20Rendering%20by%20Distance%20Fields - -### fcpp (BSD) - -Frexx C preprocessor - -https://github.com/bagder/fcpp - -### Forsyth Triangle Order Optimizer (Public Domain) - -http://gameangst.com/?p=9 - -### glsl-optimizer (MIT) - -GLSL optimizer based on Mesa's GLSL compiler. Used in Unity for mobile shader -optimization. - -https://github.com/aras-p/glsl-optimizer - -### stb_image (Public Domain) - -http://nothings.org/stb_image.c - -Contributors ------------- - -Garett Bass ([@gtbass](https://github.com/gtbass)) - OSX port. - -License (BSD 2-clause) ----------------------- - -Copyright 2010-2013 Branimir Karadzic. All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS OR -IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO -EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, -INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, -BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE -OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +bgfx +==== + +What is it? +----------- + +Cross-platform rendering library. + +Supported rendering backends: + + * OpenGL 2.1 + * OpenGL ES 2 + * OpenGL ES 3 + * Direct3D 9 + * Direct3D 11 + +Platforms: + + * Windows + * Linux + * Android + * Native Client + * JavaScript (via Emscripten) + * OSX + +Dependencies +------------ + +[https://github.com/bkaradzic/bx](https://github.com/bkaradzic/bx) + +Optional: +[https://github.com/mendsley/tinystl](https://github.com/mendsley/tinystl) + +Building +-------- + +### Prerequisites + +Premake 4.4 beta4 +[http://industriousone.com/premake/download](http://industriousone.com/premake/download) + +GNU make +Windows users download GNU make utility from: +[http://gnuwin32.sourceforge.net/packages/make.htm](http://gnuwin32.sourceforge.net/packages/make.htm) + +### Getting source + + git clone git://github.com/bkaradzic/bx.git + git clone git://github.com/bkaradzic/bgfx.git + cd bgfx + make + +After calling make, .build/projects/* directory will be generated. All +intermediate files generated by compiler will be inside .build directory +structure. Deleting .build directory at any time is safe. + +### Prerequisites for Linux + + sudo apt-get install libgl1-mesa-dev + +### Prerequisites for Windows + +When building on Windows, you have to set DXSDK_DIR environment variable to +point to DirectX SDK directory. + + setx DXSDK_DIR <path to DirectX SDK directory> + +If you're building with Visual Studio 2008, you'll need TR1 support from: +[Visual C++ 2008 Feature Pack Release](https://www.microsoft.com/en-us/download/details.aspx?id=6922) + +If you're building with MinGW/TDM compiler on Windows make DirectX SDK +directory link to directory without spaces in the path. + + mklink /D <path to DirectX SDK directory> c:\dxsdk + setx DXSDK_DIR c:\dxsdk + +### Prerequisites for Native Client (Pepper 22) on Windows + +Download Native Client SDK from: +[https://developers.google.com/native-client/sdk/download](https://developers.google.com/native-client/sdk/download) + + setx NACL <path to Native Client SDK directory>\toolchain\win_x86_newlib + +### Building + +Visual Studio 2008 command line: + + make vs2008-release64 + +Visual Studio 2008 IDE: + + start .build/projects/vs2008/bgfx.sln + +Linux 64-bit: + + make linux-release64 + +Other platforms: + + make <configuration> + +Configuration is `<platform>-<debug/release><32/64>`. For example: + + linux-release32, nacl-debug64, android-release32, etc. + +Examples +-------- + +### 00-helloworld +Initialization and debug text. + +### 01-cubes +Rendering simple static mesh. + + + +### 02-metaballs +Rendering with transient buffers. + + + +### 03-raymarch +Updating shader uniforms. + + + +### 04-mesh +Loading meshes. + + + +### 05-instancing +Geometry instancing. + + + +### 06-bump +Loading textures. + + + +### 07-callback +Implementing application specific callbacks for taking screen shots, caching +OpenGL binary shaders, and video capture. + +### 08-update +Updating textures. + +Internals +--------- + +bgfx is using sort-based draw call bucketing. This means that submition order +doesn't necessarily matches the rendering order, but on the low-level they +will be sorted and ordered correctly. On the high level this allows +more optimal way of submitting draw calls for all passes at one place, and on +the low-level this allows better optimization of rendering order. This sometimes +creates undesired results usually for GUI rendering, where draw order should +usually match submit order. bgfx provides way to enable sequential rendering for +these cases (see `bgfx::setViewSeq`). + +Internally all low-level rendering draw calls are issued inside single function +`Context::rendererSubmit`. This function exist inside each renderer backend +implementation. + +More detailed description of sort-based draw call bucketing can be found at: +[Order your graphics draw calls around!](http://realtimecollisiondetection.net/blog/?p=86) + +Customization +------------- + +By default each platform has sane default values. For example on Windows default +renderer is DirectX9, and on Linux it is OpenGL 2.1. On Windows platform all +rendering backends are available. For OpenGL ES on desktop you can find more +information at:- +[OpenGL ES 2.0 and EGL on desktop](http://www.g-truc.net/post-0457.html) + +If you're targeting specific mobile hardware, you can find GLES support in their +official SDKs: +[Adreno SDK](http://developer.qualcomm.com/mobile-development/mobile-technologies/gaming-graphics-optimization-adreno/tools-and-resources), +[Mali SDK](http://www.malideveloper.com/), +[PowerVR SDK](http://www.imgtec.com/powervr/insider/sdkdownloads/). + +All configuration settings are located inside [src/config.h](https://github.com/bkaradzic/bgfx/blob/master/src/config.h). + +Every `BGFX_CONFIG_*` setting can be changed by passing defines thru compiler +switches. For example setting preprocessor define `BGFX_CONFIG_RENDERER_OPENGL=1` +on Windows will change backend renderer to OpenGL 2.1. on Windows. Since +rendering APIs are platform specific, this obviously won't work nor make sense +in all cases. Certain platforms have only single choice, for example the Native +Client works only with OpenGL ES 2.0 renderer, using anything other than that +will result in build errors. + +Tools +----- + +### Shader Compiler (shaderc) + +bgfx cross-platform shader language is based on GLSL syntax. It's uses ANSI C +preprocessor to transform GLSL like language syntax into HLSL. This technique +has certain drawbacks, but overall it's simple and allows quick authoring of +cross-platform shaders. + +### Texture Compiler (texturec) + +This tool doesn't currently exist. Use nvdxt, or any other tool that produces +DDS textures for now. + +### Geometry Compiler (geometryc) + +Converts Wavefront .obj mesh file to format optimal for using with bgfx. + +Todo +---- + + - Multiple render targets. + - BlendFuncSeparate and BlendEquationSeparate. + - Blit between textures. + - Occlusion queries. + - iOS platforms. + - DX11: MSAA. + - GL: MSAA. + - Fullscreen mode. + +Notice +------ + +This is alpha software, and it lacks documentation and examples. If you're +interested to use it in your project, please let me know. + +Contact +------- + +[@bkaradzic](https://twitter.com/bkaradzic) +http://www.stuckingeometry.com + +Project page +https://github.com/bkaradzic/bgfx + +3rd Party Libraries +------------------- + +All required 3rd party libraries are included in bgfx repository in [3rdparty/](https://github.com/bkaradzic/bgfx/tree/master/3rdparty) +directory. + +### edtaa3 (MIT) + +Contour Rendering by Distance Fields + +https://github.com/OpenGLInsights/OpenGLInsightsCode/tree/master/Chapter%2012%202D%20Shape%20Rendering%20by%20Distance%20Fields + +### fcpp (BSD) + +Frexx C preprocessor + +https://github.com/bagder/fcpp + +### Forsyth Triangle Order Optimizer (Public Domain) + +http://gameangst.com/?p=9 + +### glsl-optimizer (MIT) + +GLSL optimizer based on Mesa's GLSL compiler. Used in Unity for mobile shader +optimization. + +https://github.com/aras-p/glsl-optimizer + +### stb_image (Public Domain) + +http://nothings.org/stb_image.c + +Contributors +------------ + +Garett Bass ([@gtbass](https://github.com/gtbass)) - OSX port. + +License (BSD 2-clause) +---------------------- + +Copyright 2010-2013 Branimir Karadzic. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/examples/00-helloworld/helloworld.cpp b/examples/00-helloworld/helloworld.cpp index 6927f61f..30d5f3d6 100644 --- a/examples/00-helloworld/helloworld.cpp +++ b/examples/00-helloworld/helloworld.cpp @@ -1,16 +1,16 @@ -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include <bgfx.h> -#include <bx/bx.h> +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include <bgfx.h> +#include <bx/bx.h> #include "../common/entry.h" -#include "../common/dbg.h" +#include "../common/dbg.h" #include "../common/processevents.h" - -int _main_(int _argc, char** _argv) -{ + +int _main_(int _argc, char** _argv) +{ uint32_t width = 1280; uint32_t height = 720; uint32_t debug = BGFX_DEBUG_TEXT; @@ -20,36 +20,36 @@ int _main_(int _argc, char** _argv) // Enable debug text. bgfx::setDebug(debug); - - // Set view 0 clear state. - bgfx::setViewClear(0 - , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT - , 0x303030ff - , 1.0f - , 0 - ); - + + // Set view 0 clear state. + bgfx::setViewClear(0 + , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT + , 0x303030ff + , 1.0f + , 0 + ); + while (!processEvents(width, height, debug) ) { // Set view 0 default viewport. bgfx::setViewRect(0, 0, 0, width, height); - - // This dummy draw call is here to make sure that view 0 is cleared - // if no other draw calls are submitted to view 0. - bgfx::submit(0); - - // Use debug font to print information about this example. - bgfx::dbgTextClear(); - bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/00-helloworld"); - bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Initialization and debug text."); - - // Advance to next frame. Rendering thread will be kicked to - // process submitted rendering primitives. - bgfx::frame(); - } - - // Shutdown bgfx. - bgfx::shutdown(); - - return 0; -} + + // This dummy draw call is here to make sure that view 0 is cleared + // if no other draw calls are submitted to view 0. + bgfx::submit(0); + + // Use debug font to print information about this example. + bgfx::dbgTextClear(); + bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/00-helloworld"); + bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Initialization and debug text."); + + // Advance to next frame. Rendering thread will be kicked to + // process submitted rendering primitives. + bgfx::frame(); + } + + // Shutdown bgfx. + bgfx::shutdown(); + + return 0; +} diff --git a/examples/01-cubes/cubes.cpp b/examples/01-cubes/cubes.cpp index e8d1d305..2e061175 100644 --- a/examples/01-cubes/cubes.cpp +++ b/examples/01-cubes/cubes.cpp @@ -96,16 +96,16 @@ static const bgfx::Memory* loadShader(const char* _name) int _main_(int _argc, char** _argv) { - uint32_t width = 1280; - uint32_t height = 720; - uint32_t debug = BGFX_DEBUG_TEXT; - - bgfx::init(); - bgfx::reset(width, height); - - // Enable debug text. - bgfx::setDebug(debug); - + uint32_t width = 1280; + uint32_t height = 720; + uint32_t debug = BGFX_DEBUG_TEXT; + + bgfx::init(); + bgfx::reset(width, height); + + // Enable debug text. + bgfx::setDebug(debug); + // Set view 0 clear state. bgfx::setViewClear(0 , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT @@ -171,11 +171,11 @@ int _main_(int _argc, char** _argv) bgfx::destroyVertexShader(vsh); bgfx::destroyFragmentShader(fsh); - while (!processEvents(width, height, debug) ) - { - // Set view 0 default viewport. - bgfx::setViewRect(0, 0, 0, width, height); - + while (!processEvents(width, height, debug) ) + { + // Set view 0 default viewport. + bgfx::setViewRect(0, 0, 0, width, height); + // This dummy draw call is here to make sure that view 0 is cleared // if no other draw calls are submitted to view 0. bgfx::submit(0); diff --git a/examples/01-cubes/fs_cubes.sc b/examples/01-cubes/fs_cubes.sc index a6318ee4..afc160fa 100644 --- a/examples/01-cubes/fs_cubes.sc +++ b/examples/01-cubes/fs_cubes.sc @@ -1,13 +1,13 @@ -$input v_color0 - -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include "../common/common.sh" - -void main() -{ - gl_FragColor = v_color0; -} +$input v_color0 + +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "../common/common.sh" + +void main() +{ + gl_FragColor = v_color0; +} diff --git a/examples/01-cubes/varying.def.sc b/examples/01-cubes/varying.def.sc index 09ee1273..9d9d83e5 100644 --- a/examples/01-cubes/varying.def.sc +++ b/examples/01-cubes/varying.def.sc @@ -1,4 +1,4 @@ -vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); - -vec3 a_position : POSITION; -vec4 a_color0 : COLOR0; +vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); + +vec3 a_position : POSITION; +vec4 a_color0 : COLOR0; diff --git a/examples/01-cubes/vs_cubes.sc b/examples/01-cubes/vs_cubes.sc index 357a9bc7..6bfaaa9a 100644 --- a/examples/01-cubes/vs_cubes.sc +++ b/examples/01-cubes/vs_cubes.sc @@ -1,15 +1,15 @@ -$input a_position, a_color0 -$output v_color0 - -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include "../common/common.sh" - -void main() -{ - gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) ); - v_color0 = a_color0; -} +$input a_position, a_color0 +$output v_color0 + +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "../common/common.sh" + +void main() +{ + gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) ); + v_color0 = a_color0; +} diff --git a/examples/02-metaballs/fs_metaballs.sc b/examples/02-metaballs/fs_metaballs.sc index fdb5b207..e6fa98df 100644 --- a/examples/02-metaballs/fs_metaballs.sc +++ b/examples/02-metaballs/fs_metaballs.sc @@ -1,16 +1,16 @@ -$input v_normal, v_color0 - -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include "../common/common.sh" - -void main() -{ - vec3 lightDir = vec3(0.0, 0.0, -1.0); - float ndotl = max(dot(normalize(v_normal), lightDir), 0.0); - float spec = pow(ndotl, 30.0); - gl_FragColor = pow(pow(v_color0, vec4_splat(2.2) ) * ndotl + spec, vec4_splat(1.0/2.2) ); -} +$input v_normal, v_color0 + +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "../common/common.sh" + +void main() +{ + vec3 lightDir = vec3(0.0, 0.0, -1.0); + float ndotl = max(dot(normalize(v_normal), lightDir), 0.0); + float spec = pow(ndotl, 30.0); + gl_FragColor = pow(pow(v_color0, vec4_splat(2.2) ) * ndotl + spec, vec4_splat(1.0/2.2) ); +} diff --git a/examples/02-metaballs/metaballs.cpp b/examples/02-metaballs/metaballs.cpp index 0afb46cb..c3f73116 100644 --- a/examples/02-metaballs/metaballs.cpp +++ b/examples/02-metaballs/metaballs.cpp @@ -571,11 +571,11 @@ int _main_(int _argc, char** _argv) const uint32_t zpitch = DIMS*DIMS; const float invdim = 1.0f/float(DIMS-1); - while (!processEvents(width, height, debug) ) - { - // Set view 0 default viewport. - bgfx::setViewRect(0, 0, 0, width, height); - + while (!processEvents(width, height, debug) ) + { + // Set view 0 default viewport. + bgfx::setViewRect(0, 0, 0, width, height); + // This dummy draw call is here to make sure that view 0 is cleared // if no other draw calls are submitted to view 0. bgfx::submit(0); diff --git a/examples/02-metaballs/varying.def.sc b/examples/02-metaballs/varying.def.sc index 6e25c36a..4a91daaf 100644 --- a/examples/02-metaballs/varying.def.sc +++ b/examples/02-metaballs/varying.def.sc @@ -1,6 +1,6 @@ -vec3 v_normal : TEXCOORD1 = vec3(0.0, 0.0, 1.0); -vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); - -vec3 a_position : POSITION; -vec3 a_normal : NORMAL; -vec4 a_color0 : COLOR0; +vec3 v_normal : TEXCOORD1 = vec3(0.0, 0.0, 1.0); +vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); + +vec3 a_position : POSITION; +vec3 a_normal : NORMAL; +vec4 a_color0 : COLOR0; diff --git a/examples/02-metaballs/vs_metaballs.sc b/examples/02-metaballs/vs_metaballs.sc index 078f7d05..22781d27 100644 --- a/examples/02-metaballs/vs_metaballs.sc +++ b/examples/02-metaballs/vs_metaballs.sc @@ -1,16 +1,16 @@ -$input a_position, a_normal, a_color0 -$output v_normal, v_color0 - -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include "../common/common.sh" - -void main() -{ - gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) ); - v_normal = mul(u_model, vec4(a_normal, 0.0) ).xyz; - v_color0 = a_color0; -} +$input a_position, a_normal, a_color0 +$output v_normal, v_color0 + +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "../common/common.sh" + +void main() +{ + gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) ); + v_normal = mul(u_model, vec4(a_normal, 0.0) ).xyz; + v_color0 = a_color0; +} diff --git a/examples/03-raymarch/fs_raymarching.sc b/examples/03-raymarch/fs_raymarching.sc index 6efcbda9..f2f05660 100644 --- a/examples/03-raymarch/fs_raymarching.sc +++ b/examples/03-raymarch/fs_raymarching.sc @@ -1,131 +1,131 @@ -$input v_color0, v_texcoord0 - -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -// References: -// Sphere tracing: a geometric method for the antialiased ray tracing of implicit surfaces - John C. Hart -// http://web.archive.org/web/20110331200546/http://graphics.cs.uiuc.edu/~jch/papers/zeno.pdf -// -// Modeling with distance functions -// http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm - -#include "../common/common.sh" -#include "iq_sdf.sh" - -uniform float u_time; -uniform mat4 u_mtx; -uniform vec3 u_lightDir; - -float sceneDist(vec3 _pos) -{ - float d1 = udRoundBox(_pos, vec3(2.5, 2.5, 2.5), 0.5); - float d2 = sdSphere(_pos + vec3( 4.0, 0.0, 0.0), 1.0); - float d3 = sdSphere(_pos + vec3(-4.0, 0.0, 0.0), 1.0); - float d4 = sdSphere(_pos + vec3( 0.0, 4.0, 0.0), 1.0); - float d5 = sdSphere(_pos + vec3( 0.0,-4.0, 0.0), 1.0); - float d6 = sdSphere(_pos + vec3( 0.0, 0.0, 4.0), 1.0); - float d7 = sdSphere(_pos + vec3( 0.0, 0.0,-4.0), 1.0); - float dist = min(min(min(min(min(min(d1, d2), d3), d4), d5), d6), d7); - return dist; -} - -vec3 calcNormal(vec3 _pos) -{ - const vec2 delta = vec2(0.002, 0.0); - float nx = sceneDist(_pos + delta.xyy) - sceneDist(_pos - delta.xyy); - float ny = sceneDist(_pos + delta.yxy) - sceneDist(_pos - delta.yxy); - float nz = sceneDist(_pos + delta.yyx) - sceneDist(_pos - delta.yyx); - return normalize(vec3(nx, ny, nz) ); -} - -float calcAmbOcc(vec3 _pos, vec3 _normal) -{ - float occ = 0.0; - float aostep = 0.2; - for (int ii = 1; ii < 4; ii++) - { - float fi = float(ii); - float dist = sceneDist(_pos + _normal * fi * aostep); - occ += (fi * aostep - dist) / pow(2.0, fi); - } - - return 1.0 - occ; -} - -float trace(vec3 _ray, vec3 _dir, float _maxd) -{ - float tt = 0.0; - float epsilon = 0.001; - - for (int ii = 0; ii < 64; ii++) - { - float dist = sceneDist(_ray + _dir*tt); - if (dist > epsilon) - { - tt += dist; - } - } - - return tt < _maxd ? tt : 0.0; -} - -vec2 blinn(vec3 _lightDir, vec3 _normal, vec3 _viewDir) -{ - float ndotl = dot(_normal, _lightDir); - vec3 reflected = _lightDir - 2.0*ndotl*_normal; // reflect(_lightDir, _normal); - float rdotv = dot(reflected, _viewDir); - return vec2(ndotl, rdotv); -} - -float fresnel(float _ndotl, float _bias, float _pow) -{ - float facing = (1.0 - _ndotl); - return max(_bias + (1.0 - _bias) * pow(facing, _pow), 0.0); -} - -vec4 lit(float _ndotl, float _rdotv, float _m) -{ - float diff = max(0.0, _ndotl); - float spec = step(0.0, _ndotl) * max(0.0, _rdotv * _m); - return vec4(1.0, diff, spec, 1.0); -} - -void main() -{ - vec4 tmp; - tmp = mul(u_mtx, vec4(v_texcoord0.x, v_texcoord0.y, 0.0, 1.0) ); - vec3 eye = tmp.xyz/tmp.w; - - tmp = mul(u_mtx, vec4(v_texcoord0.x, v_texcoord0.y, 1.0, 1.0) ); - vec3 at = tmp.xyz/tmp.w; - - float maxd = length(at - eye); - vec3 dir = normalize(at - eye); - - float dist = trace(eye, dir, maxd); - - if (dist > 0.5) - { - vec3 pos = eye + dir*dist; - vec3 normal = calcNormal(pos); - - vec2 bln = blinn(u_lightDir, normal, dir); - vec4 lc = lit(bln.x, bln.y, 1.0); - float fres = fresnel(bln.x, 0.2, 5.0); - - float val = 0.9*lc.y + pow(lc.z, 128.0)*fres; - val *= calcAmbOcc(pos, normal); - val = pow(val, 1.0/2.2); - - gl_FragColor = vec4(val, val, val, 1.0); - gl_FragDepth = dist/maxd; - } - else - { - gl_FragColor = v_color0; - gl_FragDepth = 1.0; - } -} +$input v_color0, v_texcoord0 + +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +// References: +// Sphere tracing: a geometric method for the antialiased ray tracing of implicit surfaces - John C. Hart +// http://web.archive.org/web/20110331200546/http://graphics.cs.uiuc.edu/~jch/papers/zeno.pdf +// +// Modeling with distance functions +// http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm + +#include "../common/common.sh" +#include "iq_sdf.sh" + +uniform float u_time; +uniform mat4 u_mtx; +uniform vec3 u_lightDir; + +float sceneDist(vec3 _pos) +{ + float d1 = udRoundBox(_pos, vec3(2.5, 2.5, 2.5), 0.5); + float d2 = sdSphere(_pos + vec3( 4.0, 0.0, 0.0), 1.0); + float d3 = sdSphere(_pos + vec3(-4.0, 0.0, 0.0), 1.0); + float d4 = sdSphere(_pos + vec3( 0.0, 4.0, 0.0), 1.0); + float d5 = sdSphere(_pos + vec3( 0.0,-4.0, 0.0), 1.0); + float d6 = sdSphere(_pos + vec3( 0.0, 0.0, 4.0), 1.0); + float d7 = sdSphere(_pos + vec3( 0.0, 0.0,-4.0), 1.0); + float dist = min(min(min(min(min(min(d1, d2), d3), d4), d5), d6), d7); + return dist; +} + +vec3 calcNormal(vec3 _pos) +{ + const vec2 delta = vec2(0.002, 0.0); + float nx = sceneDist(_pos + delta.xyy) - sceneDist(_pos - delta.xyy); + float ny = sceneDist(_pos + delta.yxy) - sceneDist(_pos - delta.yxy); + float nz = sceneDist(_pos + delta.yyx) - sceneDist(_pos - delta.yyx); + return normalize(vec3(nx, ny, nz) ); +} + +float calcAmbOcc(vec3 _pos, vec3 _normal) +{ + float occ = 0.0; + float aostep = 0.2; + for (int ii = 1; ii < 4; ii++) + { + float fi = float(ii); + float dist = sceneDist(_pos + _normal * fi * aostep); + occ += (fi * aostep - dist) / pow(2.0, fi); + } + + return 1.0 - occ; +} + +float trace(vec3 _ray, vec3 _dir, float _maxd) +{ + float tt = 0.0; + float epsilon = 0.001; + + for (int ii = 0; ii < 64; ii++) + { + float dist = sceneDist(_ray + _dir*tt); + if (dist > epsilon) + { + tt += dist; + } + } + + return tt < _maxd ? tt : 0.0; +} + +vec2 blinn(vec3 _lightDir, vec3 _normal, vec3 _viewDir) +{ + float ndotl = dot(_normal, _lightDir); + vec3 reflected = _lightDir - 2.0*ndotl*_normal; // reflect(_lightDir, _normal); + float rdotv = dot(reflected, _viewDir); + return vec2(ndotl, rdotv); +} + +float fresnel(float _ndotl, float _bias, float _pow) +{ + float facing = (1.0 - _ndotl); + return max(_bias + (1.0 - _bias) * pow(facing, _pow), 0.0); +} + +vec4 lit(float _ndotl, float _rdotv, float _m) +{ + float diff = max(0.0, _ndotl); + float spec = step(0.0, _ndotl) * max(0.0, _rdotv * _m); + return vec4(1.0, diff, spec, 1.0); +} + +void main() +{ + vec4 tmp; + tmp = mul(u_mtx, vec4(v_texcoord0.x, v_texcoord0.y, 0.0, 1.0) ); + vec3 eye = tmp.xyz/tmp.w; + + tmp = mul(u_mtx, vec4(v_texcoord0.x, v_texcoord0.y, 1.0, 1.0) ); + vec3 at = tmp.xyz/tmp.w; + + float maxd = length(at - eye); + vec3 dir = normalize(at - eye); + + float dist = trace(eye, dir, maxd); + + if (dist > 0.5) + { + vec3 pos = eye + dir*dist; + vec3 normal = calcNormal(pos); + + vec2 bln = blinn(u_lightDir, normal, dir); + vec4 lc = lit(bln.x, bln.y, 1.0); + float fres = fresnel(bln.x, 0.2, 5.0); + + float val = 0.9*lc.y + pow(lc.z, 128.0)*fres; + val *= calcAmbOcc(pos, normal); + val = pow(val, 1.0/2.2); + + gl_FragColor = vec4(val, val, val, 1.0); + gl_FragDepth = dist/maxd; + } + else + { + gl_FragColor = v_color0; + gl_FragDepth = 1.0; + } +} diff --git a/examples/03-raymarch/iq_sdf.sh b/examples/03-raymarch/iq_sdf.sh index 84a5a2ab..85814b12 100644 --- a/examples/03-raymarch/iq_sdf.sh +++ b/examples/03-raymarch/iq_sdf.sh @@ -1,82 +1,82 @@ -// -// References: -// Modeling with distance functions -// http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm -// - -// primitives - -float sdSphere(vec3 _pos, float _radius) -{ - return length(_pos) - _radius; -} - -float udBox(vec3 _pos, vec3 _extents) -{ - return length(max(abs(_pos) - _extents, 0.0) ); -} - -float udRoundBox(vec3 _pos, vec3 _extents, float r) -{ - return length(max(abs(_pos) - _extents, 0.0) ) - r; -} - -float sdBox(vec3 _pos, vec3 _extents) -{ - vec3 d = abs(_pos) - _extents; - return min(max(d.x, max(d.y, d.z) ), 0.0) + - length(max(d, 0.0) ); -} - -float sdTorus(vec3 _pos, vec2 t) -{ - vec2 q = vec2(length(_pos.xz) - t.x, _pos.y); - return length(q) - t.y; -} - -float sdCylinder(vec3 _pos, vec3 c) -{ - return length(_pos.xz - c.xy) - c.z; -} - -float sdCone(vec3 _pos, vec2 c) -{ - // c must be normalized - float q = length(_pos.xy); - return dot(c, vec2(q, _pos.z) ); -} - -float sdPlane(vec3 _pos, vec4 n) -{ - // n must be normalized - return dot(_pos, n.xyz) + n.w; -} - -float sdHexPrism(vec3 _pos, vec2 h) -{ - vec3 q = abs(_pos); - return max(q.z - h.y, max(q.x + q.y * 0.57735, q.y * 1.1547) - h.x); -} - -float sdTriPrism(vec3 _pos, vec2 h) -{ - vec3 q = abs(_pos); - return max(q.z - h.y, max(q.x * 0.866025 + _pos.y * 0.5, -_pos.y) - h.x * 0.5); -} - -// domain operations - -float opUnion(float d1, float d2) -{ - return min(d1, d2); -} - -float opSubtract(float d1, float d2) -{ - return max(-d1, d2); -} - -float opIntersect(float d1, float d2) -{ - return max(d1, d2); -} +// +// References: +// Modeling with distance functions +// http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm +// + +// primitives + +float sdSphere(vec3 _pos, float _radius) +{ + return length(_pos) - _radius; +} + +float udBox(vec3 _pos, vec3 _extents) +{ + return length(max(abs(_pos) - _extents, 0.0) ); +} + +float udRoundBox(vec3 _pos, vec3 _extents, float r) +{ + return length(max(abs(_pos) - _extents, 0.0) ) - r; +} + +float sdBox(vec3 _pos, vec3 _extents) +{ + vec3 d = abs(_pos) - _extents; + return min(max(d.x, max(d.y, d.z) ), 0.0) + + length(max(d, 0.0) ); +} + +float sdTorus(vec3 _pos, vec2 t) +{ + vec2 q = vec2(length(_pos.xz) - t.x, _pos.y); + return length(q) - t.y; +} + +float sdCylinder(vec3 _pos, vec3 c) +{ + return length(_pos.xz - c.xy) - c.z; +} + +float sdCone(vec3 _pos, vec2 c) +{ + // c must be normalized + float q = length(_pos.xy); + return dot(c, vec2(q, _pos.z) ); +} + +float sdPlane(vec3 _pos, vec4 n) +{ + // n must be normalized + return dot(_pos, n.xyz) + n.w; +} + +float sdHexPrism(vec3 _pos, vec2 h) +{ + vec3 q = abs(_pos); + return max(q.z - h.y, max(q.x + q.y * 0.57735, q.y * 1.1547) - h.x); +} + +float sdTriPrism(vec3 _pos, vec2 h) +{ + vec3 q = abs(_pos); + return max(q.z - h.y, max(q.x * 0.866025 + _pos.y * 0.5, -_pos.y) - h.x * 0.5); +} + +// domain operations + +float opUnion(float d1, float d2) +{ + return min(d1, d2); +} + +float opSubtract(float d1, float d2) +{ + return max(-d1, d2); +} + +float opIntersect(float d1, float d2) +{ + return max(d1, d2); +} diff --git a/examples/03-raymarch/raymarch.cpp b/examples/03-raymarch/raymarch.cpp index 6e85243d..38e82ae6 100644 --- a/examples/03-raymarch/raymarch.cpp +++ b/examples/03-raymarch/raymarch.cpp @@ -175,16 +175,16 @@ void renderScreenSpaceQuad(uint32_t _view, bgfx::ProgramHandle _program, float _ int _main_(int _argc, char** _argv) { - uint32_t width = 1280; - uint32_t height = 720; - uint32_t debug = BGFX_DEBUG_TEXT; - - bgfx::init(); - bgfx::reset(width, height); - - // Enable debug text. - bgfx::setDebug(debug); - + uint32_t width = 1280; + uint32_t height = 720; + uint32_t debug = BGFX_DEBUG_TEXT; + + bgfx::init(); + bgfx::reset(width, height); + + // Enable debug text. + bgfx::setDebug(debug); + // Set view 0 clear state. bgfx::setViewClear(0 , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT @@ -231,14 +231,14 @@ int _main_(int _argc, char** _argv) bgfx::ProgramHandle raymarching = loadProgram("vs_raymarching", "fs_raymarching"); - while (!processEvents(width, height, debug) ) - { - // Set view 0 default viewport. - bgfx::setViewRect(0, 0, 0, width, height); - - // Set view 1 default viewport. - bgfx::setViewRect(1, 0, 0, width, height); - + while (!processEvents(width, height, debug) ) + { + // Set view 0 default viewport. + bgfx::setViewRect(0, 0, 0, width, height); + + // Set view 1 default viewport. + bgfx::setViewRect(1, 0, 0, width, height); + // This dummy draw call is here to make sure that view 0 is cleared // if no other draw calls are submitted to viewZ 0. bgfx::submit(0); diff --git a/examples/03-raymarch/varying.def.sc b/examples/03-raymarch/varying.def.sc index fc051911..2aa3cb42 100644 --- a/examples/03-raymarch/varying.def.sc +++ b/examples/03-raymarch/varying.def.sc @@ -1,6 +1,6 @@ -vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); -vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0); - -vec3 a_position : POSITION; -vec4 a_color0 : COLOR0; -vec2 a_texcoord0 : TEXCOORD0; +vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); +vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0); + +vec3 a_position : POSITION; +vec4 a_color0 : COLOR0; +vec2 a_texcoord0 : TEXCOORD0; diff --git a/examples/03-raymarch/vs_raymarching.sc b/examples/03-raymarch/vs_raymarching.sc index e0d12092..32afe95b 100644 --- a/examples/03-raymarch/vs_raymarching.sc +++ b/examples/03-raymarch/vs_raymarching.sc @@ -1,16 +1,16 @@ -$input a_position, a_color0, a_texcoord0 -$output v_color0, v_texcoord0 - -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include "../common/common.sh" - -void main() -{ - gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) ); - v_color0 = a_color0; - v_texcoord0 = a_texcoord0; -} +$input a_position, a_color0, a_texcoord0 +$output v_color0, v_texcoord0 + +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "../common/common.sh" + +void main() +{ + gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) ); + v_color0 = a_color0; + v_texcoord0 = a_texcoord0; +} diff --git a/examples/04-mesh/fs_mesh.sc b/examples/04-mesh/fs_mesh.sc index b1b53eb8..a0c19d11 100644 --- a/examples/04-mesh/fs_mesh.sc +++ b/examples/04-mesh/fs_mesh.sc @@ -1,54 +1,54 @@ -$input v_pos, v_view, v_normal, v_color0 - -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include "../common/common.sh" - -uniform float u_time; - -vec2 blinn(vec3 _lightDir, vec3 _normal, vec3 _viewDir) -{ - float ndotl = dot(_normal, _lightDir); - vec3 reflected = _lightDir - 2.0*ndotl*_normal; // reflect(_lightDir, _normal); - float rdotv = dot(reflected, _viewDir); - return vec2(ndotl, rdotv); -} - -float fresnel(float _ndotl, float _bias, float _pow) -{ - float facing = (1.0 - _ndotl); - return max(_bias + (1.0 - _bias) * pow(facing, _pow), 0.0); -} - -vec4 lit(float _ndotl, float _rdotv, float _m) -{ - float diff = max(0.0, _ndotl); - float spec = step(0.0, _ndotl) * max(0.0, _rdotv * _m); - return vec4(1.0, diff, spec, 1.0); -} - -void main() -{ - vec3 lightDir = vec3(0.0, 0.0, -1.0); - vec3 normal = normalize(v_normal); - vec3 view = normalize(v_view); - vec2 bln = blinn(lightDir, normal, view); - vec4 lc = lit(bln.x, bln.y, 1.0); - float fres = fresnel(bln.x, 0.2, 5.0); - - float index = ( (sin(v_pos.x*3.0+u_time)*0.3+0.7) - + ( cos(v_pos.y*3.0+u_time)*0.4+0.6) - + ( cos(v_pos.z*3.0+u_time)*0.2+0.8) - )*M_PI; - - vec3 color = vec3(sin(index*8.0)*0.4 + 0.6 - , sin(index*4.0)*0.4 + 0.6 - , sin(index*2.0)*0.4 + 0.6 - ) * v_color0.xyz; - - gl_FragColor.xyz = pow(vec3(0.07, 0.06, 0.08) + color*lc.y + fres*pow(lc.z, 128.0), vec3_splat(1.0/2.2) ); - gl_FragColor.w = 1.0; -} +$input v_pos, v_view, v_normal, v_color0 + +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "../common/common.sh" + +uniform float u_time; + +vec2 blinn(vec3 _lightDir, vec3 _normal, vec3 _viewDir) +{ + float ndotl = dot(_normal, _lightDir); + vec3 reflected = _lightDir - 2.0*ndotl*_normal; // reflect(_lightDir, _normal); + float rdotv = dot(reflected, _viewDir); + return vec2(ndotl, rdotv); +} + +float fresnel(float _ndotl, float _bias, float _pow) +{ + float facing = (1.0 - _ndotl); + return max(_bias + (1.0 - _bias) * pow(facing, _pow), 0.0); +} + +vec4 lit(float _ndotl, float _rdotv, float _m) +{ + float diff = max(0.0, _ndotl); + float spec = step(0.0, _ndotl) * max(0.0, _rdotv * _m); + return vec4(1.0, diff, spec, 1.0); +} + +void main() +{ + vec3 lightDir = vec3(0.0, 0.0, -1.0); + vec3 normal = normalize(v_normal); + vec3 view = normalize(v_view); + vec2 bln = blinn(lightDir, normal, view); + vec4 lc = lit(bln.x, bln.y, 1.0); + float fres = fresnel(bln.x, 0.2, 5.0); + + float index = ( (sin(v_pos.x*3.0+u_time)*0.3+0.7) + + ( cos(v_pos.y*3.0+u_time)*0.4+0.6) + + ( cos(v_pos.z*3.0+u_time)*0.2+0.8) + )*M_PI; + + vec3 color = vec3(sin(index*8.0)*0.4 + 0.6 + , sin(index*4.0)*0.4 + 0.6 + , sin(index*2.0)*0.4 + 0.6 + ) * v_color0.xyz; + + gl_FragColor.xyz = pow(vec3(0.07, 0.06, 0.08) + color*lc.y + fres*pow(lc.z, 128.0), vec3_splat(1.0/2.2) ); + gl_FragColor.w = 1.0; +} diff --git a/examples/04-mesh/varying.def.sc b/examples/04-mesh/varying.def.sc index b7b399dd..a6670786 100644 --- a/examples/04-mesh/varying.def.sc +++ b/examples/04-mesh/varying.def.sc @@ -1,10 +1,10 @@ -vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); -vec3 v_normal : NORMAL = vec3(0.0, 0.0, 1.0); -vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0); -vec3 v_pos : TEXCOORD1 = vec3(0.0, 0.0, 0.0); -vec3 v_view : TEXCOORD2 = vec3(0.0, 0.0, 0.0); - -vec3 a_position : POSITION; -vec4 a_color0 : COLOR0; -vec2 a_texcoord0 : TEXCOORD0; -vec3 a_normal : NORMAL; +vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); +vec3 v_normal : NORMAL = vec3(0.0, 0.0, 1.0); +vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0); +vec3 v_pos : TEXCOORD1 = vec3(0.0, 0.0, 0.0); +vec3 v_view : TEXCOORD2 = vec3(0.0, 0.0, 0.0); + +vec3 a_position : POSITION; +vec4 a_color0 : COLOR0; +vec2 a_texcoord0 : TEXCOORD0; +vec3 a_normal : NORMAL; diff --git a/examples/04-mesh/vs_mesh.sc b/examples/04-mesh/vs_mesh.sc index 131d5344..8cecbcfb 100644 --- a/examples/04-mesh/vs_mesh.sc +++ b/examples/04-mesh/vs_mesh.sc @@ -1,32 +1,32 @@ -$input a_position, a_normal -$output v_pos, v_view, v_normal, v_color0 - -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include "../common/common.sh" - -uniform float u_time; - -void main() -{ - vec3 pos = a_position; - - float sx = sin(pos.x*32.0+u_time*4.0)*0.5+0.5; - float cy = cos(pos.y*32.0+u_time*4.0)*0.5+0.5; - vec3 displacement = vec3(sx, cy, sx*cy); - vec3 normal = a_normal.xyz*2.0 - 1.0; - - pos = pos + normal*displacement*vec3(0.06, 0.06, 0.06); - - gl_Position = mul(u_modelViewProj, vec4(pos, 1.0) ); - v_pos = gl_Position.xyz; - v_view = mul(u_modelView, vec4(pos, 1.0) ).xyz; - - v_normal = mul(u_modelView, vec4(normal, 0.0) ).xyz; - - float len = length(displacement)*0.4+0.6; - v_color0 = vec4(len, len, len, 1.0); -} +$input a_position, a_normal +$output v_pos, v_view, v_normal, v_color0 + +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "../common/common.sh" + +uniform float u_time; + +void main() +{ + vec3 pos = a_position; + + float sx = sin(pos.x*32.0+u_time*4.0)*0.5+0.5; + float cy = cos(pos.y*32.0+u_time*4.0)*0.5+0.5; + vec3 displacement = vec3(sx, cy, sx*cy); + vec3 normal = a_normal.xyz*2.0 - 1.0; + + pos = pos + normal*displacement*vec3(0.06, 0.06, 0.06); + + gl_Position = mul(u_modelViewProj, vec4(pos, 1.0) ); + v_pos = gl_Position.xyz; + v_view = mul(u_modelView, vec4(pos, 1.0) ).xyz; + + v_normal = mul(u_modelView, vec4(normal, 0.0) ).xyz; + + float len = length(displacement)*0.4+0.6; + v_color0 = vec4(len, len, len, 1.0); +} diff --git a/examples/05-instancing/fs_instancing.sc b/examples/05-instancing/fs_instancing.sc index a6318ee4..afc160fa 100644 --- a/examples/05-instancing/fs_instancing.sc +++ b/examples/05-instancing/fs_instancing.sc @@ -1,13 +1,13 @@ -$input v_color0 - -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include "../common/common.sh" - -void main() -{ - gl_FragColor = v_color0; -} +$input v_color0 + +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "../common/common.sh" + +void main() +{ + gl_FragColor = v_color0; +} diff --git a/examples/05-instancing/instancing.cpp b/examples/05-instancing/instancing.cpp index 8af0db7f..5cce7c8b 100644 --- a/examples/05-instancing/instancing.cpp +++ b/examples/05-instancing/instancing.cpp @@ -96,16 +96,16 @@ static const bgfx::Memory* loadShader(const char* _name) int _main_(int _argc, char** _argv) { - uint32_t width = 1280; - uint32_t height = 720; - uint32_t debug = BGFX_DEBUG_TEXT; - - bgfx::init(); - bgfx::reset(width, height); - - // Enable debug text. - bgfx::setDebug(debug); - + uint32_t width = 1280; + uint32_t height = 720; + uint32_t debug = BGFX_DEBUG_TEXT; + + bgfx::init(); + bgfx::reset(width, height); + + // Enable debug text. + bgfx::setDebug(debug); + // Set view 0 clear state. bgfx::setViewClear(0 , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT @@ -171,11 +171,11 @@ int _main_(int _argc, char** _argv) bgfx::destroyVertexShader(vsh); bgfx::destroyFragmentShader(fsh); - while (!processEvents(width, height, debug) ) - { - // Set view 0 default viewport. - bgfx::setViewRect(0, 0, 0, width, height); - + while (!processEvents(width, height, debug) ) + { + // Set view 0 default viewport. + bgfx::setViewRect(0, 0, 0, width, height); + // This dummy draw call is here to make sure that view 0 is cleared // if no other draw calls are submitted to view 0. bgfx::submit(0); diff --git a/examples/05-instancing/varying.def.sc b/examples/05-instancing/varying.def.sc index 77ab0393..d8e2ca91 100644 --- a/examples/05-instancing/varying.def.sc +++ b/examples/05-instancing/varying.def.sc @@ -1,9 +1,9 @@ -vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); - -vec3 a_position : POSITION; -vec4 a_color0 : COLOR0; -vec4 i_data0 : TEXCOORD3; -vec4 i_data1 : TEXCOORD4; -vec4 i_data2 : TEXCOORD5; -vec4 i_data3 : TEXCOORD6; -vec4 i_data4 : TEXCOORD7; +vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); + +vec3 a_position : POSITION; +vec4 a_color0 : COLOR0; +vec4 i_data0 : TEXCOORD3; +vec4 i_data1 : TEXCOORD4; +vec4 i_data2 : TEXCOORD5; +vec4 i_data3 : TEXCOORD6; +vec4 i_data4 : TEXCOORD7; diff --git a/examples/05-instancing/vs_instancing.sc b/examples/05-instancing/vs_instancing.sc index c0fcbe8e..e8e439aa 100644 --- a/examples/05-instancing/vs_instancing.sc +++ b/examples/05-instancing/vs_instancing.sc @@ -1,22 +1,22 @@ -$input a_position, a_color0, i_data0, i_data1, i_data2, i_data3, i_data4 -$output v_color0 - -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include "../common/common.sh" - -void main() -{ - mat4 model; - model[0] = i_data0; - model[1] = i_data1; - model[2] = i_data2; - model[3] = i_data3; - - vec4 worldPos = instMul(model, vec4(a_position, 1.0) ); - gl_Position = mul(u_viewProj, worldPos); - v_color0 = a_color0*i_data4; -} +$input a_position, a_color0, i_data0, i_data1, i_data2, i_data3, i_data4 +$output v_color0 + +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "../common/common.sh" + +void main() +{ + mat4 model; + model[0] = i_data0; + model[1] = i_data1; + model[2] = i_data2; + model[3] = i_data3; + + vec4 worldPos = instMul(model, vec4(a_position, 1.0) ); + gl_Position = mul(u_viewProj, worldPos); + v_color0 = a_color0*i_data4; +} diff --git a/examples/06-bump/bump.cpp b/examples/06-bump/bump.cpp index ac672674..8d8e223e 100644 --- a/examples/06-bump/bump.cpp +++ b/examples/06-bump/bump.cpp @@ -151,17 +151,17 @@ static const bgfx::Memory* loadTexture(const char* _name) void calcTangents(void* _vertices, uint16_t _numVertices, bgfx::VertexDecl _decl, const uint16_t* _indices, uint32_t _numIndices) { - struct PosTexcoord - { - float m_x; - float m_y; - float m_z; - float m_pad0; - float m_u; - float m_v; - float m_pad1; - float m_pad2; - }; + struct PosTexcoord + { + float m_x; + float m_y; + float m_z; + float m_pad0; + float m_u; + float m_v; + float m_pad1; + float m_pad2; + }; float* tangents = new float[6*_numVertices]; memset(tangents, 0, 6*_numVertices*sizeof(float) ); @@ -252,16 +252,16 @@ void calcTangents(void* _vertices, uint16_t _numVertices, bgfx::VertexDecl _decl int _main_(int _argc, char** _argv) { - uint32_t width = 1280; - uint32_t height = 720; - uint32_t debug = BGFX_DEBUG_TEXT; - - bgfx::init(); - bgfx::reset(width, height); - - // Enable debug text. - bgfx::setDebug(debug); - + uint32_t width = 1280; + uint32_t height = 720; + uint32_t debug = BGFX_DEBUG_TEXT; + + bgfx::init(); + bgfx::reset(width, height); + + // Enable debug text. + bgfx::setDebug(debug); + // Set view 0 clear state. bgfx::setViewClear(0 , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT @@ -347,11 +347,11 @@ int _main_(int _argc, char** _argv) mem = loadTexture("fieldstone-n.dds"); bgfx::TextureHandle textureNormal = bgfx::createTexture(mem); - while (!processEvents(width, height, debug) ) - { - // Set view 0 default viewport. - bgfx::setViewRect(0, 0, 0, width, height); - + while (!processEvents(width, height, debug) ) + { + // Set view 0 default viewport. + bgfx::setViewRect(0, 0, 0, width, height); + // This dummy draw call is here to make sure that view 0 is cleared // if no other draw calls are submitted to view 0. bgfx::submit(0); diff --git a/examples/06-bump/fs_bump.sc b/examples/06-bump/fs_bump.sc index e1e508d6..9350190b 100644 --- a/examples/06-bump/fs_bump.sc +++ b/examples/06-bump/fs_bump.sc @@ -1,79 +1,79 @@ -$input v_wpos, v_view, v_normal, v_tangent, v_bitangent, v_texcoord0 - -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include "../common/common.sh" - -SAMPLER2D(u_texColor, 0); -SAMPLER2D(u_texNormal, 1); -uniform vec4 u_lightPosRadius[4]; -uniform vec4 u_lightRgbInnerR[4]; - -vec2 blinn(vec3 _lightDir, vec3 _normal, vec3 _viewDir) -{ - float ndotl = dot(_normal, _lightDir); - vec3 reflected = _lightDir - 2.0*ndotl*_normal; // reflect(_lightDir, _normal); - float rdotv = dot(reflected, _viewDir); - return vec2(ndotl, rdotv); -} - -float fresnel(float _ndotl, float _bias, float _pow) -{ - float facing = (1.0 - _ndotl); - return max(_bias + (1.0 - _bias) * pow(facing, _pow), 0.0); -} - -vec4 lit(float _ndotl, float _rdotv, float _m) -{ - float diff = max(0.0, _ndotl); - float spec = step(0.0, _ndotl) * max(0.0, _rdotv * _m); - return vec4(1.0, diff, spec, 1.0); -} - -vec4 powRgba(vec4 _rgba, float _pow) -{ - vec4 result; - result.xyz = pow(_rgba.xyz, vec3_splat(_pow) ); - result.w = _rgba.w; - return result; -} - -vec3 calcLight(int _idx, mat3 _tbn, vec3 _wpos, vec3 _normal, vec3 _view) -{ - vec3 lp = u_lightPosRadius[_idx].xyz - _wpos; - float attn = 1.0 - smoothstep(u_lightRgbInnerR[_idx].w, 1.0, length(lp) / u_lightPosRadius[_idx].w); - vec3 lightDir = mul(_tbn, normalize(lp) ); - vec2 bln = blinn(lightDir, _normal, _view); - vec4 lc = lit(bln.x, bln.y, 1.0); - vec3 rgb = u_lightRgbInnerR[_idx].xyz*max(0.0, saturate(lc.y) ) * attn; - return rgb; -} - -void main() -{ - mat3 tbn = mat3( - normalize(v_tangent), - normalize(v_bitangent), - normalize(v_normal) - ); - - vec3 normal; - normal.xy = texture2D(u_texNormal, v_texcoord0).xy * 2.0 - 1.0; - normal.z = sqrt(1.0 - dot(normal.xy, normal.xy) ); - vec3 view = -normalize(v_view); - - vec3 lightColor; - lightColor = calcLight(0, tbn, v_wpos, normal, view); - lightColor += calcLight(1, tbn, v_wpos, normal, view); - lightColor += calcLight(2, tbn, v_wpos, normal, view); - lightColor += calcLight(3, tbn, v_wpos, normal, view); - - vec4 color = toLinear(texture2D(u_texColor, v_texcoord0) ); - - gl_FragColor.xyz = max(vec3_splat(0.05), lightColor.xyz)*color.xyz; - gl_FragColor.w = 1.0; - gl_FragColor = toGamma(gl_FragColor); -} +$input v_wpos, v_view, v_normal, v_tangent, v_bitangent, v_texcoord0 + +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "../common/common.sh" + +SAMPLER2D(u_texColor, 0); +SAMPLER2D(u_texNormal, 1); +uniform vec4 u_lightPosRadius[4]; +uniform vec4 u_lightRgbInnerR[4]; + +vec2 blinn(vec3 _lightDir, vec3 _normal, vec3 _viewDir) +{ + float ndotl = dot(_normal, _lightDir); + vec3 reflected = _lightDir - 2.0*ndotl*_normal; // reflect(_lightDir, _normal); + float rdotv = dot(reflected, _viewDir); + return vec2(ndotl, rdotv); +} + +float fresnel(float _ndotl, float _bias, float _pow) +{ + float facing = (1.0 - _ndotl); + return max(_bias + (1.0 - _bias) * pow(facing, _pow), 0.0); +} + +vec4 lit(float _ndotl, float _rdotv, float _m) +{ + float diff = max(0.0, _ndotl); + float spec = step(0.0, _ndotl) * max(0.0, _rdotv * _m); + return vec4(1.0, diff, spec, 1.0); +} + +vec4 powRgba(vec4 _rgba, float _pow) +{ + vec4 result; + result.xyz = pow(_rgba.xyz, vec3_splat(_pow) ); + result.w = _rgba.w; + return result; +} + +vec3 calcLight(int _idx, mat3 _tbn, vec3 _wpos, vec3 _normal, vec3 _view) +{ + vec3 lp = u_lightPosRadius[_idx].xyz - _wpos; + float attn = 1.0 - smoothstep(u_lightRgbInnerR[_idx].w, 1.0, length(lp) / u_lightPosRadius[_idx].w); + vec3 lightDir = mul(_tbn, normalize(lp) ); + vec2 bln = blinn(lightDir, _normal, _view); + vec4 lc = lit(bln.x, bln.y, 1.0); + vec3 rgb = u_lightRgbInnerR[_idx].xyz*max(0.0, saturate(lc.y) ) * attn; + return rgb; +} + +void main() +{ + mat3 tbn = mat3( + normalize(v_tangent), + normalize(v_bitangent), + normalize(v_normal) + ); + + vec3 normal; + normal.xy = texture2D(u_texNormal, v_texcoord0).xy * 2.0 - 1.0; + normal.z = sqrt(1.0 - dot(normal.xy, normal.xy) ); + vec3 view = -normalize(v_view); + + vec3 lightColor; + lightColor = calcLight(0, tbn, v_wpos, normal, view); + lightColor += calcLight(1, tbn, v_wpos, normal, view); + lightColor += calcLight(2, tbn, v_wpos, normal, view); + lightColor += calcLight(3, tbn, v_wpos, normal, view); + + vec4 color = toLinear(texture2D(u_texColor, v_texcoord0) ); + + gl_FragColor.xyz = max(vec3_splat(0.05), lightColor.xyz)*color.xyz; + gl_FragColor.w = 1.0; + gl_FragColor = toGamma(gl_FragColor); +} diff --git a/examples/06-bump/varying.def.sc b/examples/06-bump/varying.def.sc index 41b31c03..716bbca5 100644 --- a/examples/06-bump/varying.def.sc +++ b/examples/06-bump/varying.def.sc @@ -1,15 +1,15 @@ -vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0); -vec3 v_wpos : TEXCOORD1 = vec3(0.0, 0.0, 0.0); -vec3 v_view : TEXCOORD2 = vec3(0.0, 0.0, 0.0); -vec3 v_normal : NORMAL = vec3(0.0, 0.0, 1.0); -vec3 v_tangent : TANGENT = vec3(1.0, 0.0, 0.0); -vec3 v_bitangent : BINORMAL = vec3(0.0, 1.0, 0.0); - -vec3 a_position : POSITION; -vec4 a_normal : NORMAL; -vec4 a_tangent : TANGENT; -vec2 a_texcoord0 : TEXCOORD0; -vec4 i_data0 : TEXCOORD4; -vec4 i_data1 : TEXCOORD5; -vec4 i_data2 : TEXCOORD6; -vec4 i_data3 : TEXCOORD7; +vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0); +vec3 v_wpos : TEXCOORD1 = vec3(0.0, 0.0, 0.0); +vec3 v_view : TEXCOORD2 = vec3(0.0, 0.0, 0.0); +vec3 v_normal : NORMAL = vec3(0.0, 0.0, 1.0); +vec3 v_tangent : TANGENT = vec3(1.0, 0.0, 0.0); +vec3 v_bitangent : BINORMAL = vec3(0.0, 1.0, 0.0); + +vec3 a_position : POSITION; +vec4 a_normal : NORMAL; +vec4 a_tangent : TANGENT; +vec2 a_texcoord0 : TEXCOORD0; +vec4 i_data0 : TEXCOORD4; +vec4 i_data1 : TEXCOORD5; +vec4 i_data2 : TEXCOORD6; +vec4 i_data3 : TEXCOORD7; diff --git a/examples/06-bump/vs_bump.sc b/examples/06-bump/vs_bump.sc index f65eff9a..d93bbdce 100644 --- a/examples/06-bump/vs_bump.sc +++ b/examples/06-bump/vs_bump.sc @@ -1,43 +1,43 @@ -$input a_position, a_normal, a_tangent, a_texcoord0, i_data0, i_data1, i_data2, i_data3 -$output v_wpos, v_view, v_normal, v_tangent, v_bitangent, v_texcoord0 - -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include "../common/common.sh" - -void main() -{ - mat4 model; - model[0] = i_data0; - model[1] = i_data1; - model[2] = i_data2; - model[3] = i_data3; - - vec3 wpos = instMul(model, vec4(a_position, 1.0) ).xyz; - gl_Position = mul(u_viewProj, vec4(wpos, 1.0) ); - - vec4 normal = a_normal * 2.0f - 1.0f; - vec3 wnormal = instMul(model, vec4(normal.xyz, 0.0) ).xyz; - - vec4 tangent = a_tangent * 2.0f - 1.0f; - vec3 wtangent = instMul(model, vec4(tangent.xyz, 0.0) ).xyz; - - vec3 viewNormal = normalize(mul(u_view, vec4(wnormal, 0.0) ).xyz); - vec3 viewTangent = normalize(mul(u_view, vec4(wtangent, 0.0) ).xyz); - vec3 viewBitangent = cross(viewNormal, viewTangent) * tangent.w; - mat3 tbn = mat3(viewTangent, viewBitangent, viewNormal); - - v_wpos = wpos; - - vec3 view = mul(u_view, vec4(wpos, 0.0) ).xyz; - v_view = instMul(view, tbn); - - v_normal = viewNormal; - v_tangent = viewTangent; - v_bitangent = viewBitangent; - - v_texcoord0 = a_texcoord0; -} +$input a_position, a_normal, a_tangent, a_texcoord0, i_data0, i_data1, i_data2, i_data3 +$output v_wpos, v_view, v_normal, v_tangent, v_bitangent, v_texcoord0 + +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "../common/common.sh" + +void main() +{ + mat4 model; + model[0] = i_data0; + model[1] = i_data1; + model[2] = i_data2; + model[3] = i_data3; + + vec3 wpos = instMul(model, vec4(a_position, 1.0) ).xyz; + gl_Position = mul(u_viewProj, vec4(wpos, 1.0) ); + + vec4 normal = a_normal * 2.0f - 1.0f; + vec3 wnormal = instMul(model, vec4(normal.xyz, 0.0) ).xyz; + + vec4 tangent = a_tangent * 2.0f - 1.0f; + vec3 wtangent = instMul(model, vec4(tangent.xyz, 0.0) ).xyz; + + vec3 viewNormal = normalize(mul(u_view, vec4(wnormal, 0.0) ).xyz); + vec3 viewTangent = normalize(mul(u_view, vec4(wtangent, 0.0) ).xyz); + vec3 viewBitangent = cross(viewNormal, viewTangent) * tangent.w; + mat3 tbn = mat3(viewTangent, viewBitangent, viewNormal); + + v_wpos = wpos; + + vec3 view = mul(u_view, vec4(wpos, 0.0) ).xyz; + v_view = instMul(view, tbn); + + v_normal = viewNormal; + v_tangent = viewTangent; + v_bitangent = viewBitangent; + + v_texcoord0 = a_texcoord0; +} diff --git a/examples/07-callback/fs_callback.sc b/examples/07-callback/fs_callback.sc index ce3e23dd..3985e408 100644 --- a/examples/07-callback/fs_callback.sc +++ b/examples/07-callback/fs_callback.sc @@ -1,17 +1,17 @@ -$input v_world, v_color0 - -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include "../common/common.sh" - -void main() -{ - vec3 normal = normalize(cross(dFdx(v_world), dFdy(v_world) ) ); - vec3 lightDir = vec3(0.0, 0.0, 1.0); - float ndotl = max(dot(normal, lightDir), 0.0); - float spec = pow(ndotl, 30.0); - gl_FragColor = pow(pow(v_color0, vec4_splat(2.2) ) * ndotl + spec, vec4_splat(1.0/2.2) ); -} +$input v_world, v_color0 + +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "../common/common.sh" + +void main() +{ + vec3 normal = normalize(cross(dFdx(v_world), dFdy(v_world) ) ); + vec3 lightDir = vec3(0.0, 0.0, 1.0); + float ndotl = max(dot(normal, lightDir), 0.0); + float spec = pow(ndotl, 30.0); + gl_FragColor = pow(pow(v_color0, vec4_splat(2.2) ) * ndotl + spec, vec4_splat(1.0/2.2) ); +} diff --git a/examples/07-callback/varying.def.sc b/examples/07-callback/varying.def.sc index 3f1a2779..9e761618 100644 --- a/examples/07-callback/varying.def.sc +++ b/examples/07-callback/varying.def.sc @@ -1,5 +1,5 @@ -vec3 v_world : TEXCOORD0 = vec3(0.0, 0.0, 0.0); -vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); - -vec3 a_position : POSITION; -vec4 a_color0 : COLOR0; +vec3 v_world : TEXCOORD0 = vec3(0.0, 0.0, 0.0); +vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); + +vec3 a_position : POSITION; +vec4 a_color0 : COLOR0; diff --git a/examples/07-callback/vs_callback.sc b/examples/07-callback/vs_callback.sc index 50da3b66..4c2f5392 100644 --- a/examples/07-callback/vs_callback.sc +++ b/examples/07-callback/vs_callback.sc @@ -1,16 +1,16 @@ -$input a_position, a_color0 -$output v_world, v_color0 - -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include "../common/common.sh" - -void main() -{ - gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) ); - v_world = mul(u_model, vec4(a_position, 1.0) ).xyz; - v_color0 = a_color0; -} +$input a_position, a_color0 +$output v_world, v_color0 + +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "../common/common.sh" + +void main() +{ + gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) ); + v_world = mul(u_model, vec4(a_position, 1.0) ).xyz; + v_color0 = a_color0; +} diff --git a/examples/08-update/fs_update.sc b/examples/08-update/fs_update.sc index 1f7ce62d..74bf3122 100644 --- a/examples/08-update/fs_update.sc +++ b/examples/08-update/fs_update.sc @@ -1,15 +1,15 @@ -$input v_texcoord0 - -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include "../common/common.sh" - -SAMPLERCUBE(u_texCube, 0); - -void main() -{ - gl_FragColor = textureCube(u_texCube, v_texcoord0); -} +$input v_texcoord0 + +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "../common/common.sh" + +SAMPLERCUBE(u_texCube, 0); + +void main() +{ + gl_FragColor = textureCube(u_texCube, v_texcoord0); +} diff --git a/examples/08-update/update.cpp b/examples/08-update/update.cpp index f065013b..b4d9c7b4 100644 --- a/examples/08-update/update.cpp +++ b/examples/08-update/update.cpp @@ -125,16 +125,16 @@ static const bgfx::Memory* loadShader(const char* _name) int _main_(int _argc, char** _argv) { - uint32_t width = 1280; - uint32_t height = 720; - uint32_t debug = BGFX_DEBUG_TEXT; - - bgfx::init(); - bgfx::reset(width, height); - - // Enable debug text. - bgfx::setDebug(debug); - + uint32_t width = 1280; + uint32_t height = 720; + uint32_t debug = BGFX_DEBUG_TEXT; + + bgfx::init(); + bgfx::reset(width, height); + + // Enable debug text. + bgfx::setDebug(debug); + // Set view 0 clear state. bgfx::setViewClear(0 , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT @@ -227,11 +227,11 @@ int _main_(int _argc, char** _argv) int64_t updateTime = 0; - while (!processEvents(width, height, debug) ) - { - // Set view 0 default viewport. - bgfx::setViewRect(0, 0, 0, width, height); - + while (!processEvents(width, height, debug) ) + { + // Set view 0 default viewport. + bgfx::setViewRect(0, 0, 0, width, height); + // This dummy draw call is here to make sure that view 0 is cleared // if no other draw calls are submitted to view 0. bgfx::submit(0); diff --git a/examples/08-update/varying.def.sc b/examples/08-update/varying.def.sc index 73b1866c..9b8cfd3c 100644 --- a/examples/08-update/varying.def.sc +++ b/examples/08-update/varying.def.sc @@ -1,4 +1,4 @@ -vec3 v_texcoord0 : TEXCOORD0 = vec3(9.0, 0.0, 0.0); - -vec3 a_position : POSITION; -vec3 a_texcoord0 : TEXCOORD0; +vec3 v_texcoord0 : TEXCOORD0 = vec3(9.0, 0.0, 0.0); + +vec3 a_position : POSITION; +vec3 a_texcoord0 : TEXCOORD0; diff --git a/examples/08-update/vs_update.sc b/examples/08-update/vs_update.sc index 6369cca3..c158f761 100644 --- a/examples/08-update/vs_update.sc +++ b/examples/08-update/vs_update.sc @@ -1,15 +1,15 @@ -$input a_position, a_texcoord0 -$output v_texcoord0 - -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include "../common/common.sh" - -void main() -{ - gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) ); - v_texcoord0 = a_texcoord0; -} +$input a_position, a_texcoord0 +$output v_texcoord0 + +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "../common/common.sh" + +void main() +{ + gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) ); + v_texcoord0 = a_texcoord0; +} diff --git a/examples/common/aviwriter.h b/examples/common/aviwriter.h index 82454c37..678a4ad7 100644 --- a/examples/common/aviwriter.h +++ b/examples/common/aviwriter.h @@ -1,229 +1,229 @@ -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#ifndef __AVIWRITER_H__ -#define __AVIWRITER_H__ - -#include <bx/readerwriter.h> - -// Simple AVI writer. VideoLAN and VirtualDub can decode it. -// Needs some bits to get jiggled to work with other players. But it's good -// enough for an example. -struct AviWriter -{ - AviWriter() - : m_frame(NULL) - , m_frameSize(0) - , m_numFrames(0) - , m_width(0) - , m_height(0) - , m_yflip(false) - { - } - - bool open(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _fps, bool _yflip) - { - if (0 != m_writer.open(_filePath) ) - { - return false; - } - - m_frameSize = _width * _height * 3; - m_frame = new uint8_t[m_frameSize + 8]; - m_numFrames = 0; - m_width = _width; - m_height = _height; - - // Bgfx returns _yflip true for OpenGL since bottom left corner is 0, 0. In D3D top left corner - // is 0, 0. DIB expect OpenGL style coordinates, so this is inverted logic for AVI writer. - m_yflip = !_yflip; - - bx::StaticMemoryBlockWriter mem(m_frame, 8); - // Stream Data (LIST 'movi' Chunk) http://msdn.microsoft.com/en-us/library/ms899496.aspx - bx::write(&mem, BX_MAKEFOURCC('0', '0', 'd', 'b') ); - bx::write(&mem, m_frameSize); - - bx::write(&m_writer, BX_MAKEFOURCC('R', 'I', 'F', 'F') ); - m_riffSizeOffset = m_writer.seek(); - bx::write(&m_writer, UINT32_C(0) ); - - bx::write(&m_writer, BX_MAKEFOURCC('A', 'V', 'I', ' ') ); - - // AVI RIFF Form http://msdn.microsoft.com/en-us/library/ms899422.aspx - bx::write(&m_writer, BX_MAKEFOURCC('L', 'I', 'S', 'T') ); - bx::write(&m_writer, UINT32_C(192) ); - bx::write(&m_writer, BX_MAKEFOURCC('h', 'd', 'r', 'l') ); - - // AVI Main Header http://msdn.microsoft.com/en-us/library/ms779632.aspx - bx::write(&m_writer, BX_MAKEFOURCC('a', 'v', 'i', 'h') ); - bx::write(&m_writer, UINT32_C(56) ); - bx::write(&m_writer, UINT32_C(0) ); // dwMicroSecPerFrame - bx::write(&m_writer, UINT32_C(0) ); // dwMaxBytesPerSec - bx::write(&m_writer, UINT32_C(0) ); // dwPaddingGranularity - bx::write(&m_writer, UINT32_C(0x101) ); // dwFlags - - m_totalFramesOffset = m_writer.seek(); - bx::write(&m_writer, UINT32_C(0) ); // dwTotalFrames - - bx::write(&m_writer, UINT32_C(0) ); // dwInitialFrames - bx::write(&m_writer, UINT32_C(1) ); // dwStreams - bx::write(&m_writer, UINT32_C(0) ); // dwSuggestedBufferSize - bx::write(&m_writer, _width); // dwWidth - bx::write(&m_writer, _height); // dwHeight - bx::write(&m_writer, UINT32_C(0) ); // dwReserved0 - bx::write(&m_writer, UINT32_C(0) ); // dwReserved1 - bx::write(&m_writer, UINT32_C(0) ); // dwReserved2 - bx::write(&m_writer, UINT32_C(0) ); // dwReserved3 - - bx::write(&m_writer, BX_MAKEFOURCC('L', 'I', 'S', 'T') ); - bx::write(&m_writer, UINT32_C(116) ); - bx::write(&m_writer, BX_MAKEFOURCC('s', 't', 'r', 'l') ); - - // AVISTREAMHEADER Structure http://msdn.microsoft.com/en-us/library/ms779638.aspx - bx::write(&m_writer, BX_MAKEFOURCC('s', 't', 'r', 'h') ); - bx::write(&m_writer, UINT32_C(56) ); - // AVI Stream Headers http://msdn.microsoft.com/en-us/library/ms899423.aspx - bx::write(&m_writer, BX_MAKEFOURCC('v', 'i', 'd', 's') ); // fccType - bx::write(&m_writer, BX_MAKEFOURCC('D', 'I', 'B', ' ') ); // fccHandler - bx::write(&m_writer, UINT32_C(0) ); // dwFlags - bx::write(&m_writer, UINT16_C(0) ); // wPriority - bx::write(&m_writer, UINT16_C(0) ); // wLanguage - bx::write(&m_writer, UINT32_C(0) ); // dwInitialFrames - bx::write(&m_writer, UINT32_C(1) ); // dwScale - bx::write(&m_writer, _fps); // dwRate - bx::write(&m_writer, UINT32_C(0) ); // dwStart - - m_lengthOffset = m_writer.seek(); - bx::write(&m_writer, UINT32_C(0) ); // dwLength - - bx::write(&m_writer, m_frameSize); // dwSuggestedBufferSize - bx::write(&m_writer, UINT32_MAX); // dwQuality - bx::write(&m_writer, UINT32_C(0) ); // dwSampleSize - bx::write(&m_writer, INT16_C(0) ); // rcFrame.left - bx::write(&m_writer, INT16_C(0) ); // rcFrame.top - bx::write(&m_writer, uint16_t(_width) ); // rcFrame.right - bx::write(&m_writer, uint16_t(_height) );// rcFrame.bottom - - bx::write(&m_writer, BX_MAKEFOURCC('s', 't', 'r', 'f') ); - bx::write(&m_writer, UINT32_C(40) ); - - // BITMAPINFOHEADER structure http://msdn.microsoft.com/en-us/library/windows/desktop/dd318229%28v=vs.85%29.aspx - bx::write(&m_writer, UINT32_C(40) ); // biSize - bx::write(&m_writer, _width); // biWidth - bx::write(&m_writer, _height); // biHeight - bx::write(&m_writer, UINT16_C(1) ); // biPlanes - bx::write(&m_writer, UINT16_C(24) ); // biBitCount - bx::write(&m_writer, UINT32_C(0) ); // biCompression - bx::write(&m_writer, m_frameSize); // biSizeImage - bx::write(&m_writer, UINT32_C(0) ); // biXPelsPerMeter - bx::write(&m_writer, UINT32_C(0) ); // biYPelsPerMeter - bx::write(&m_writer, UINT32_C(0) ); // biClrUsed - bx::write(&m_writer, UINT32_C(0) ); // biClrImportant - - bx::write(&m_writer, BX_MAKEFOURCC('L', 'I', 'S', 'T') ); - - m_moviListOffset = m_writer.seek(); - bx::write(&m_writer, UINT32_C(0) ); - bx::write(&m_writer, BX_MAKEFOURCC('m', 'o', 'v', 'i') ); - - return true; - } - - void close() - { - if (NULL != m_frame) - { - int64_t pos = m_writer.seek(); - m_writer.seek(m_moviListOffset, bx::Whence::Begin); - bx::write(&m_writer, uint32_t(pos-m_moviListOffset-4) ); - m_writer.seek(pos, bx::Whence::Begin); - - bx::write(&m_writer, BX_MAKEFOURCC('i', 'd', 'x', '1') ); - bx::write(&m_writer, m_numFrames*16); - - for (uint32_t ii = 0, offset = 4; ii < m_numFrames; ++ii) - { - bx::write(&m_writer, BX_MAKEFOURCC('0', '0', 'd', 'b') ); - bx::write(&m_writer, UINT32_C(16) ); - bx::write(&m_writer, offset); - bx::write(&m_writer, m_frameSize); - offset += m_frameSize + 8; - } - - pos = m_writer.seek(); - m_writer.seek(m_riffSizeOffset, bx::Whence::Begin); - bx::write(&m_writer, uint32_t(pos-m_riffSizeOffset-4) ); - - m_writer.seek(m_totalFramesOffset, bx::Whence::Begin); - bx::write(&m_writer, m_numFrames); - - m_writer.seek(m_lengthOffset, bx::Whence::Begin); - bx::write(&m_writer, m_numFrames); - - m_writer.close(); - - delete [] m_frame; - m_frame = NULL; - m_frameSize = 0; - } - } - - void frame(const void* _data) - { - if (NULL != m_frame) - { - ++m_numFrames; - uint32_t width = m_width; - uint32_t height = m_height; - - uint8_t* bgr = &m_frame[8]; - - if (m_yflip) - { - for (uint32_t yy = 0; yy < height; ++yy) - { - const uint8_t* bgra = (const uint8_t*)_data + (height-1-yy)*width*4; - - for (uint32_t ii = 0; ii < width; ++ii) - { - bgr[0] = bgra[0]; - bgr[1] = bgra[1]; - bgr[2] = bgra[2]; - bgr += 3; - bgra += 4; - } - } - } - else - { - const uint8_t* bgra = (const uint8_t*)_data; - for (uint32_t ii = 0, num = m_frameSize/3; ii < num; ++ii) - { - bgr[0] = bgra[0]; - bgr[1] = bgra[1]; - bgr[2] = bgra[2]; - bgr += 3; - bgra += 4; - } - } - - bx::write(&m_writer, m_frame, m_frameSize+8); - } - } - - bx::CrtFileWriter m_writer; - int64_t m_riffSizeOffset; - int64_t m_totalFramesOffset; - int64_t m_lengthOffset; - int64_t m_moviListOffset; - uint8_t* m_frame; - uint32_t m_frameSize; - uint32_t m_numFrames; - uint32_t m_width; - uint32_t m_height; - bool m_yflip; -}; - -#endif // __AVIWRITER_H__ +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#ifndef __AVIWRITER_H__ +#define __AVIWRITER_H__ + +#include <bx/readerwriter.h> + +// Simple AVI writer. VideoLAN and VirtualDub can decode it. +// Needs some bits to get jiggled to work with other players. But it's good +// enough for an example. +struct AviWriter +{ + AviWriter() + : m_frame(NULL) + , m_frameSize(0) + , m_numFrames(0) + , m_width(0) + , m_height(0) + , m_yflip(false) + { + } + + bool open(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _fps, bool _yflip) + { + if (0 != m_writer.open(_filePath) ) + { + return false; + } + + m_frameSize = _width * _height * 3; + m_frame = new uint8_t[m_frameSize + 8]; + m_numFrames = 0; + m_width = _width; + m_height = _height; + + // Bgfx returns _yflip true for OpenGL since bottom left corner is 0, 0. In D3D top left corner + // is 0, 0. DIB expect OpenGL style coordinates, so this is inverted logic for AVI writer. + m_yflip = !_yflip; + + bx::StaticMemoryBlockWriter mem(m_frame, 8); + // Stream Data (LIST 'movi' Chunk) http://msdn.microsoft.com/en-us/library/ms899496.aspx + bx::write(&mem, BX_MAKEFOURCC('0', '0', 'd', 'b') ); + bx::write(&mem, m_frameSize); + + bx::write(&m_writer, BX_MAKEFOURCC('R', 'I', 'F', 'F') ); + m_riffSizeOffset = m_writer.seek(); + bx::write(&m_writer, UINT32_C(0) ); + + bx::write(&m_writer, BX_MAKEFOURCC('A', 'V', 'I', ' ') ); + + // AVI RIFF Form http://msdn.microsoft.com/en-us/library/ms899422.aspx + bx::write(&m_writer, BX_MAKEFOURCC('L', 'I', 'S', 'T') ); + bx::write(&m_writer, UINT32_C(192) ); + bx::write(&m_writer, BX_MAKEFOURCC('h', 'd', 'r', 'l') ); + + // AVI Main Header http://msdn.microsoft.com/en-us/library/ms779632.aspx + bx::write(&m_writer, BX_MAKEFOURCC('a', 'v', 'i', 'h') ); + bx::write(&m_writer, UINT32_C(56) ); + bx::write(&m_writer, UINT32_C(0) ); // dwMicroSecPerFrame + bx::write(&m_writer, UINT32_C(0) ); // dwMaxBytesPerSec + bx::write(&m_writer, UINT32_C(0) ); // dwPaddingGranularity + bx::write(&m_writer, UINT32_C(0x101) ); // dwFlags + + m_totalFramesOffset = m_writer.seek(); + bx::write(&m_writer, UINT32_C(0) ); // dwTotalFrames + + bx::write(&m_writer, UINT32_C(0) ); // dwInitialFrames + bx::write(&m_writer, UINT32_C(1) ); // dwStreams + bx::write(&m_writer, UINT32_C(0) ); // dwSuggestedBufferSize + bx::write(&m_writer, _width); // dwWidth + bx::write(&m_writer, _height); // dwHeight + bx::write(&m_writer, UINT32_C(0) ); // dwReserved0 + bx::write(&m_writer, UINT32_C(0) ); // dwReserved1 + bx::write(&m_writer, UINT32_C(0) ); // dwReserved2 + bx::write(&m_writer, UINT32_C(0) ); // dwReserved3 + + bx::write(&m_writer, BX_MAKEFOURCC('L', 'I', 'S', 'T') ); + bx::write(&m_writer, UINT32_C(116) ); + bx::write(&m_writer, BX_MAKEFOURCC('s', 't', 'r', 'l') ); + + // AVISTREAMHEADER Structure http://msdn.microsoft.com/en-us/library/ms779638.aspx + bx::write(&m_writer, BX_MAKEFOURCC('s', 't', 'r', 'h') ); + bx::write(&m_writer, UINT32_C(56) ); + // AVI Stream Headers http://msdn.microsoft.com/en-us/library/ms899423.aspx + bx::write(&m_writer, BX_MAKEFOURCC('v', 'i', 'd', 's') ); // fccType + bx::write(&m_writer, BX_MAKEFOURCC('D', 'I', 'B', ' ') ); // fccHandler + bx::write(&m_writer, UINT32_C(0) ); // dwFlags + bx::write(&m_writer, UINT16_C(0) ); // wPriority + bx::write(&m_writer, UINT16_C(0) ); // wLanguage + bx::write(&m_writer, UINT32_C(0) ); // dwInitialFrames + bx::write(&m_writer, UINT32_C(1) ); // dwScale + bx::write(&m_writer, _fps); // dwRate + bx::write(&m_writer, UINT32_C(0) ); // dwStart + + m_lengthOffset = m_writer.seek(); + bx::write(&m_writer, UINT32_C(0) ); // dwLength + + bx::write(&m_writer, m_frameSize); // dwSuggestedBufferSize + bx::write(&m_writer, UINT32_MAX); // dwQuality + bx::write(&m_writer, UINT32_C(0) ); // dwSampleSize + bx::write(&m_writer, INT16_C(0) ); // rcFrame.left + bx::write(&m_writer, INT16_C(0) ); // rcFrame.top + bx::write(&m_writer, uint16_t(_width) ); // rcFrame.right + bx::write(&m_writer, uint16_t(_height) );// rcFrame.bottom + + bx::write(&m_writer, BX_MAKEFOURCC('s', 't', 'r', 'f') ); + bx::write(&m_writer, UINT32_C(40) ); + + // BITMAPINFOHEADER structure http://msdn.microsoft.com/en-us/library/windows/desktop/dd318229%28v=vs.85%29.aspx + bx::write(&m_writer, UINT32_C(40) ); // biSize + bx::write(&m_writer, _width); // biWidth + bx::write(&m_writer, _height); // biHeight + bx::write(&m_writer, UINT16_C(1) ); // biPlanes + bx::write(&m_writer, UINT16_C(24) ); // biBitCount + bx::write(&m_writer, UINT32_C(0) ); // biCompression + bx::write(&m_writer, m_frameSize); // biSizeImage + bx::write(&m_writer, UINT32_C(0) ); // biXPelsPerMeter + bx::write(&m_writer, UINT32_C(0) ); // biYPelsPerMeter + bx::write(&m_writer, UINT32_C(0) ); // biClrUsed + bx::write(&m_writer, UINT32_C(0) ); // biClrImportant + + bx::write(&m_writer, BX_MAKEFOURCC('L', 'I', 'S', 'T') ); + + m_moviListOffset = m_writer.seek(); + bx::write(&m_writer, UINT32_C(0) ); + bx::write(&m_writer, BX_MAKEFOURCC('m', 'o', 'v', 'i') ); + + return true; + } + + void close() + { + if (NULL != m_frame) + { + int64_t pos = m_writer.seek(); + m_writer.seek(m_moviListOffset, bx::Whence::Begin); + bx::write(&m_writer, uint32_t(pos-m_moviListOffset-4) ); + m_writer.seek(pos, bx::Whence::Begin); + + bx::write(&m_writer, BX_MAKEFOURCC('i', 'd', 'x', '1') ); + bx::write(&m_writer, m_numFrames*16); + + for (uint32_t ii = 0, offset = 4; ii < m_numFrames; ++ii) + { + bx::write(&m_writer, BX_MAKEFOURCC('0', '0', 'd', 'b') ); + bx::write(&m_writer, UINT32_C(16) ); + bx::write(&m_writer, offset); + bx::write(&m_writer, m_frameSize); + offset += m_frameSize + 8; + } + + pos = m_writer.seek(); + m_writer.seek(m_riffSizeOffset, bx::Whence::Begin); + bx::write(&m_writer, uint32_t(pos-m_riffSizeOffset-4) ); + + m_writer.seek(m_totalFramesOffset, bx::Whence::Begin); + bx::write(&m_writer, m_numFrames); + + m_writer.seek(m_lengthOffset, bx::Whence::Begin); + bx::write(&m_writer, m_numFrames); + + m_writer.close(); + + delete [] m_frame; + m_frame = NULL; + m_frameSize = 0; + } + } + + void frame(const void* _data) + { + if (NULL != m_frame) + { + ++m_numFrames; + uint32_t width = m_width; + uint32_t height = m_height; + + uint8_t* bgr = &m_frame[8]; + + if (m_yflip) + { + for (uint32_t yy = 0; yy < height; ++yy) + { + const uint8_t* bgra = (const uint8_t*)_data + (height-1-yy)*width*4; + + for (uint32_t ii = 0; ii < width; ++ii) + { + bgr[0] = bgra[0]; + bgr[1] = bgra[1]; + bgr[2] = bgra[2]; + bgr += 3; + bgra += 4; + } + } + } + else + { + const uint8_t* bgra = (const uint8_t*)_data; + for (uint32_t ii = 0, num = m_frameSize/3; ii < num; ++ii) + { + bgr[0] = bgra[0]; + bgr[1] = bgra[1]; + bgr[2] = bgra[2]; + bgr += 3; + bgra += 4; + } + } + + bx::write(&m_writer, m_frame, m_frameSize+8); + } + } + + bx::CrtFileWriter m_writer; + int64_t m_riffSizeOffset; + int64_t m_totalFramesOffset; + int64_t m_lengthOffset; + int64_t m_moviListOffset; + uint8_t* m_frame; + uint32_t m_frameSize; + uint32_t m_numFrames; + uint32_t m_width; + uint32_t m_height; + bool m_yflip; +}; + +#endif // __AVIWRITER_H__ diff --git a/examples/common/common.sh b/examples/common/common.sh index cf704e68..c0566ab0 100644 --- a/examples/common/common.sh +++ b/examples/common/common.sh @@ -1,7 +1,7 @@ -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include "../../src/common.sh" -#include "shaderlib.sh" +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "../../src/common.sh" +#include "shaderlib.sh" diff --git a/examples/common/dbg.cpp b/examples/common/dbg.cpp index c807063a..2f03092d 100755 --- a/examples/common/dbg.cpp +++ b/examples/common/dbg.cpp @@ -1,109 +1,109 @@ -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include <bx/bx.h> - -#include <stdio.h> -#include <stdint.h> -#include <inttypes.h> -#include <ctype.h> // isprint - -#include "dbg.h" -#include <bx/string.h> - -#if BX_COMPILER_MSVC -# define snprintf _snprintf -#endif // BX_COMPILER_MSVC - -#if BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360 -extern "C" -{ - __declspec(dllimport) void __stdcall OutputDebugStringA(const char* _str); -} -#endif // BX_PLATFORM_WINDOWS - -void dbgOutput(const char* _out) -{ -#if BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360 - OutputDebugStringA(_out); -#elif BX_PLATFORM_NACL || BX_PLATFORM_LINUX || BX_PLATFORM_OSX - fputs(_out, stderr); - fflush(stderr); -#endif // BX_PLATFORM_ -} - -void dbgPrintfVargs(const char* _format, va_list _argList) -{ - char temp[8192]; - char* out = temp; - int32_t len = bx::vsnprintf(out, sizeof(temp), _format, _argList); - if ( (int32_t)sizeof(temp) < len) - { - out = (char*)alloca(len+1); - len = bx::vsnprintf(out, len, _format, _argList); - } - out[len] = '\0'; - dbgOutput(out); -} - -void dbgPrintf(const char* _format, ...) -{ - va_list argList; - va_start(argList, _format); - dbgPrintfVargs(_format, argList); - va_end(argList); -} - -#define DBG_ADDRESS "%" PRIxPTR - -void dbgPrintfData(const void* _data, uint32_t _size, const char* _format, ...) -{ -#define HEX_DUMP_WIDTH 16 -#define HEX_DUMP_SPACE_WIDTH 48 -#define HEX_DUMP_FORMAT "%-" DBG_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "." DBG_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "s" - - va_list argList; - va_start(argList, _format); - dbgPrintfVargs(_format, argList); - va_end(argList); - - dbgPrintf("\ndata: " DBG_ADDRESS ", size: %d\n", _data, _size); - - if (NULL != _data) - { - const uint8_t* data = reinterpret_cast<const uint8_t*>(_data); - char hex[HEX_DUMP_WIDTH*3+1]; - char ascii[HEX_DUMP_WIDTH+1]; - uint32_t hexPos = 0; - uint32_t asciiPos = 0; - for (uint32_t ii = 0; ii < _size; ++ii) - { - snprintf(&hex[hexPos], sizeof(hex)-hexPos, "%02x ", data[asciiPos]); - hexPos += 3; - - ascii[asciiPos] = isprint(data[asciiPos]) ? data[asciiPos] : '.'; - asciiPos++; - - if (HEX_DUMP_WIDTH == asciiPos) - { - ascii[asciiPos] = '\0'; - dbgPrintf("\t" DBG_ADDRESS "\t" HEX_DUMP_FORMAT "\t%s\n", data, hex, ascii); - data += asciiPos; - hexPos = 0; - asciiPos = 0; - } - } - - if (0 != asciiPos) - { - ascii[asciiPos] = '\0'; - dbgPrintf("\t" DBG_ADDRESS "\t" HEX_DUMP_FORMAT "\t%s\n", data, hex, ascii); - } - } - -#undef HEX_DUMP_WIDTH -#undef HEX_DUMP_SPACE_WIDTH -#undef HEX_DUMP_FORMAT -} +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include <bx/bx.h> + +#include <stdio.h> +#include <stdint.h> +#include <inttypes.h> +#include <ctype.h> // isprint + +#include "dbg.h" +#include <bx/string.h> + +#if BX_COMPILER_MSVC +# define snprintf _snprintf +#endif // BX_COMPILER_MSVC + +#if BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360 +extern "C" +{ + __declspec(dllimport) void __stdcall OutputDebugStringA(const char* _str); +} +#endif // BX_PLATFORM_WINDOWS + +void dbgOutput(const char* _out) +{ +#if BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360 + OutputDebugStringA(_out); +#elif BX_PLATFORM_NACL || BX_PLATFORM_LINUX || BX_PLATFORM_OSX + fputs(_out, stderr); + fflush(stderr); +#endif // BX_PLATFORM_ +} + +void dbgPrintfVargs(const char* _format, va_list _argList) +{ + char temp[8192]; + char* out = temp; + int32_t len = bx::vsnprintf(out, sizeof(temp), _format, _argList); + if ( (int32_t)sizeof(temp) < len) + { + out = (char*)alloca(len+1); + len = bx::vsnprintf(out, len, _format, _argList); + } + out[len] = '\0'; + dbgOutput(out); +} + +void dbgPrintf(const char* _format, ...) +{ + va_list argList; + va_start(argList, _format); + dbgPrintfVargs(_format, argList); + va_end(argList); +} + +#define DBG_ADDRESS "%" PRIxPTR + +void dbgPrintfData(const void* _data, uint32_t _size, const char* _format, ...) +{ +#define HEX_DUMP_WIDTH 16 +#define HEX_DUMP_SPACE_WIDTH 48 +#define HEX_DUMP_FORMAT "%-" DBG_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "." DBG_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "s" + + va_list argList; + va_start(argList, _format); + dbgPrintfVargs(_format, argList); + va_end(argList); + + dbgPrintf("\ndata: " DBG_ADDRESS ", size: %d\n", _data, _size); + + if (NULL != _data) + { + const uint8_t* data = reinterpret_cast<const uint8_t*>(_data); + char hex[HEX_DUMP_WIDTH*3+1]; + char ascii[HEX_DUMP_WIDTH+1]; + uint32_t hexPos = 0; + uint32_t asciiPos = 0; + for (uint32_t ii = 0; ii < _size; ++ii) + { + snprintf(&hex[hexPos], sizeof(hex)-hexPos, "%02x ", data[asciiPos]); + hexPos += 3; + + ascii[asciiPos] = isprint(data[asciiPos]) ? data[asciiPos] : '.'; + asciiPos++; + + if (HEX_DUMP_WIDTH == asciiPos) + { + ascii[asciiPos] = '\0'; + dbgPrintf("\t" DBG_ADDRESS "\t" HEX_DUMP_FORMAT "\t%s\n", data, hex, ascii); + data += asciiPos; + hexPos = 0; + asciiPos = 0; + } + } + + if (0 != asciiPos) + { + ascii[asciiPos] = '\0'; + dbgPrintf("\t" DBG_ADDRESS "\t" HEX_DUMP_FORMAT "\t%s\n", data, hex, ascii); + } + } + +#undef HEX_DUMP_WIDTH +#undef HEX_DUMP_SPACE_WIDTH +#undef HEX_DUMP_FORMAT +} diff --git a/examples/common/dbg.h b/examples/common/dbg.h index c14ad52b..b4c8e6be 100644 --- a/examples/common/dbg.h +++ b/examples/common/dbg.h @@ -1,21 +1,21 @@ -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#ifndef __DBG_H__ -#define __DBG_H__ - -#include <stdarg.h> // va_list - -#define DBG_STRINGIZE(_x) DBG_STRINGIZE_(_x) -#define DBG_STRINGIZE_(_x) #_x -#define DBG_FILE_LINE_LITERAL "" __FILE__ "(" DBG_STRINGIZE(__LINE__) "): " -#define DBG(_format, ...) dbgPrintf(DBG_FILE_LINE_LITERAL "" _format "\n", ##__VA_ARGS__) - -extern void dbgOutput(const char* _out); -extern void dbgPrintfVargs(const char* _format, va_list _argList); -extern void dbgPrintf(const char* _format, ...); -extern void dbgPrintfData(const void* _data, uint32_t _size, const char* _format, ...); - -#endif // __DBG_H__ +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#ifndef __DBG_H__ +#define __DBG_H__ + +#include <stdarg.h> // va_list + +#define DBG_STRINGIZE(_x) DBG_STRINGIZE_(_x) +#define DBG_STRINGIZE_(_x) #_x +#define DBG_FILE_LINE_LITERAL "" __FILE__ "(" DBG_STRINGIZE(__LINE__) "): " +#define DBG(_format, ...) dbgPrintf(DBG_FILE_LINE_LITERAL "" _format "\n", ##__VA_ARGS__) + +extern void dbgOutput(const char* _out); +extern void dbgPrintfVargs(const char* _format, va_list _argList); +extern void dbgPrintf(const char* _format, ...); +extern void dbgPrintfData(const void* _data, uint32_t _size, const char* _format, ...); + +#endif // __DBG_H__ diff --git a/examples/common/entry.h b/examples/common/entry.h index e528492b..86f3c62d 100644 --- a/examples/common/entry.h +++ b/examples/common/entry.h @@ -1,166 +1,166 @@ -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#ifndef __ENTRY_H__ -#define __ENTRY_H__ - -namespace entry -{ - struct MouseButton - { - enum Enum - { - None, - Left, - Middle, - Right, - - Count - }; - }; - - struct Modifier - { - enum Enum - { - None = 0, - LeftAlt = 0x01, - RightAlt = 0x02, - LeftCtrl = 0x04, - RightCtrl = 0x08, - LeftShift = 0x10, - RightShift = 0x20, - LeftMeta = 0x40, - RightMeta = 0x80, - }; - }; - - struct Key - { - enum Enum - { - None = 0, - Esc, - Return, - Tab, - Space, - Backspace, - Up, - Down, - Left, - Right, - PageUp, - PageDown, - Home, - End, - Print, - Plus, - Minus, - F1, - F2, - F3, - F4, - F5, - F6, - F7, - F8, - F9, - F10, - F11, - F12, - NumPad0, - NumPad1, - NumPad2, - NumPad3, - NumPad4, - NumPad5, - NumPad6, - NumPad7, - NumPad8, - NumPad9, - Key0, - Key1, - Key2, - Key3, - Key4, - Key5, - Key6, - Key7, - Key8, - Key9, - KeyA, - KeyB, - KeyC, - KeyD, - KeyE, - KeyF, - KeyG, - KeyH, - KeyI, - KeyJ, - KeyK, - KeyL, - KeyM, - KeyN, - KeyO, - KeyP, - KeyQ, - KeyR, - KeyS, - KeyT, - KeyU, - KeyV, - KeyW, - KeyX, - KeyY, - KeyZ, - }; - }; - - struct Event - { - enum Enum - { - Exit, - Key, - Mouse, - Size, - }; - - Event::Enum m_type; - }; - - struct KeyEvent : public Event - { - Key::Enum m_key; - uint8_t m_modifiers; - bool m_down; - }; - - struct MouseEvent : public Event - { - int32_t m_mx; - int32_t m_my; - MouseButton::Enum m_button; - bool m_down; - bool m_move; - }; - - struct SizeEvent : public Event - { - uint32_t m_width; - uint32_t m_height; - }; - - const Event* poll(); - void release(const Event* _event); - - void setWindowSize(uint32_t _width, uint32_t _height); - void toggleWindowFrame(); - void setMouseLock(bool _lock); - -} // namespace entry - -#endif // __ENTRY_H__ +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#ifndef __ENTRY_H__ +#define __ENTRY_H__ + +namespace entry +{ + struct MouseButton + { + enum Enum + { + None, + Left, + Middle, + Right, + + Count + }; + }; + + struct Modifier + { + enum Enum + { + None = 0, + LeftAlt = 0x01, + RightAlt = 0x02, + LeftCtrl = 0x04, + RightCtrl = 0x08, + LeftShift = 0x10, + RightShift = 0x20, + LeftMeta = 0x40, + RightMeta = 0x80, + }; + }; + + struct Key + { + enum Enum + { + None = 0, + Esc, + Return, + Tab, + Space, + Backspace, + Up, + Down, + Left, + Right, + PageUp, + PageDown, + Home, + End, + Print, + Plus, + Minus, + F1, + F2, + F3, + F4, + F5, + F6, + F7, + F8, + F9, + F10, + F11, + F12, + NumPad0, + NumPad1, + NumPad2, + NumPad3, + NumPad4, + NumPad5, + NumPad6, + NumPad7, + NumPad8, + NumPad9, + Key0, + Key1, + Key2, + Key3, + Key4, + Key5, + Key6, + Key7, + Key8, + Key9, + KeyA, + KeyB, + KeyC, + KeyD, + KeyE, + KeyF, + KeyG, + KeyH, + KeyI, + KeyJ, + KeyK, + KeyL, + KeyM, + KeyN, + KeyO, + KeyP, + KeyQ, + KeyR, + KeyS, + KeyT, + KeyU, + KeyV, + KeyW, + KeyX, + KeyY, + KeyZ, + }; + }; + + struct Event + { + enum Enum + { + Exit, + Key, + Mouse, + Size, + }; + + Event::Enum m_type; + }; + + struct KeyEvent : public Event + { + Key::Enum m_key; + uint8_t m_modifiers; + bool m_down; + }; + + struct MouseEvent : public Event + { + int32_t m_mx; + int32_t m_my; + MouseButton::Enum m_button; + bool m_down; + bool m_move; + }; + + struct SizeEvent : public Event + { + uint32_t m_width; + uint32_t m_height; + }; + + const Event* poll(); + void release(const Event* _event); + + void setWindowSize(uint32_t _width, uint32_t _height); + void toggleWindowFrame(); + void setMouseLock(bool _lock); + +} // namespace entry + +#endif // __ENTRY_H__ diff --git a/examples/common/entry_emscripten.cpp b/examples/common/entry_emscripten.cpp index 3bd38e84..db96faf1 100644 --- a/examples/common/entry_emscripten.cpp +++ b/examples/common/entry_emscripten.cpp @@ -1,20 +1,20 @@ -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include <bx/bx.h> - -#if BX_PLATFORM_EMSCRIPTEN - -#include <emscripten/emscripten.h> -#include <alloca.h> -#include <setjmp.h> - -#include "entry.h" - -namespace entry -{ +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include <bx/bx.h> + +#if BX_PLATFORM_EMSCRIPTEN + +#include <emscripten/emscripten.h> +#include <alloca.h> +#include <setjmp.h> + +#include "entry.h" + +namespace entry +{ const Event* poll() { return NULL; @@ -24,50 +24,50 @@ namespace entry { } - void setWindowSize(uint32_t _width, uint32_t _height) - { + void setWindowSize(uint32_t _width, uint32_t _height) + { } void toggleWindowFrame() - { + { } void setMouseLock(bool _lock) { } -} // namespace entry - -extern int _main_(int _argc, char** _argv); - -static jmp_buf s_main; -static jmp_buf s_loop; - -void emscripten_yield() -{ - if (!setjmp(s_main) ) - { - longjmp(s_loop, 1); - } -} - -void loop() -{ - if (!setjmp(s_loop) ) - { - longjmp(s_main, 1); - } -} - -int main(int _argc, char** _argv) -{ - if (!setjmp(s_loop) ) - { - alloca(16<<10); - _main_(_argc, _argv); - } - - emscripten_set_main_loop(loop, 10, true); -} - -#endif // BX_PLATFORM_EMSCRIPTEN +} // namespace entry + +extern int _main_(int _argc, char** _argv); + +static jmp_buf s_main; +static jmp_buf s_loop; + +void emscripten_yield() +{ + if (!setjmp(s_main) ) + { + longjmp(s_loop, 1); + } +} + +void loop() +{ + if (!setjmp(s_loop) ) + { + longjmp(s_main, 1); + } +} + +int main(int _argc, char** _argv) +{ + if (!setjmp(s_loop) ) + { + alloca(16<<10); + _main_(_argc, _argv); + } + + emscripten_set_main_loop(loop, 10, true); +} + +#endif // BX_PLATFORM_EMSCRIPTEN diff --git a/examples/common/entry_nacl.cpp b/examples/common/entry_nacl.cpp index cddbe069..dc78395d 100644 --- a/examples/common/entry_nacl.cpp +++ b/examples/common/entry_nacl.cpp @@ -1,32 +1,32 @@ -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include <bx/bx.h> - -#if BX_PLATFORM_NACL - -#include <stdio.h> -#include <string.h> -#include <pthread.h> - -#include <ppapi/c/pp_errors.h> -#include <ppapi/c/pp_module.h> -#include <ppapi/c/ppb.h> -#include <ppapi/c/ppb_graphics_3d.h> -#include <ppapi/c/ppb_instance.h> -#include <ppapi/c/ppp.h> -#include <ppapi/c/ppp_instance.h> -#include <ppapi/gles2/gl2ext_ppapi.h> - -#include <bgfxplatform.h> -#include "dbg.h" - -#include "entry.h" - -namespace entry -{ +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include <bx/bx.h> + +#if BX_PLATFORM_NACL + +#include <stdio.h> +#include <string.h> +#include <pthread.h> + +#include <ppapi/c/pp_errors.h> +#include <ppapi/c/pp_module.h> +#include <ppapi/c/ppb.h> +#include <ppapi/c/ppb_graphics_3d.h> +#include <ppapi/c/ppb_instance.h> +#include <ppapi/c/ppp.h> +#include <ppapi/c/ppp_instance.h> +#include <ppapi/gles2/gl2ext_ppapi.h> + +#include <bgfxplatform.h> +#include "dbg.h" + +#include "entry.h" + +namespace entry +{ const Event* poll() { return NULL; @@ -36,117 +36,117 @@ namespace entry { } - void setWindowSize(uint32_t _width, uint32_t _height) - { + void setWindowSize(uint32_t _width, uint32_t _height) + { } void toggleWindowFrame() - { + { } - + void setMouseLock(bool _lock) { } -} // namespace entry - -extern int _main_(int _argc, char** _argv); - -static void* _entry_(void*) -{ - const char* argv[1] = { "nacl.nexe" }; - _main_(1, const_cast<char**>(argv) ); - return NULL; -} - -struct NaclContext -{ - NaclContext() - : m_thread(0) - { - } - - const PPB_Instance* m_instInterface; - const PPB_Graphics3D* m_graphicsInterface; - - pthread_t m_thread; - PP_Module m_module; - PP_Instance m_instance; -}; - -static NaclContext s_ctx; - -static PP_Bool naclInstanceDidCreate(PP_Instance _instance, uint32_t _argc, const char* _argn[], const char* _argv[]) -{ - s_ctx.m_instance = _instance; - - bgfx::naclSetIntefraces(s_ctx.m_instance, s_ctx.m_instInterface, s_ctx.m_graphicsInterface, NULL); - pthread_create(&s_ctx.m_thread, NULL, _entry_, NULL); - - return PP_TRUE; -} - -static void naclInstanceDidDestroy(PP_Instance _instance) -{ - if (s_ctx.m_thread) - { - pthread_join(s_ctx.m_thread, NULL); - } -} - -static void naclInstanceDidChangeView(PP_Instance _instance, PP_Resource _view) -{ -} - -static void naclInstanceDidChangeFocus(PP_Instance _instance, PP_Bool _focus) -{ -} - -static PP_Bool naclInstanceHandleDocumentLoad(PP_Instance _instance, PP_Resource _urlLoader) -{ - return PP_FALSE; -} - -PP_EXPORT const void* PPP_GetInterface(const char* _name) -{ - if (0 == strcmp(_name, PPP_INSTANCE_INTERFACE) ) - { - static PPP_Instance instanceInterface = - { - &naclInstanceDidCreate, - &naclInstanceDidDestroy, - &naclInstanceDidChangeView, - &naclInstanceDidChangeFocus, - &naclInstanceHandleDocumentLoad, - }; - - return &instanceInterface; - } - - return NULL; -} - -template<typename Type> -bool initializeInterface(const Type*& _result, PPB_GetInterface _interface, const char* _name) -{ - _result = reinterpret_cast<const Type*>(_interface(_name) ); - return NULL != _result; -} - -PP_EXPORT int32_t PPP_InitializeModule(PP_Module _module, PPB_GetInterface _interface) -{ - s_ctx.m_module = _module; - - bool result = true; - result &= initializeInterface(s_ctx.m_instInterface, _interface, PPB_INSTANCE_INTERFACE); - result &= initializeInterface(s_ctx.m_graphicsInterface, _interface, PPB_GRAPHICS_3D_INTERFACE); - result &= glInitializePPAPI(_interface); - - return result ? PP_OK : PP_ERROR_NOINTERFACE; -} - -PP_EXPORT void PPP_ShutdownModule() -{ -} - -#endif // BX_PLATFROM_NACL +} // namespace entry + +extern int _main_(int _argc, char** _argv); + +static void* _entry_(void*) +{ + const char* argv[1] = { "nacl.nexe" }; + _main_(1, const_cast<char**>(argv) ); + return NULL; +} + +struct NaclContext +{ + NaclContext() + : m_thread(0) + { + } + + const PPB_Instance* m_instInterface; + const PPB_Graphics3D* m_graphicsInterface; + + pthread_t m_thread; + PP_Module m_module; + PP_Instance m_instance; +}; + +static NaclContext s_ctx; + +static PP_Bool naclInstanceDidCreate(PP_Instance _instance, uint32_t _argc, const char* _argn[], const char* _argv[]) +{ + s_ctx.m_instance = _instance; + + bgfx::naclSetIntefraces(s_ctx.m_instance, s_ctx.m_instInterface, s_ctx.m_graphicsInterface, NULL); + pthread_create(&s_ctx.m_thread, NULL, _entry_, NULL); + + return PP_TRUE; +} + +static void naclInstanceDidDestroy(PP_Instance _instance) +{ + if (s_ctx.m_thread) + { + pthread_join(s_ctx.m_thread, NULL); + } +} + +static void naclInstanceDidChangeView(PP_Instance _instance, PP_Resource _view) +{ +} + +static void naclInstanceDidChangeFocus(PP_Instance _instance, PP_Bool _focus) +{ +} + +static PP_Bool naclInstanceHandleDocumentLoad(PP_Instance _instance, PP_Resource _urlLoader) +{ + return PP_FALSE; +} + +PP_EXPORT const void* PPP_GetInterface(const char* _name) +{ + if (0 == strcmp(_name, PPP_INSTANCE_INTERFACE) ) + { + static PPP_Instance instanceInterface = + { + &naclInstanceDidCreate, + &naclInstanceDidDestroy, + &naclInstanceDidChangeView, + &naclInstanceDidChangeFocus, + &naclInstanceHandleDocumentLoad, + }; + + return &instanceInterface; + } + + return NULL; +} + +template<typename Type> +bool initializeInterface(const Type*& _result, PPB_GetInterface _interface, const char* _name) +{ + _result = reinterpret_cast<const Type*>(_interface(_name) ); + return NULL != _result; +} + +PP_EXPORT int32_t PPP_InitializeModule(PP_Module _module, PPB_GetInterface _interface) +{ + s_ctx.m_module = _module; + + bool result = true; + result &= initializeInterface(s_ctx.m_instInterface, _interface, PPB_INSTANCE_INTERFACE); + result &= initializeInterface(s_ctx.m_graphicsInterface, _interface, PPB_GRAPHICS_3D_INTERFACE); + result &= glInitializePPAPI(_interface); + + return result ? PP_OK : PP_ERROR_NOINTERFACE; +} + +PP_EXPORT void PPP_ShutdownModule() +{ +} + +#endif // BX_PLATFROM_NACL diff --git a/examples/common/entry_windows.cpp b/examples/common/entry_windows.cpp index b57a8078..56d65c95 100644 --- a/examples/common/entry_windows.cpp +++ b/examples/common/entry_windows.cpp @@ -602,14 +602,14 @@ namespace entry s_ctx.m_eventQueue.release(_event); } - void setWindowSize(uint32_t _width, uint32_t _height) - { - PostMessage(s_ctx.m_hwnd, WM_USER_SET_WINDOW_SIZE, 0, (_height<<16) | (_width&0xffff) ); + void setWindowSize(uint32_t _width, uint32_t _height) + { + PostMessage(s_ctx.m_hwnd, WM_USER_SET_WINDOW_SIZE, 0, (_height<<16) | (_width&0xffff) ); } void toggleWindowFrame() - { - PostMessage(s_ctx.m_hwnd, WM_USER_TOGGLE_WINDOW_FRAME, 0, 0); + { + PostMessage(s_ctx.m_hwnd, WM_USER_TOGGLE_WINDOW_FRAME, 0, 0); } void setMouseLock(bool _lock) diff --git a/examples/common/shaderlib.sh b/examples/common/shaderlib.sh index fd47721e..98a41ba1 100644 --- a/examples/common/shaderlib.sh +++ b/examples/common/shaderlib.sh @@ -1,258 +1,258 @@ -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#ifndef __SHADERLIB_SH__ -#define __SHADERLIB_SH__ - -vec4 encodeRE8(float _r) -{ - float exponent = ceil(log2(_r) ); - return vec4(_r / exp2(exponent) - , 0.0 - , 0.0 - , (exponent + 128.0) / 255.0 - ); -} - -float decodeRE8(vec4 _re8) -{ - float exponent = _re8.w * 255.0 - 128.0; - return _re8.x * exp2(exponent); -} - -vec4 encodeRGBE8(vec3 _rgb) -{ - vec4 rgbe8; - float maxComponent = max(max(_rgb.x, _rgb.y), _rgb.z); - float exponent = ceil(log2(maxComponent) ); - rgbe8.xyz = _rgb / exp2(exponent); - rgbe8.w = (exponent + 128.0) / 255.0; - return rgbe8; -} - -vec3 decodeRGBE8(vec4 _rgbe8) -{ - float exponent = _rgbe8.w * 255.0 - 128.0; - vec3 rgb = _rgbe8.xyz * exp2(exponent); - return rgb; -} - -// Reference: -// RGB/XYZ Matrices -// http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html -vec3 convertRGB2XYZ(vec3 _rgb) -{ - vec3 xyz; - xyz.x = dot(vec3(0.4124564, 0.3575761, 0.1804375), _rgb); - xyz.y = dot(vec3(0.2126729, 0.7151522, 0.0721750), _rgb); - xyz.z = dot(vec3(0.0193339, 0.1191920, 0.9503041), _rgb); - return xyz; -} - -vec3 convertXYZ2RGB(vec3 _xyz) -{ - vec3 rgb; - rgb.x = dot(vec3( 3.2404542, -1.5371385, -0.4985314), _xyz); - rgb.y = dot(vec3(-0.9692660, 1.8760108, 0.0415560), _xyz); - rgb.z = dot(vec3( 0.0556434, -0.2040259, 1.0572252), _xyz); - return rgb; -} - -vec3 convertXYZ2Yxy(vec3 _xyz) -{ - // Reference: - // http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_xyY.html - float inv = 1.0/dot(_xyz, vec3(1.0, 1.0, 1.0) ); - return vec3(_xyz.y, _xyz.x*inv, _xyz.y*inv); -} - -vec3 convertYxy2XYZ(vec3 _Yxy) -{ - // Reference: - // http://www.brucelindbloom.com/index.html?Eqn_xyY_to_XYZ.html - vec3 xyz; - xyz.x = _Yxy.x*_Yxy.y/_Yxy.z; - xyz.y = _Yxy.x; - xyz.z = _Yxy.x*(1.0 - _Yxy.y - _Yxy.z)/_Yxy.z; - return xyz; -} - -vec3 convertRGB2Yxy(vec3 _rgb) -{ - return convertXYZ2Yxy(convertRGB2XYZ(_rgb) ); -} - -vec3 convertYxy2RGB(vec3 _Yxy) -{ - return convertXYZ2RGB(convertYxy2XYZ(_Yxy) ); -} - -vec3 convertRGB2Yuv(vec3 _rgb) -{ - vec3 yuv; - yuv.x = dot(_rgb, vec3(0.299, 0.587, 0.114) ); - yuv.y = (_rgb.x - yuv.x)*0.713 + 0.5; - yuv.z = (_rgb.z - yuv.x)*0.564 + 0.5; - return yuv; -} - -vec3 convertYuv2RGB(vec3 _yuv) -{ - vec3 rgb; - rgb.x = _yuv.x + 1.403*(_yuv.y-0.5); - rgb.y = _yuv.x - 0.344*(_yuv.y-0.5) - 0.714*(_yuv.z-0.5); - rgb.z = _yuv.x + 1.773*(_yuv.z-0.5); - return rgb; -} - -vec3 convertRGB2YIQ(vec3 _rgb) -{ - vec3 yiq; - yiq.x = dot(vec3(0.299, 0.587, 0.114 ), _rgb); - yiq.y = dot(vec3(0.595716, -0.274453, -0.321263), _rgb); - yiq.z = dot(vec3(0.211456, -0.522591, 0.311135), _rgb); - return yiq; -} - -vec3 convertYIQ2RGB(vec3 _yiq) -{ - vec3 rgb; - rgb.x = dot(vec3(1.0, 0.9563, 0.6210), _yiq); - rgb.y = dot(vec3(1.0, -0.2721, -0.6474), _yiq); - rgb.z = dot(vec3(1.0, -1.1070, 1.7046), _yiq); - return rgb; -} - -vec3 toLinear(vec3 _rgb) -{ - return pow(_rgb, vec3_splat(2.2) ); -} - -vec4 toLinear(vec4 _rgba) -{ - return vec4(toLinear(_rgba.xyz), _rgba.w); -} - -vec3 toGamma(vec3 _rgb) -{ - return pow(_rgb, vec3_splat(1.0/2.2) ); -} - -vec4 toGamma(vec4 _rgba) -{ - return vec4(toGamma(_rgba.xyz), _rgba.w); -} - -vec3 toReinhard(vec3 _rgb) -{ - return toGamma(_rgb/(_rgb+vec3_splat(1.0) ) ); -} - -vec4 toReinhard(vec4 _rgba) -{ - return vec4(toReinhard(_rgba.xyz), _rgba.w); -} - -vec3 toFilmic(vec3 _rgb) -{ - _rgb = max(vec3_splat(0.0), _rgb - 0.004); - _rgb = (_rgb*(6.2*_rgb + 0.5) ) / (_rgb*(6.2*_rgb + 1.7) + 0.06); - return _rgb; -} - -vec4 toFilmic(vec4 _rgba) -{ - return vec4(toFilmic(_rgba.xyz), _rgba.w); -} - -vec3 luma(vec3 _rgb) -{ - float yy = dot(vec3(0.2126729, 0.7151522, 0.0721750), _rgb); - return vec3_splat(yy); -} - -vec4 luma(vec4 _rgba) -{ - return vec4(luma(_rgba.xyz), _rgba.w); -} - -vec3 conSatBri(vec3 _rgb, vec3 _csb) -{ - vec3 rgb = _rgb * _csb.z; - rgb = lerp(luma(rgb), rgb, _csb.y); - rgb = lerp(vec3_splat(0.5), rgb, _csb.x); - return rgb; -} - -vec4 conSatBri(vec4 _rgba, vec3 _csb) -{ - return vec4(conSatBri(_rgba.xyz, _csb), _rgba.w); -} - -vec3 posterize(vec3 _rgb, float _numColors) -{ - return floor(_rgb*_numColors) / _numColors; -} - -vec4 posterize(vec4 _rgba, float _numColors) -{ - return vec4(posterize(_rgba.xyz, _numColors), _rgba.w); -} - -vec3 sepia(vec3 _rgb) -{ - vec3 color; - color.x = dot(_rgb, vec3(0.393, 0.769, 0.189) ); - color.y = dot(_rgb, vec3(0.349, 0.686, 0.168) ); - color.z = dot(_rgb, vec3(0.272, 0.534, 0.131) ); - return color; -} - -vec4 sepia(vec4 _rgba) -{ - return vec4(sepia(_rgba.xyz), _rgba.w); -} - -vec3 blendOverlay(vec3 _base, vec3 _blend) -{ - vec3 lt = 2.0 * _base * _blend; - vec3 gte = 1.0 - 2.0 * (1.0 - _base) * (1.0 - _blend); - return lerp(lt, gte, step(vec3_splat(0.5), _base) ); -} - -vec4 blendOverlay(vec4 _base, vec4 _blend) -{ - return vec4(blendOverlay(_base.xyz, _blend.xyz), _base.w); -} - -vec3 adjustHue(vec3 _rgb, float _hue) -{ - vec3 yiq = convertRGB2YIQ(_rgb); - float angle = _hue + atan2(yiq.z, yiq.y); - float len = length(yiq.yz); - return convertYIQ2RGB(vec3(yiq.x, len*cos(angle), len*sin(angle) ) ); -} - -vec4 packFloatToRgba(float _value) -{ - const vec4 shift = vec4(256 * 256 * 256, 256 * 256, 256, 1.0); - const vec4 mask = vec4(0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0); - vec4 comp = frac(_value * shift); - comp -= comp.xxyz * mask; - return comp; -} - -float unpackRgbaToFloat(vec4 _rgba) -{ - const vec4 shift = vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0); - return dot(_rgba, shift); -} - -float random(vec2 _uv) -{ - return frac(sin(dot(_uv.xy, vec2(12.9898, 78.233) ) ) * 43758.5453); -} - -#endif // __SHADERLIB_SH__ +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#ifndef __SHADERLIB_SH__ +#define __SHADERLIB_SH__ + +vec4 encodeRE8(float _r) +{ + float exponent = ceil(log2(_r) ); + return vec4(_r / exp2(exponent) + , 0.0 + , 0.0 + , (exponent + 128.0) / 255.0 + ); +} + +float decodeRE8(vec4 _re8) +{ + float exponent = _re8.w * 255.0 - 128.0; + return _re8.x * exp2(exponent); +} + +vec4 encodeRGBE8(vec3 _rgb) +{ + vec4 rgbe8; + float maxComponent = max(max(_rgb.x, _rgb.y), _rgb.z); + float exponent = ceil(log2(maxComponent) ); + rgbe8.xyz = _rgb / exp2(exponent); + rgbe8.w = (exponent + 128.0) / 255.0; + return rgbe8; +} + +vec3 decodeRGBE8(vec4 _rgbe8) +{ + float exponent = _rgbe8.w * 255.0 - 128.0; + vec3 rgb = _rgbe8.xyz * exp2(exponent); + return rgb; +} + +// Reference: +// RGB/XYZ Matrices +// http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html +vec3 convertRGB2XYZ(vec3 _rgb) +{ + vec3 xyz; + xyz.x = dot(vec3(0.4124564, 0.3575761, 0.1804375), _rgb); + xyz.y = dot(vec3(0.2126729, 0.7151522, 0.0721750), _rgb); + xyz.z = dot(vec3(0.0193339, 0.1191920, 0.9503041), _rgb); + return xyz; +} + +vec3 convertXYZ2RGB(vec3 _xyz) +{ + vec3 rgb; + rgb.x = dot(vec3( 3.2404542, -1.5371385, -0.4985314), _xyz); + rgb.y = dot(vec3(-0.9692660, 1.8760108, 0.0415560), _xyz); + rgb.z = dot(vec3( 0.0556434, -0.2040259, 1.0572252), _xyz); + return rgb; +} + +vec3 convertXYZ2Yxy(vec3 _xyz) +{ + // Reference: + // http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_xyY.html + float inv = 1.0/dot(_xyz, vec3(1.0, 1.0, 1.0) ); + return vec3(_xyz.y, _xyz.x*inv, _xyz.y*inv); +} + +vec3 convertYxy2XYZ(vec3 _Yxy) +{ + // Reference: + // http://www.brucelindbloom.com/index.html?Eqn_xyY_to_XYZ.html + vec3 xyz; + xyz.x = _Yxy.x*_Yxy.y/_Yxy.z; + xyz.y = _Yxy.x; + xyz.z = _Yxy.x*(1.0 - _Yxy.y - _Yxy.z)/_Yxy.z; + return xyz; +} + +vec3 convertRGB2Yxy(vec3 _rgb) +{ + return convertXYZ2Yxy(convertRGB2XYZ(_rgb) ); +} + +vec3 convertYxy2RGB(vec3 _Yxy) +{ + return convertXYZ2RGB(convertYxy2XYZ(_Yxy) ); +} + +vec3 convertRGB2Yuv(vec3 _rgb) +{ + vec3 yuv; + yuv.x = dot(_rgb, vec3(0.299, 0.587, 0.114) ); + yuv.y = (_rgb.x - yuv.x)*0.713 + 0.5; + yuv.z = (_rgb.z - yuv.x)*0.564 + 0.5; + return yuv; +} + +vec3 convertYuv2RGB(vec3 _yuv) +{ + vec3 rgb; + rgb.x = _yuv.x + 1.403*(_yuv.y-0.5); + rgb.y = _yuv.x - 0.344*(_yuv.y-0.5) - 0.714*(_yuv.z-0.5); + rgb.z = _yuv.x + 1.773*(_yuv.z-0.5); + return rgb; +} + +vec3 convertRGB2YIQ(vec3 _rgb) +{ + vec3 yiq; + yiq.x = dot(vec3(0.299, 0.587, 0.114 ), _rgb); + yiq.y = dot(vec3(0.595716, -0.274453, -0.321263), _rgb); + yiq.z = dot(vec3(0.211456, -0.522591, 0.311135), _rgb); + return yiq; +} + +vec3 convertYIQ2RGB(vec3 _yiq) +{ + vec3 rgb; + rgb.x = dot(vec3(1.0, 0.9563, 0.6210), _yiq); + rgb.y = dot(vec3(1.0, -0.2721, -0.6474), _yiq); + rgb.z = dot(vec3(1.0, -1.1070, 1.7046), _yiq); + return rgb; +} + +vec3 toLinear(vec3 _rgb) +{ + return pow(_rgb, vec3_splat(2.2) ); +} + +vec4 toLinear(vec4 _rgba) +{ + return vec4(toLinear(_rgba.xyz), _rgba.w); +} + +vec3 toGamma(vec3 _rgb) +{ + return pow(_rgb, vec3_splat(1.0/2.2) ); +} + +vec4 toGamma(vec4 _rgba) +{ + return vec4(toGamma(_rgba.xyz), _rgba.w); +} + +vec3 toReinhard(vec3 _rgb) +{ + return toGamma(_rgb/(_rgb+vec3_splat(1.0) ) ); +} + +vec4 toReinhard(vec4 _rgba) +{ + return vec4(toReinhard(_rgba.xyz), _rgba.w); +} + +vec3 toFilmic(vec3 _rgb) +{ + _rgb = max(vec3_splat(0.0), _rgb - 0.004); + _rgb = (_rgb*(6.2*_rgb + 0.5) ) / (_rgb*(6.2*_rgb + 1.7) + 0.06); + return _rgb; +} + +vec4 toFilmic(vec4 _rgba) +{ + return vec4(toFilmic(_rgba.xyz), _rgba.w); +} + +vec3 luma(vec3 _rgb) +{ + float yy = dot(vec3(0.2126729, 0.7151522, 0.0721750), _rgb); + return vec3_splat(yy); +} + +vec4 luma(vec4 _rgba) +{ + return vec4(luma(_rgba.xyz), _rgba.w); +} + +vec3 conSatBri(vec3 _rgb, vec3 _csb) +{ + vec3 rgb = _rgb * _csb.z; + rgb = lerp(luma(rgb), rgb, _csb.y); + rgb = lerp(vec3_splat(0.5), rgb, _csb.x); + return rgb; +} + +vec4 conSatBri(vec4 _rgba, vec3 _csb) +{ + return vec4(conSatBri(_rgba.xyz, _csb), _rgba.w); +} + +vec3 posterize(vec3 _rgb, float _numColors) +{ + return floor(_rgb*_numColors) / _numColors; +} + +vec4 posterize(vec4 _rgba, float _numColors) +{ + return vec4(posterize(_rgba.xyz, _numColors), _rgba.w); +} + +vec3 sepia(vec3 _rgb) +{ + vec3 color; + color.x = dot(_rgb, vec3(0.393, 0.769, 0.189) ); + color.y = dot(_rgb, vec3(0.349, 0.686, 0.168) ); + color.z = dot(_rgb, vec3(0.272, 0.534, 0.131) ); + return color; +} + +vec4 sepia(vec4 _rgba) +{ + return vec4(sepia(_rgba.xyz), _rgba.w); +} + +vec3 blendOverlay(vec3 _base, vec3 _blend) +{ + vec3 lt = 2.0 * _base * _blend; + vec3 gte = 1.0 - 2.0 * (1.0 - _base) * (1.0 - _blend); + return lerp(lt, gte, step(vec3_splat(0.5), _base) ); +} + +vec4 blendOverlay(vec4 _base, vec4 _blend) +{ + return vec4(blendOverlay(_base.xyz, _blend.xyz), _base.w); +} + +vec3 adjustHue(vec3 _rgb, float _hue) +{ + vec3 yiq = convertRGB2YIQ(_rgb); + float angle = _hue + atan2(yiq.z, yiq.y); + float len = length(yiq.yz); + return convertYIQ2RGB(vec3(yiq.x, len*cos(angle), len*sin(angle) ) ); +} + +vec4 packFloatToRgba(float _value) +{ + const vec4 shift = vec4(256 * 256 * 256, 256 * 256, 256, 1.0); + const vec4 mask = vec4(0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0); + vec4 comp = frac(_value * shift); + comp -= comp.xxyz * mask; + return comp; +} + +float unpackRgbaToFloat(vec4 _rgba) +{ + const vec4 shift = vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0); + return dot(_rgba, shift); +} + +float random(vec2 _uv) +{ + return frac(sin(dot(_uv.xy, vec2(12.9898, 78.233) ) ) * 43758.5453); +} + +#endif // __SHADERLIB_SH__ diff --git a/src/bgfx_shader.sh b/src/bgfx_shader.sh index e7096c24..65367d1c 100644 --- a/src/bgfx_shader.sh +++ b/src/bgfx_shader.sh @@ -1,161 +1,161 @@ -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#ifndef __BGFX_SHADER_H__ -#define __BGFX_SHADER_H__ - -#ifndef __cplusplus - -#if BGFX_SHADER_LANGUAGE_HLSL -# define dFdx(_x) ddx(_x) -# define dFdy(_y) ddy(-_y) - -# if BGFX_SHADER_LANGUAGE_HLSL > 3 -struct BgfxSampler2D -{ - SamplerState m_sampler; - Texture2D m_texture; -}; - -vec4 bgfxTexture2D(BgfxSampler2D _sampler, vec2 _coord) -{ - return _sampler.m_texture.Sample(_sampler.m_sampler, _coord); -} - -vec4 bgfxTexture2DLod(BgfxSampler2D _sampler, vec2 _coord, float _level) -{ - return _sampler.m_texture.SampleLevel(_sampler.m_sampler, _coord, _level); -} - -struct BgfxSampler3D -{ - SamplerState m_sampler; - Texture3D m_texture; -}; - -vec4 bgfxTexture3D(BgfxSampler3D _sampler, vec3 _coord) -{ - return _sampler.m_texture.Sample(_sampler.m_sampler, _coord); -} - -vec4 bgfxTexture3DLod(BgfxSampler3D _sampler, vec3 _coord, float _level) -{ - return _sampler.m_texture.SampleLevel(_sampler.m_sampler, _coord, _level); -} - -struct BgfxSamplerCube -{ - SamplerState m_sampler; - TextureCube m_texture; -}; - -vec4 bgfxTextureCube(BgfxSamplerCube _sampler, vec3 _coord) -{ - return _sampler.m_texture.Sample(_sampler.m_sampler, _coord); -} - -vec4 bgfxTextureCubeLod(BgfxSamplerCube _sampler, vec3 _coord, float _level) -{ - return _sampler.m_texture.SampleLevel(_sampler.m_sampler, _coord, _level); -} - -# define SAMPLER2D(_name, _reg) \ - uniform SamplerState _name ## Sampler : register(s[_reg]); \ - uniform Texture2D _name ## Texture : register(t[_reg]); \ - static BgfxSampler2D _name = { _name ## Sampler, _name ## Texture } -# define sampler2D BgfxSampler2D -# define texture2D(_sampler, _coord) bgfxTexture2D(_sampler, _coord) -# define texture2DLod(_sampler, _coord, _level) bgfxTexture2DLod(_sampler, _coord, _level) - -# define SAMPLER3D(_name, _reg) \ - uniform SamplerState _name ## Sampler : register(s[_reg]); \ - uniform Texture3D _name ## Texture : register(t[_reg]); \ - static BgfxSampler3D _name = { _name ## Sampler, _name ## Texture } -# define sampler3D BgfxSampler3D -# define texture3D(_sampler, _coord) bgfxTexture3D(_sampler, _coord) -# define texture3DLod(_sampler, _coord, _level) bgfxTexture3DLod(_sampler, _coord, _level) - -# define SAMPLERCUBE(_name, _reg) \ - uniform SamplerState _name ## Sampler : register(s[_reg]); \ - uniform TextureCube _name ## Texture : register(t[_reg]); \ - static BgfxSamplerCube _name = { _name ## Sampler, _name ## Texture } -# define samplerCube BgfxSamplerCube -# define textureCube(_sampler, _coord) bgfxTextureCube(_sampler, _coord) -# define textureCubeLod(_sampler, _coord, _level) bgfxTextureCubeLod(_sampler, _coord, _level) -# else -# define SAMPLER2D(_name, _reg) uniform sampler2D _name : register(s ## _reg) -# define texture2D(_sampler, _coord) tex2D(_sampler, _coord) -# define texture2DLod(_sampler, _coord, _level) tex2Dlod(_sampler, vec3( (_coord).xy, _level) ) -# define SAMPLER3D(_name, _reg) uniform sampler3D _name : register(s ## _reg) -# define texture3D(_sampler, _coord) tex3D(_sampler, _coord) -# define texture3DLod(_sampler, _coord, _level) tex3Dlod(_sampler, vec4( (_coord).xyz, _level) ) -# define SAMPLERCUBE(_name, _reg) uniform samplerCUBE _name : register(s[_reg]) -# define textureCube(_sampler, _coord) texCUBE(_sampler, _coord) -# define textureCubeLod(_sampler, _coord, _level) texCUBElod(_sampler, vec4( (_coord).xyz, _level) ) -# endif // - -# define vec2_splat(_x) float2(_x, _x) -# define vec3_splat(_x) float3(_x, _x, _x) -# define vec4_splat(_x) float4(_x, _x, _x, _x) - -# define bvec2 bool2 -# define bvec3 bool3 -# define bvec4 bool4 - -vec3 instMul(vec3 _vec, mat3 _mtx) { return mul(_mtx, _vec); } -vec3 instMul(mat3 _mtx, vec3 _vec) { return mul(_vec, _mtx); } -vec4 instMul(vec4 _vec, mat4 _mtx) { return mul(_mtx, _vec); } -vec4 instMul(mat4 _mtx, vec4 _vec) { return mul(_vec, _mtx); } - -bvec2 lessThan(vec2 _a, vec2 _b) { return _a < _b; } -bvec3 lessThan(vec3 _a, vec3 _b) { return _a < _b; } -bvec4 lessThan(vec4 _a, vec4 _b) { return _a < _b; } - -bvec2 lessThanEqual(vec2 _a, vec2 _b) { return _a <= _b; } -bvec2 lessThanEqual(vec3 _a, vec3 _b) { return _a <= _b; } -bvec2 lessThanEqual(vec4 _a, vec4 _b) { return _a <= _b; } - -bvec2 greaterThan(vec2 _a, vec2 _b) { return _a > _b; } -bvec3 greaterThan(vec3 _a, vec3 _b) { return _a > _b; } -bvec4 greaterThan(vec4 _a, vec4 _b) { return _a > _b; } - -bvec2 greaterThanEqual(vec2 _a, vec2 _b) { return _a >= _b; } -bvec3 greaterThanEqual(vec3 _a, vec3 _b) { return _a >= _b; } -bvec4 greaterThanEqual(vec4 _a, vec4 _b) { return _a >= _b; } - -bvec2 notEqual(vec2 _a, vec2 _b) { return _a != _b; } -bvec3 notEqual(vec3 _a, vec3 _b) { return _a != _b; } -bvec4 notEqual(vec4 _a, vec4 _b) { return _a != _b; } - -bvec2 equal(vec2 _a, vec2 _b) { return _a == _b; } -bvec3 equal(vec3 _a, vec3 _b) { return _a == _b; } -bvec4 equal(vec4 _a, vec4 _b) { return _a == _b; } - -vec2 mix(vec2 _a, vec2 _b, vec2 _t) { return lerp(_a, _b, _t); } -vec3 mix(vec3 _a, vec3 _b, vec3 _t) { return lerp(_a, _b, _t); } -vec4 mix(vec4 _a, vec4 _b, vec4 _t) { return lerp(_a, _b, _t); } - -#elif BGFX_SHADER_LANGUAGE_GLSL -# define atan2(_x, _y) atan(_x, _y) -# define frac(_x) fract(_x) -# define lerp(_x, _y, _t) mix(_x, _y, _t) -# define mul(_a, _b) ( (_a) * (_b) ) -# define saturate(_x) clamp(_x, 0.0, 1.0) -# define SAMPLER2D(_name, _reg) uniform sampler2D _name -# define SAMPLER3D(_name, _reg) uniform sampler3D _name -# define SAMPLERCUBE(_name, _reg) uniform samplerCube _name -# define vec2_splat(_x) vec2(_x) -# define vec3_splat(_x) vec3(_x) -# define vec4_splat(_x) vec4(_x) - -vec3 instMul(vec3 _vec, mat3 _mtx) { return mul(_vec, _mtx); } -vec3 instMul(mat3 _mtx, vec3 _vec) { return mul(_mtx, _vec); } -vec4 instMul(vec4 _vec, mat4 _mtx) { return mul(_vec, _mtx); } -vec4 instMul(mat4 _mtx, vec4 _vec) { return mul(_mtx, _vec); } -#endif // BGFX_SHADER_LANGUAGE_HLSL - -#endif // __cplusplus - -#endif // __BGFX_SHADER_H__ +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#ifndef __BGFX_SHADER_H__ +#define __BGFX_SHADER_H__ + +#ifndef __cplusplus + +#if BGFX_SHADER_LANGUAGE_HLSL +# define dFdx(_x) ddx(_x) +# define dFdy(_y) ddy(-_y) + +# if BGFX_SHADER_LANGUAGE_HLSL > 3 +struct BgfxSampler2D +{ + SamplerState m_sampler; + Texture2D m_texture; +}; + +vec4 bgfxTexture2D(BgfxSampler2D _sampler, vec2 _coord) +{ + return _sampler.m_texture.Sample(_sampler.m_sampler, _coord); +} + +vec4 bgfxTexture2DLod(BgfxSampler2D _sampler, vec2 _coord, float _level) +{ + return _sampler.m_texture.SampleLevel(_sampler.m_sampler, _coord, _level); +} + +struct BgfxSampler3D +{ + SamplerState m_sampler; + Texture3D m_texture; +}; + +vec4 bgfxTexture3D(BgfxSampler3D _sampler, vec3 _coord) +{ + return _sampler.m_texture.Sample(_sampler.m_sampler, _coord); +} + +vec4 bgfxTexture3DLod(BgfxSampler3D _sampler, vec3 _coord, float _level) +{ + return _sampler.m_texture.SampleLevel(_sampler.m_sampler, _coord, _level); +} + +struct BgfxSamplerCube +{ + SamplerState m_sampler; + TextureCube m_texture; +}; + +vec4 bgfxTextureCube(BgfxSamplerCube _sampler, vec3 _coord) +{ + return _sampler.m_texture.Sample(_sampler.m_sampler, _coord); +} + +vec4 bgfxTextureCubeLod(BgfxSamplerCube _sampler, vec3 _coord, float _level) +{ + return _sampler.m_texture.SampleLevel(_sampler.m_sampler, _coord, _level); +} + +# define SAMPLER2D(_name, _reg) \ + uniform SamplerState _name ## Sampler : register(s[_reg]); \ + uniform Texture2D _name ## Texture : register(t[_reg]); \ + static BgfxSampler2D _name = { _name ## Sampler, _name ## Texture } +# define sampler2D BgfxSampler2D +# define texture2D(_sampler, _coord) bgfxTexture2D(_sampler, _coord) +# define texture2DLod(_sampler, _coord, _level) bgfxTexture2DLod(_sampler, _coord, _level) + +# define SAMPLER3D(_name, _reg) \ + uniform SamplerState _name ## Sampler : register(s[_reg]); \ + uniform Texture3D _name ## Texture : register(t[_reg]); \ + static BgfxSampler3D _name = { _name ## Sampler, _name ## Texture } +# define sampler3D BgfxSampler3D +# define texture3D(_sampler, _coord) bgfxTexture3D(_sampler, _coord) +# define texture3DLod(_sampler, _coord, _level) bgfxTexture3DLod(_sampler, _coord, _level) + +# define SAMPLERCUBE(_name, _reg) \ + uniform SamplerState _name ## Sampler : register(s[_reg]); \ + uniform TextureCube _name ## Texture : register(t[_reg]); \ + static BgfxSamplerCube _name = { _name ## Sampler, _name ## Texture } +# define samplerCube BgfxSamplerCube +# define textureCube(_sampler, _coord) bgfxTextureCube(_sampler, _coord) +# define textureCubeLod(_sampler, _coord, _level) bgfxTextureCubeLod(_sampler, _coord, _level) +# else +# define SAMPLER2D(_name, _reg) uniform sampler2D _name : register(s ## _reg) +# define texture2D(_sampler, _coord) tex2D(_sampler, _coord) +# define texture2DLod(_sampler, _coord, _level) tex2Dlod(_sampler, vec3( (_coord).xy, _level) ) +# define SAMPLER3D(_name, _reg) uniform sampler3D _name : register(s ## _reg) +# define texture3D(_sampler, _coord) tex3D(_sampler, _coord) +# define texture3DLod(_sampler, _coord, _level) tex3Dlod(_sampler, vec4( (_coord).xyz, _level) ) +# define SAMPLERCUBE(_name, _reg) uniform samplerCUBE _name : register(s[_reg]) +# define textureCube(_sampler, _coord) texCUBE(_sampler, _coord) +# define textureCubeLod(_sampler, _coord, _level) texCUBElod(_sampler, vec4( (_coord).xyz, _level) ) +# endif // + +# define vec2_splat(_x) float2(_x, _x) +# define vec3_splat(_x) float3(_x, _x, _x) +# define vec4_splat(_x) float4(_x, _x, _x, _x) + +# define bvec2 bool2 +# define bvec3 bool3 +# define bvec4 bool4 + +vec3 instMul(vec3 _vec, mat3 _mtx) { return mul(_mtx, _vec); } +vec3 instMul(mat3 _mtx, vec3 _vec) { return mul(_vec, _mtx); } +vec4 instMul(vec4 _vec, mat4 _mtx) { return mul(_mtx, _vec); } +vec4 instMul(mat4 _mtx, vec4 _vec) { return mul(_vec, _mtx); } + +bvec2 lessThan(vec2 _a, vec2 _b) { return _a < _b; } +bvec3 lessThan(vec3 _a, vec3 _b) { return _a < _b; } +bvec4 lessThan(vec4 _a, vec4 _b) { return _a < _b; } + +bvec2 lessThanEqual(vec2 _a, vec2 _b) { return _a <= _b; } +bvec2 lessThanEqual(vec3 _a, vec3 _b) { return _a <= _b; } +bvec2 lessThanEqual(vec4 _a, vec4 _b) { return _a <= _b; } + +bvec2 greaterThan(vec2 _a, vec2 _b) { return _a > _b; } +bvec3 greaterThan(vec3 _a, vec3 _b) { return _a > _b; } +bvec4 greaterThan(vec4 _a, vec4 _b) { return _a > _b; } + +bvec2 greaterThanEqual(vec2 _a, vec2 _b) { return _a >= _b; } +bvec3 greaterThanEqual(vec3 _a, vec3 _b) { return _a >= _b; } +bvec4 greaterThanEqual(vec4 _a, vec4 _b) { return _a >= _b; } + +bvec2 notEqual(vec2 _a, vec2 _b) { return _a != _b; } +bvec3 notEqual(vec3 _a, vec3 _b) { return _a != _b; } +bvec4 notEqual(vec4 _a, vec4 _b) { return _a != _b; } + +bvec2 equal(vec2 _a, vec2 _b) { return _a == _b; } +bvec3 equal(vec3 _a, vec3 _b) { return _a == _b; } +bvec4 equal(vec4 _a, vec4 _b) { return _a == _b; } + +vec2 mix(vec2 _a, vec2 _b, vec2 _t) { return lerp(_a, _b, _t); } +vec3 mix(vec3 _a, vec3 _b, vec3 _t) { return lerp(_a, _b, _t); } +vec4 mix(vec4 _a, vec4 _b, vec4 _t) { return lerp(_a, _b, _t); } + +#elif BGFX_SHADER_LANGUAGE_GLSL +# define atan2(_x, _y) atan(_x, _y) +# define frac(_x) fract(_x) +# define lerp(_x, _y, _t) mix(_x, _y, _t) +# define mul(_a, _b) ( (_a) * (_b) ) +# define saturate(_x) clamp(_x, 0.0, 1.0) +# define SAMPLER2D(_name, _reg) uniform sampler2D _name +# define SAMPLER3D(_name, _reg) uniform sampler3D _name +# define SAMPLERCUBE(_name, _reg) uniform samplerCube _name +# define vec2_splat(_x) vec2(_x) +# define vec3_splat(_x) vec3(_x) +# define vec4_splat(_x) vec4(_x) + +vec3 instMul(vec3 _vec, mat3 _mtx) { return mul(_vec, _mtx); } +vec3 instMul(mat3 _mtx, vec3 _vec) { return mul(_mtx, _vec); } +vec4 instMul(vec4 _vec, mat4 _mtx) { return mul(_vec, _mtx); } +vec4 instMul(mat4 _mtx, vec4 _vec) { return mul(_mtx, _vec); } +#endif // BGFX_SHADER_LANGUAGE_HLSL + +#endif // __cplusplus + +#endif // __BGFX_SHADER_H__ diff --git a/src/common.sh b/src/common.sh index ad412608..350f6776 100644 --- a/src/common.sh +++ b/src/common.sh @@ -1,19 +1,19 @@ -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#ifndef __SHADER_COMMON_H__ -#define __SHADER_COMMON_H__ - -#include "bgfx_shader.sh" - -uniform mat4 u_view; -uniform mat4 u_viewProj; -uniform mat4 u_model; -uniform mat4 u_modelView; -uniform mat4 u_modelViewProj; -uniform mat4 u_modelViewProjX; -uniform mat4 u_viewProjX; - -#endif // __SHADER_COMMON_H__ +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#ifndef __SHADER_COMMON_H__ +#define __SHADER_COMMON_H__ + +#include "bgfx_shader.sh" + +uniform mat4 u_view; +uniform mat4 u_viewProj; +uniform mat4 u_model; +uniform mat4 u_modelView; +uniform mat4 u_modelViewProj; +uniform mat4 u_modelViewProjX; +uniform mat4 u_viewProjX; + +#endif // __SHADER_COMMON_H__ diff --git a/src/config.h b/src/config.h index 995e101f..3d138014 100644 --- a/src/config.h +++ b/src/config.h @@ -1,200 +1,200 @@ -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#ifndef __CONFIG_H__ -#define __CONFIG_H__ - -#if !defined(BGFX_CONFIG_RENDERER_DIRECT3D9) \ - && !defined(BGFX_CONFIG_RENDERER_DIRECT3D11) \ - && !defined(BGFX_CONFIG_RENDERER_OPENGL) \ - && !defined(BGFX_CONFIG_RENDERER_OPENGLES2) \ - && !defined(BGFX_CONFIG_RENDERER_OPENGLES3) \ - && !defined(BGFX_CONFIG_RENDERER_NULL) - -# ifndef BGFX_CONFIG_RENDERER_DIRECT3D9 -# define BGFX_CONFIG_RENDERER_DIRECT3D9 (0 \ - | (BX_PLATFORM_WINDOWS && _WIN32_WINNT < 0x0602 /*_WIN32_WINNT_WIN8*/) \ - | BX_PLATFORM_XBOX360 \ - ) -# endif // BGFX_CONFIG_RENDERER_DIRECT3D9 - -# ifndef BGFX_CONFIG_RENDERER_DIRECT3D11 -# define BGFX_CONFIG_RENDERER_DIRECT3D11 (0 \ - | (BX_PLATFORM_WINDOWS && _WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) \ - ) -# endif // BGFX_CONFIG_RENDERER_DIRECT3D11 - -# ifndef BGFX_CONFIG_RENDERER_OPENGL -# define BGFX_CONFIG_RENDERER_OPENGL (0 \ - | BX_PLATFORM_LINUX \ - | BX_PLATFORM_OSX \ - ) -# endif // BGFX_CONFIG_RENDERER_OPENGL - -# ifndef BGFX_CONFIG_RENDERER_OPENGLES2 -# define BGFX_CONFIG_RENDERER_OPENGLES2 (0 \ - | BX_PLATFORM_EMSCRIPTEN \ - | BX_PLATFORM_NACL \ - | BX_PLATFORM_ANDROID \ - | BX_PLATFORM_IOS \ - ) -# endif // BGFX_CONFIG_RENDERER_OPENGLES2 - -# ifndef BGFX_CONFIG_RENDERER_OPENGLES3 -# define BGFX_CONFIG_RENDERER_OPENGLES3 (0 \ - ) -# endif // BGFX_CONFIG_RENDERER_OPENGLES3 - -# ifndef BGFX_CONFIG_RENDERER_NULL -# define BGFX_CONFIG_RENDERER_NULL (!(0 \ - | BGFX_CONFIG_RENDERER_DIRECT3D9 \ - | BGFX_CONFIG_RENDERER_DIRECT3D11 \ - | BGFX_CONFIG_RENDERER_OPENGL \ - | BGFX_CONFIG_RENDERER_OPENGLES2 \ - | BGFX_CONFIG_RENDERER_OPENGLES3 \ - ) ) -# endif // BGFX_CONFIG_RENDERER_NULL -#else -# ifndef BGFX_CONFIG_RENDERER_DIRECT3D9 -# define BGFX_CONFIG_RENDERER_DIRECT3D9 0 -# endif // BGFX_CONFIG_RENDERER_DIRECT3D9 - -# ifndef BGFX_CONFIG_RENDERER_DIRECT3D11 -# define BGFX_CONFIG_RENDERER_DIRECT3D11 0 -# endif // BGFX_CONFIG_RENDERER_DIRECT3D11 - -# ifndef BGFX_CONFIG_RENDERER_OPENGL -# define BGFX_CONFIG_RENDERER_OPENGL 0 -# endif // BGFX_CONFIG_RENDERER_OPENGL - -# ifndef BGFX_CONFIG_RENDERER_OPENGLES2 -# define BGFX_CONFIG_RENDERER_OPENGLES2 0 -# endif // BGFX_CONFIG_RENDERER_OPENGLES2 - -# ifndef BGFX_CONFIG_RENDERER_OPENGLES3 -# define BGFX_CONFIG_RENDERER_OPENGLES3 0 -# endif // BGFX_CONFIG_RENDERER_OPENGLES3 - -# ifndef BGFX_CONFIG_RENDERER_NULL -# define BGFX_CONFIG_RENDERER_NULL 0 -# endif // BGFX_CONFIG_RENDERER_NULL -#endif // !defined... - -#ifndef BGFX_CONFIG_DEBUG_PERFHUD -# define BGFX_CONFIG_DEBUG_PERFHUD 0 -#endif // BGFX_CONFIG_DEBUG_NVPERFHUD - -/// DX9 PIX markers -#ifndef BGFX_CONFIG_DEBUG_PIX -# define BGFX_CONFIG_DEBUG_PIX 0 -#endif // BGFX_CONFIG_DEBUG_PIX - -/// AMD gDEBugger markers -#ifndef BGFX_CONFIG_DEBUG_GREMEDY -# define BGFX_CONFIG_DEBUG_GREMEDY 0 -#endif // BGFX_CONFIG_DEBUG_GREMEDY - -/// DX11 object names -#ifndef BGFX_CONFIG_DEBUG_OBJECT_NAME -# define BGFX_CONFIG_DEBUG_OBJECT_NAME BGFX_CONFIG_DEBUG -#endif // BGFX_CONFIG_DEBUG_OBJECT_NAME - -#ifndef BGFX_CONFIG_MULTITHREADED -# define BGFX_CONFIG_MULTITHREADED ( (BX_PLATFORM_WINDOWS|BX_PLATFORM_XBOX360|BX_PLATFORM_NACL)&(!BGFX_CONFIG_RENDERER_NULL) ) -#endif // BGFX_CONFIG_MULTITHREADED - -#ifndef BGFX_CONFIG_MAX_DRAW_CALLS -# define BGFX_CONFIG_MAX_DRAW_CALLS (8<<10) -#endif // BGFX_CONFIG_MAX_DRAW_CALLS - -#ifndef BGFX_CONFIG_MAX_MATRIX_CACHE -# define BGFX_CONFIG_MAX_MATRIX_CACHE (16<<10) -#endif // BGFX_CONFIG_MAX_MATRIX_CACHE - -#ifndef BGFX_CONFIG_MAX_VIEWS -# define BGFX_CONFIG_MAX_VIEWS 32 -#endif // BGFX_CONFIG_MAX_VIEWS - -#ifndef BGFX_CONFIG_MAX_VERTEX_DECLS -# define BGFX_CONFIG_MAX_VERTEX_DECLS 64 -#endif // BGFX_CONFIG_MAX_VERTEX_DECLS - -#ifndef BGFX_CONFIG_MAX_INDEX_BUFFERS -# define BGFX_CONFIG_MAX_INDEX_BUFFERS (4<<10) -#endif // BGFX_CONFIG_MAX_INDEX_BUFFERS - -#ifndef BGFX_CONFIG_MAX_VERTEX_BUFFERS -# define BGFX_CONFIG_MAX_VERTEX_BUFFERS (4<<10) -#endif // BGFX_CONFIG_MAX_VERTEX_BUFFERS - -#ifndef BGFX_CONFIG_MAX_DYNAMIC_INDEX_BUFFERS -# define BGFX_CONFIG_MAX_DYNAMIC_INDEX_BUFFERS (4<<10) -#endif // BGFX_CONFIG_MAX_DYNAMIC_INDEX_BUFFERS - -#ifndef BGFX_CONFIG_MAX_DYNAMIC_VERTEX_BUFFERS -# define BGFX_CONFIG_MAX_DYNAMIC_VERTEX_BUFFERS (4<<10) -#endif // BGFX_CONFIG_MAX_DYNAMIC_VERTEX_BUFFERS - -#ifndef BGFX_CONFIG_DYNAMIC_INDEX_BUFFER_SIZE -# define BGFX_CONFIG_DYNAMIC_INDEX_BUFFER_SIZE (1<<20) -#endif // BGFX_CONFIG_DYNAMIC_INDEX_BUFFER_SIZE - -#ifndef BGFX_CONFIG_DYNAMIC_VERTEX_BUFFER_SIZE -# define BGFX_CONFIG_DYNAMIC_VERTEX_BUFFER_SIZE (3<<20) -#endif // BGFX_CONFIG_DYNAMIC_VERTEX_BUFFER_SIZE - -#ifndef BGFX_CONFIG_MAX_VERTEX_SHADERS -# define BGFX_CONFIG_MAX_VERTEX_SHADERS 256 -#endif // BGFX_CONFIG_MAX_VERTEX_SHADERS - -#ifndef BGFX_CONFIG_MAX_FRAGMENT_SHADERS -# define BGFX_CONFIG_MAX_FRAGMENT_SHADERS 256 -#endif // BGFX_CONFIG_MAX_FRAGMENT_SHADERS - -#ifndef BGFX_CONFIG_MAX_PROGRAMS -# define BGFX_CONFIG_MAX_PROGRAMS 512 -#endif // BGFX_CONFIG_MAX_PROGRAMS - -#ifndef BGFX_CONFIG_MAX_PROGRAMS -# define BGFX_CONFIG_MAX_PROGRAMS (4<<10) -#endif // BGFX_CONFIG_MAX_PROGRAMS - -#ifndef BGFX_CONFIG_MAX_TEXTURES -# define BGFX_CONFIG_MAX_TEXTURES (4<<10) -#endif // BGFX_CONFIG_MAX_TEXTURES - -#ifndef BGFX_CONFIG_MAX_RENDER_TARGETS -# define BGFX_CONFIG_MAX_RENDER_TARGETS 64 -#endif // BGFX_CONFIG_MAX_RENDER_TARGETS - -#ifndef BGFX_CONFIG_MAX_UNIFORMS -# define BGFX_CONFIG_MAX_UNIFORMS 512 -#endif // BGFX_CONFIG_MAX_CONSTANTS - -#ifndef BGFX_CONFIG_MAX_COMMAND_BUFFER_SIZE -# define BGFX_CONFIG_MAX_COMMAND_BUFFER_SIZE (64<<10) -#endif // BGFX_CONFIG_MAX_COMMAND_BUFFER_SIZE - -#ifndef BGFX_CONFIG_TRANSIENT_VERTEX_BUFFER_SIZE -# define BGFX_CONFIG_TRANSIENT_VERTEX_BUFFER_SIZE (6<<20) -#endif // BGFX_CONFIG_TRANSIENT_VERTEX_BUFFER_SIZE - -#ifndef BGFX_CONFIG_TRANSIENT_INDEX_BUFFER_SIZE -# define BGFX_CONFIG_TRANSIENT_INDEX_BUFFER_SIZE (2<<20) -#endif // BGFX_CONFIG_TRANSIENT_INDEX_BUFFER_SIZE - -#ifndef BGFX_CONFIG_MAX_CONSTANT_BUFFER_SIZE -# define BGFX_CONFIG_MAX_CONSTANT_BUFFER_SIZE (512<<10) -#endif // BGFX_CONFIG_MAX_CONSTANT_BUFFER_SIZE - -#ifndef BGFX_CONFIG_USE_TINYSTL -# define BGFX_CONFIG_USE_TINYSTL 0 -#endif // BGFX_CONFIG_USE_TINYSTL - -#ifndef BGFX_CONFIG_MAX_INSTANCE_DATA_COUNT -# define BGFX_CONFIG_MAX_INSTANCE_DATA_COUNT 5 -#endif // BGFX_CONFIG_MAX_INSTANCE_DATA_COUNT - -#endif // __CONFIG_H__ +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#ifndef __CONFIG_H__ +#define __CONFIG_H__ + +#if !defined(BGFX_CONFIG_RENDERER_DIRECT3D9) \ + && !defined(BGFX_CONFIG_RENDERER_DIRECT3D11) \ + && !defined(BGFX_CONFIG_RENDERER_OPENGL) \ + && !defined(BGFX_CONFIG_RENDERER_OPENGLES2) \ + && !defined(BGFX_CONFIG_RENDERER_OPENGLES3) \ + && !defined(BGFX_CONFIG_RENDERER_NULL) + +# ifndef BGFX_CONFIG_RENDERER_DIRECT3D9 +# define BGFX_CONFIG_RENDERER_DIRECT3D9 (0 \ + | (BX_PLATFORM_WINDOWS && _WIN32_WINNT < 0x0602 /*_WIN32_WINNT_WIN8*/) \ + | BX_PLATFORM_XBOX360 \ + ) +# endif // BGFX_CONFIG_RENDERER_DIRECT3D9 + +# ifndef BGFX_CONFIG_RENDERER_DIRECT3D11 +# define BGFX_CONFIG_RENDERER_DIRECT3D11 (0 \ + | (BX_PLATFORM_WINDOWS && _WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) \ + ) +# endif // BGFX_CONFIG_RENDERER_DIRECT3D11 + +# ifndef BGFX_CONFIG_RENDERER_OPENGL +# define BGFX_CONFIG_RENDERER_OPENGL (0 \ + | BX_PLATFORM_LINUX \ + | BX_PLATFORM_OSX \ + ) +# endif // BGFX_CONFIG_RENDERER_OPENGL + +# ifndef BGFX_CONFIG_RENDERER_OPENGLES2 +# define BGFX_CONFIG_RENDERER_OPENGLES2 (0 \ + | BX_PLATFORM_EMSCRIPTEN \ + | BX_PLATFORM_NACL \ + | BX_PLATFORM_ANDROID \ + | BX_PLATFORM_IOS \ + ) +# endif // BGFX_CONFIG_RENDERER_OPENGLES2 + +# ifndef BGFX_CONFIG_RENDERER_OPENGLES3 +# define BGFX_CONFIG_RENDERER_OPENGLES3 (0 \ + ) +# endif // BGFX_CONFIG_RENDERER_OPENGLES3 + +# ifndef BGFX_CONFIG_RENDERER_NULL +# define BGFX_CONFIG_RENDERER_NULL (!(0 \ + | BGFX_CONFIG_RENDERER_DIRECT3D9 \ + | BGFX_CONFIG_RENDERER_DIRECT3D11 \ + | BGFX_CONFIG_RENDERER_OPENGL \ + | BGFX_CONFIG_RENDERER_OPENGLES2 \ + | BGFX_CONFIG_RENDERER_OPENGLES3 \ + ) ) +# endif // BGFX_CONFIG_RENDERER_NULL +#else +# ifndef BGFX_CONFIG_RENDERER_DIRECT3D9 +# define BGFX_CONFIG_RENDERER_DIRECT3D9 0 +# endif // BGFX_CONFIG_RENDERER_DIRECT3D9 + +# ifndef BGFX_CONFIG_RENDERER_DIRECT3D11 +# define BGFX_CONFIG_RENDERER_DIRECT3D11 0 +# endif // BGFX_CONFIG_RENDERER_DIRECT3D11 + +# ifndef BGFX_CONFIG_RENDERER_OPENGL +# define BGFX_CONFIG_RENDERER_OPENGL 0 +# endif // BGFX_CONFIG_RENDERER_OPENGL + +# ifndef BGFX_CONFIG_RENDERER_OPENGLES2 +# define BGFX_CONFIG_RENDERER_OPENGLES2 0 +# endif // BGFX_CONFIG_RENDERER_OPENGLES2 + +# ifndef BGFX_CONFIG_RENDERER_OPENGLES3 +# define BGFX_CONFIG_RENDERER_OPENGLES3 0 +# endif // BGFX_CONFIG_RENDERER_OPENGLES3 + +# ifndef BGFX_CONFIG_RENDERER_NULL +# define BGFX_CONFIG_RENDERER_NULL 0 +# endif // BGFX_CONFIG_RENDERER_NULL +#endif // !defined... + +#ifndef BGFX_CONFIG_DEBUG_PERFHUD +# define BGFX_CONFIG_DEBUG_PERFHUD 0 +#endif // BGFX_CONFIG_DEBUG_NVPERFHUD + +/// DX9 PIX markers +#ifndef BGFX_CONFIG_DEBUG_PIX +# define BGFX_CONFIG_DEBUG_PIX 0 +#endif // BGFX_CONFIG_DEBUG_PIX + +/// AMD gDEBugger markers +#ifndef BGFX_CONFIG_DEBUG_GREMEDY +# define BGFX_CONFIG_DEBUG_GREMEDY 0 +#endif // BGFX_CONFIG_DEBUG_GREMEDY + +/// DX11 object names +#ifndef BGFX_CONFIG_DEBUG_OBJECT_NAME +# define BGFX_CONFIG_DEBUG_OBJECT_NAME BGFX_CONFIG_DEBUG +#endif // BGFX_CONFIG_DEBUG_OBJECT_NAME + +#ifndef BGFX_CONFIG_MULTITHREADED +# define BGFX_CONFIG_MULTITHREADED ( (BX_PLATFORM_WINDOWS|BX_PLATFORM_XBOX360|BX_PLATFORM_NACL)&(!BGFX_CONFIG_RENDERER_NULL) ) +#endif // BGFX_CONFIG_MULTITHREADED + +#ifndef BGFX_CONFIG_MAX_DRAW_CALLS +# define BGFX_CONFIG_MAX_DRAW_CALLS (8<<10) +#endif // BGFX_CONFIG_MAX_DRAW_CALLS + +#ifndef BGFX_CONFIG_MAX_MATRIX_CACHE +# define BGFX_CONFIG_MAX_MATRIX_CACHE (16<<10) +#endif // BGFX_CONFIG_MAX_MATRIX_CACHE + +#ifndef BGFX_CONFIG_MAX_VIEWS +# define BGFX_CONFIG_MAX_VIEWS 32 +#endif // BGFX_CONFIG_MAX_VIEWS + +#ifndef BGFX_CONFIG_MAX_VERTEX_DECLS +# define BGFX_CONFIG_MAX_VERTEX_DECLS 64 +#endif // BGFX_CONFIG_MAX_VERTEX_DECLS + +#ifndef BGFX_CONFIG_MAX_INDEX_BUFFERS +# define BGFX_CONFIG_MAX_INDEX_BUFFERS (4<<10) +#endif // BGFX_CONFIG_MAX_INDEX_BUFFERS + +#ifndef BGFX_CONFIG_MAX_VERTEX_BUFFERS +# define BGFX_CONFIG_MAX_VERTEX_BUFFERS (4<<10) +#endif // BGFX_CONFIG_MAX_VERTEX_BUFFERS + +#ifndef BGFX_CONFIG_MAX_DYNAMIC_INDEX_BUFFERS +# define BGFX_CONFIG_MAX_DYNAMIC_INDEX_BUFFERS (4<<10) +#endif // BGFX_CONFIG_MAX_DYNAMIC_INDEX_BUFFERS + +#ifndef BGFX_CONFIG_MAX_DYNAMIC_VERTEX_BUFFERS +# define BGFX_CONFIG_MAX_DYNAMIC_VERTEX_BUFFERS (4<<10) +#endif // BGFX_CONFIG_MAX_DYNAMIC_VERTEX_BUFFERS + +#ifndef BGFX_CONFIG_DYNAMIC_INDEX_BUFFER_SIZE +# define BGFX_CONFIG_DYNAMIC_INDEX_BUFFER_SIZE (1<<20) +#endif // BGFX_CONFIG_DYNAMIC_INDEX_BUFFER_SIZE + +#ifndef BGFX_CONFIG_DYNAMIC_VERTEX_BUFFER_SIZE +# define BGFX_CONFIG_DYNAMIC_VERTEX_BUFFER_SIZE (3<<20) +#endif // BGFX_CONFIG_DYNAMIC_VERTEX_BUFFER_SIZE + +#ifndef BGFX_CONFIG_MAX_VERTEX_SHADERS +# define BGFX_CONFIG_MAX_VERTEX_SHADERS 256 +#endif // BGFX_CONFIG_MAX_VERTEX_SHADERS + +#ifndef BGFX_CONFIG_MAX_FRAGMENT_SHADERS +# define BGFX_CONFIG_MAX_FRAGMENT_SHADERS 256 +#endif // BGFX_CONFIG_MAX_FRAGMENT_SHADERS + +#ifndef BGFX_CONFIG_MAX_PROGRAMS +# define BGFX_CONFIG_MAX_PROGRAMS 512 +#endif // BGFX_CONFIG_MAX_PROGRAMS + +#ifndef BGFX_CONFIG_MAX_PROGRAMS +# define BGFX_CONFIG_MAX_PROGRAMS (4<<10) +#endif // BGFX_CONFIG_MAX_PROGRAMS + +#ifndef BGFX_CONFIG_MAX_TEXTURES +# define BGFX_CONFIG_MAX_TEXTURES (4<<10) +#endif // BGFX_CONFIG_MAX_TEXTURES + +#ifndef BGFX_CONFIG_MAX_RENDER_TARGETS +# define BGFX_CONFIG_MAX_RENDER_TARGETS 64 +#endif // BGFX_CONFIG_MAX_RENDER_TARGETS + +#ifndef BGFX_CONFIG_MAX_UNIFORMS +# define BGFX_CONFIG_MAX_UNIFORMS 512 +#endif // BGFX_CONFIG_MAX_CONSTANTS + +#ifndef BGFX_CONFIG_MAX_COMMAND_BUFFER_SIZE +# define BGFX_CONFIG_MAX_COMMAND_BUFFER_SIZE (64<<10) +#endif // BGFX_CONFIG_MAX_COMMAND_BUFFER_SIZE + +#ifndef BGFX_CONFIG_TRANSIENT_VERTEX_BUFFER_SIZE +# define BGFX_CONFIG_TRANSIENT_VERTEX_BUFFER_SIZE (6<<20) +#endif // BGFX_CONFIG_TRANSIENT_VERTEX_BUFFER_SIZE + +#ifndef BGFX_CONFIG_TRANSIENT_INDEX_BUFFER_SIZE +# define BGFX_CONFIG_TRANSIENT_INDEX_BUFFER_SIZE (2<<20) +#endif // BGFX_CONFIG_TRANSIENT_INDEX_BUFFER_SIZE + +#ifndef BGFX_CONFIG_MAX_CONSTANT_BUFFER_SIZE +# define BGFX_CONFIG_MAX_CONSTANT_BUFFER_SIZE (512<<10) +#endif // BGFX_CONFIG_MAX_CONSTANT_BUFFER_SIZE + +#ifndef BGFX_CONFIG_USE_TINYSTL +# define BGFX_CONFIG_USE_TINYSTL 0 +#endif // BGFX_CONFIG_USE_TINYSTL + +#ifndef BGFX_CONFIG_MAX_INSTANCE_DATA_COUNT +# define BGFX_CONFIG_MAX_INSTANCE_DATA_COUNT 5 +#endif // BGFX_CONFIG_MAX_INSTANCE_DATA_COUNT + +#endif // __CONFIG_H__ diff --git a/src/dds.h b/src/dds.h index fac39ba3..72bcc605 100644 --- a/src/dds.h +++ b/src/dds.h @@ -1,47 +1,47 @@ -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#ifndef __DDS_H__ -#define __DDS_H__ - -#include <stdint.h> - -namespace bgfx -{ - struct Dds - { - TextureFormat::Enum m_type; - uint32_t m_width; - uint32_t m_height; - uint32_t m_depth; - uint8_t m_blockSize; - uint8_t m_numMips; - uint8_t m_bpp; - bool m_hasAlpha; - bool m_cubeMap; - }; - - struct Mip - { - uint32_t m_width; - uint32_t m_height; - uint32_t m_blockSize; - uint32_t m_size; - uint8_t m_bpp; - uint8_t m_type; - bool m_hasAlpha; - const uint8_t* m_data; - - uint32_t getDecodedSize() const; - void decode(uint8_t* _dst); - }; - - bool isDds(const Memory* _mem); - bool parseDds(Dds& _dds, const Memory* _mem); - bool getRawImageData(const Dds& _dds, uint8_t _side, uint8_t _index, const Memory* _mem, Mip& _mip); - -} // namespace bgfx - -#endif // __DDS_H__ +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#ifndef __DDS_H__ +#define __DDS_H__ + +#include <stdint.h> + +namespace bgfx +{ + struct Dds + { + TextureFormat::Enum m_type; + uint32_t m_width; + uint32_t m_height; + uint32_t m_depth; + uint8_t m_blockSize; + uint8_t m_numMips; + uint8_t m_bpp; + bool m_hasAlpha; + bool m_cubeMap; + }; + + struct Mip + { + uint32_t m_width; + uint32_t m_height; + uint32_t m_blockSize; + uint32_t m_size; + uint8_t m_bpp; + uint8_t m_type; + bool m_hasAlpha; + const uint8_t* m_data; + + uint32_t getDecodedSize() const; + void decode(uint8_t* _dst); + }; + + bool isDds(const Memory* _mem); + bool parseDds(Dds& _dds, const Memory* _mem); + bool getRawImageData(const Dds& _dds, uint8_t _side, uint8_t _index, const Memory* _mem, Mip& _mip); + +} // namespace bgfx + +#endif // __DDS_H__ diff --git a/src/fs_clear.sc b/src/fs_clear.sc index 58a50d34..88d21c7a 100644 --- a/src/fs_clear.sc +++ b/src/fs_clear.sc @@ -1,13 +1,13 @@ -$input v_color0 - -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include "common.sh" - -void main() -{ - gl_FragColor = v_color0; -} +$input v_color0 + +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "common.sh" + +void main() +{ + gl_FragColor = v_color0; +} diff --git a/src/fs_debugfont.sc b/src/fs_debugfont.sc index 367a48a2..7d80c1a0 100644 --- a/src/fs_debugfont.sc +++ b/src/fs_debugfont.sc @@ -1,20 +1,20 @@ -$input v_color0, v_color1, v_texcoord0 - -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include "common.sh" - -SAMPLER2D(u_texColor, 0); - -void main() -{ - vec4 color = lerp(v_color1, v_color0, texture2D(u_texColor, v_texcoord0).xxxx); - if (color.w < 1.0/255.0) - { - discard; - } - gl_FragColor = color; -} +$input v_color0, v_color1, v_texcoord0 + +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "common.sh" + +SAMPLER2D(u_texColor, 0); + +void main() +{ + vec4 color = lerp(v_color1, v_color0, texture2D(u_texColor, v_texcoord0).xxxx); + if (color.w < 1.0/255.0) + { + discard; + } + gl_FragColor = color; +} diff --git a/src/renderer_d3d.h b/src/renderer_d3d.h index 7d1abe79..cd09969a 100644 --- a/src/renderer_d3d.h +++ b/src/renderer_d3d.h @@ -1,72 +1,72 @@ -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#ifndef __RENDERER_D3D_H__ -#define __RENDERER_D3D_H__ - -#if BGFX_CONFIG_DEBUG && BX_PLATFORM_WINDOWS && BX_COMPILER_MSVC -# include <dxerr.h> -# pragma comment(lib, "dxerr.lib") -# define DX_CHECK_EXTRA_F " (%s): %s" -# define DX_CHECK_EXTRA_ARGS , DXGetErrorString(__hr__), DXGetErrorDescription(__hr__) -#else -# define DX_CHECK_EXTRA_F "" -# define DX_CHECK_EXTRA_ARGS -#endif // BGFX_CONFIG_DEBUG && BX_PLATFORM_WINDOWS && BX_COMPILER_MSVC - -namespace bgfx -{ -#define _DX_CHECK(_call) \ - do { \ - HRESULT __hr__ = _call; \ - BX_CHECK(SUCCEEDED(__hr__), #_call " FAILED 0x%08x" DX_CHECK_EXTRA_F "\n" \ - , (uint32_t)__hr__ \ - DX_CHECK_EXTRA_ARGS \ - ); \ - } while (0) - -#if BGFX_CONFIG_DEBUG -# define DX_CHECK(_call) _DX_CHECK(_call) -#else -# define DX_CHECK(_call) _call -#endif // BGFX_CONFIG_DEBUG - -#if BGFX_CONFIG_DEBUG -# define DX_CHECK_REFCOUNT(_ptr, _expected) \ - do { \ - ULONG count = getRefCount(_ptr); \ - BX_CHECK(_expected == count, "RefCount is %d (expected %d).", count, _expected); \ - } while (0) - -# define DX_RELEASE(_ptr, _expected) \ - do { \ - if (NULL != _ptr) \ - { \ - ULONG count = _ptr->Release(); \ - BX_CHECK(_expected == count, "RefCount is %d (expected %d).", count, _expected); \ - _ptr = NULL; \ - } \ - } while (0) -#else -# define DX_CHECK_REFCOUNT(_ptr, _expected) -# define DX_RELEASE(_ptr, _expected) \ - do { \ - if (NULL != _ptr) \ - { \ - _ptr->Release(); \ - _ptr = NULL; \ - } \ - } while (0) -#endif // BGFX_CONFIG_DEBUG - - inline int getRefCount(IUnknown* _interface) - { - _interface->AddRef(); - return _interface->Release(); - } - -} // namespace bgfx - -#endif // __RENDERER_D3D_H__ +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#ifndef __RENDERER_D3D_H__ +#define __RENDERER_D3D_H__ + +#if BGFX_CONFIG_DEBUG && BX_PLATFORM_WINDOWS && BX_COMPILER_MSVC +# include <dxerr.h> +# pragma comment(lib, "dxerr.lib") +# define DX_CHECK_EXTRA_F " (%s): %s" +# define DX_CHECK_EXTRA_ARGS , DXGetErrorString(__hr__), DXGetErrorDescription(__hr__) +#else +# define DX_CHECK_EXTRA_F "" +# define DX_CHECK_EXTRA_ARGS +#endif // BGFX_CONFIG_DEBUG && BX_PLATFORM_WINDOWS && BX_COMPILER_MSVC + +namespace bgfx +{ +#define _DX_CHECK(_call) \ + do { \ + HRESULT __hr__ = _call; \ + BX_CHECK(SUCCEEDED(__hr__), #_call " FAILED 0x%08x" DX_CHECK_EXTRA_F "\n" \ + , (uint32_t)__hr__ \ + DX_CHECK_EXTRA_ARGS \ + ); \ + } while (0) + +#if BGFX_CONFIG_DEBUG +# define DX_CHECK(_call) _DX_CHECK(_call) +#else +# define DX_CHECK(_call) _call +#endif // BGFX_CONFIG_DEBUG + +#if BGFX_CONFIG_DEBUG +# define DX_CHECK_REFCOUNT(_ptr, _expected) \ + do { \ + ULONG count = getRefCount(_ptr); \ + BX_CHECK(_expected == count, "RefCount is %d (expected %d).", count, _expected); \ + } while (0) + +# define DX_RELEASE(_ptr, _expected) \ + do { \ + if (NULL != _ptr) \ + { \ + ULONG count = _ptr->Release(); \ + BX_CHECK(_expected == count, "RefCount is %d (expected %d).", count, _expected); \ + _ptr = NULL; \ + } \ + } while (0) +#else +# define DX_CHECK_REFCOUNT(_ptr, _expected) +# define DX_RELEASE(_ptr, _expected) \ + do { \ + if (NULL != _ptr) \ + { \ + _ptr->Release(); \ + _ptr = NULL; \ + } \ + } while (0) +#endif // BGFX_CONFIG_DEBUG + + inline int getRefCount(IUnknown* _interface) + { + _interface->AddRef(); + return _interface->Release(); + } + +} // namespace bgfx + +#endif // __RENDERER_D3D_H__ diff --git a/src/renderer_d3d11.cpp b/src/renderer_d3d11.cpp index 68afb3ff..a34ce49a 100644 --- a/src/renderer_d3d11.cpp +++ b/src/renderer_d3d11.cpp @@ -99,14 +99,14 @@ namespace bgfx }; /* - * D3D11_FILTER_MIN_MAG_MIP_POINT = 0x00, - * D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR = 0x01, - * D3D11_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT = 0x04, - * D3D11_FILTER_MIN_POINT_MAG_MIP_LINEAR = 0x05, - * D3D11_FILTER_MIN_LINEAR_MAG_MIP_POINT = 0x10, - * D3D11_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR = 0x11, - * D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT = 0x14, - * D3D11_FILTER_MIN_MAG_MIP_LINEAR = 0x15, + * D3D11_FILTER_MIN_MAG_MIP_POINT = 0x00, + * D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR = 0x01, + * D3D11_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT = 0x04, + * D3D11_FILTER_MIN_POINT_MAG_MIP_LINEAR = 0x05, + * D3D11_FILTER_MIN_LINEAR_MAG_MIP_POINT = 0x10, + * D3D11_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR = 0x11, + * D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT = 0x14, + * D3D11_FILTER_MIN_MAG_MIP_LINEAR = 0x15, * D3D11_FILTER_ANISOTROPIC = 0x55, * * According to D3D11_FILTER enum bits for mip, mag and mip are: diff --git a/src/renderer_d3d9.cpp b/src/renderer_d3d9.cpp index 92c996ae..da2b0e5f 100644 --- a/src/renderer_d3d9.cpp +++ b/src/renderer_d3d9.cpp @@ -1267,9 +1267,9 @@ namespace bgfx if (NULL != _rect) { RECT rect; - rect.left = _rect->m_x; - rect.top = _rect->m_y; - rect.right = rect.left + _rect->m_width; + rect.left = _rect->m_x; + rect.top = _rect->m_y; + rect.right = rect.left + _rect->m_width; rect.bottom = rect.top + _rect->m_height; DX_CHECK(m_texture2d->LockRect(_lod, &lockedRect, &rect, 0) ); } @@ -1299,9 +1299,9 @@ namespace bgfx if (NULL != _rect) { RECT rect; - rect.left = _rect->m_x; - rect.top = _rect->m_y; - rect.right = rect.left + _rect->m_width; + rect.left = _rect->m_x; + rect.top = _rect->m_y; + rect.right = rect.left + _rect->m_width; rect.bottom = rect.top + _rect->m_height; DX_CHECK(m_textureCube->LockRect(D3DCUBEMAP_FACES(_side), _lod, &lockedRect, &rect, 0) ); } @@ -1516,7 +1516,7 @@ namespace bgfx { uint32_t width = tc.m_width; uint32_t height = tc.m_height; - uint32_t depth = tc.m_depth; + uint32_t depth = tc.m_depth; for (uint32_t lod = 0, num = tc.m_numMips; lod < num; ++lod) { @@ -1534,7 +1534,7 @@ namespace bgfx width >>= 1; height >>= 1; - depth >>= 1; + depth >>= 1; } } @@ -1548,7 +1548,7 @@ namespace bgfx } } - void Texture::update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, const Memory* _mem) + void Texture::update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, const Memory* _mem) { uint32_t pitch; uint32_t slicePitch; diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index 6860dd34..a30d4dbb 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -231,9 +231,9 @@ namespace bgfx GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_backBufferFbo) ); GL_CHECK(glGenRenderbuffers(3, m_backBufferRbos) ); GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_backBufferRbos[0]) ); - GL_CHECK(glRenderbufferStorageMultisample(GL_RENDERBUFFER, _msaa, GL_RGBA8, _width, _height) ); + GL_CHECK(glRenderbufferStorageMultisample(GL_RENDERBUFFER, _msaa, GL_RGBA8, _width, _height) ); GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_backBufferRbos[1]) ); - GL_CHECK(glRenderbufferStorageMultisample(GL_RENDERBUFFER, _msaa, GL_DEPTH24_STENCIL8, _width, _height) ); + GL_CHECK(glRenderbufferStorageMultisample(GL_RENDERBUFFER, _msaa, GL_DEPTH24_STENCIL8, _width, _height) ); GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_backBufferRbos[0]) ); GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_backBufferRbos[1]) ); diff --git a/src/varying.def.sc b/src/varying.def.sc index 1cfa631e..dab116a2 100644 --- a/src/varying.def.sc +++ b/src/varying.def.sc @@ -1,10 +1,10 @@ -vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); -vec4 v_color1 : COLOR1 = vec4(0.0, 1.0, 0.0, 1.0); -vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0); -vec3 v_normal : TEXCOORD1 = vec3(0.0, 1.0, 0.0); - -vec3 a_position : POSITION; -vec3 a_normal : NORMAL0; -vec4 a_color0 : COLOR0; -vec4 a_color1 : COLOR1; -vec2 a_texcoord0 : TEXCOORD0; +vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); +vec4 v_color1 : COLOR1 = vec4(0.0, 1.0, 0.0, 1.0); +vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0); +vec3 v_normal : TEXCOORD1 = vec3(0.0, 1.0, 0.0); + +vec3 a_position : POSITION; +vec3 a_normal : NORMAL0; +vec4 a_color0 : COLOR0; +vec4 a_color1 : COLOR1; +vec2 a_texcoord0 : TEXCOORD0; diff --git a/src/vs_clear.sc b/src/vs_clear.sc index a94487d1..0858e73a 100644 --- a/src/vs_clear.sc +++ b/src/vs_clear.sc @@ -1,15 +1,15 @@ -$input a_position, a_color0 -$output v_color0 - -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include "common.sh" - -void main() -{ - gl_Position = vec4(a_position, 1.0); - v_color0 = a_color0; -} +$input a_position, a_color0 +$output v_color0 + +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "common.sh" + +void main() +{ + gl_Position = vec4(a_position, 1.0); + v_color0 = a_color0; +} diff --git a/src/vs_debugfont.sc b/src/vs_debugfont.sc index ecc66784..be833cb2 100644 --- a/src/vs_debugfont.sc +++ b/src/vs_debugfont.sc @@ -1,17 +1,17 @@ -$input a_position, a_color0, a_color1, a_texcoord0 -$output v_color0, v_color1, v_texcoord0 - -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include "common.sh" - -void main() -{ - gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) ); - v_texcoord0 = a_texcoord0; - v_color0 = a_color0; - v_color1 = a_color1; -} +$input a_position, a_color0, a_color1, a_texcoord0 +$output v_color0, v_color1, v_texcoord0 + +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "common.sh" + +void main() +{ + gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) ); + v_texcoord0 = a_texcoord0; + v_color0 = a_color0; + v_color1 = a_color1; +} diff --git a/tools/geometryc/bounds.cpp b/tools/geometryc/bounds.cpp index 68f14674..8743b91c 100644 --- a/tools/geometryc/bounds.cpp +++ b/tools/geometryc/bounds.cpp @@ -1,15 +1,15 @@ -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include <bx/rng.h> -#include "bounds.h" -#include "math.h" - -void aabbToObb(Obb& _obb, const Aabb& _aabb) -{ - memset(_obb.m_mtx, 0, sizeof(_obb.m_mtx) ); +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include <bx/rng.h> +#include "bounds.h" +#include "math.h" + +void aabbToObb(Obb& _obb, const Aabb& _aabb) +{ + memset(_obb.m_mtx, 0, sizeof(_obb.m_mtx) ); _obb.m_mtx[ 0] = (_aabb.m_max[0] - _aabb.m_min[0]) * 0.5f; _obb.m_mtx[ 5] = (_aabb.m_max[1] - _aabb.m_min[1]) * 0.5f; _obb.m_mtx[10] = (_aabb.m_max[2] - _aabb.m_min[2]) * 0.5f; @@ -17,232 +17,232 @@ void aabbToObb(Obb& _obb, const Aabb& _aabb) _obb.m_mtx[13] = (_aabb.m_min[1] + _aabb.m_max[1]) * 0.5f; _obb.m_mtx[14] = (_aabb.m_min[2] + _aabb.m_max[2]) * 0.5f; _obb.m_mtx[15] = 1.0f; -} - -void aabbTransformToObb(Obb& _obb, const Aabb& _aabb, const float* _mtx) -{ - aabbToObb(_obb, _aabb); - float result[16]; - mtxMul(result, _obb.m_mtx, _mtx); - memcpy(_obb.m_mtx, result, sizeof(result) ); -} - -float calcAreaAabb(Aabb& _aabb) -{ +} + +void aabbTransformToObb(Obb& _obb, const Aabb& _aabb, const float* _mtx) +{ + aabbToObb(_obb, _aabb); + float result[16]; + mtxMul(result, _obb.m_mtx, _mtx); + memcpy(_obb.m_mtx, result, sizeof(result) ); +} + +float calcAreaAabb(Aabb& _aabb) +{ float ww = _aabb.m_max[0] - _aabb.m_min[0]; float hh = _aabb.m_max[1] - _aabb.m_min[1]; float dd = _aabb.m_max[2] - _aabb.m_min[2]; return 2.0f * (ww*hh + ww*dd + hh*dd); -} - -void calcAabb(Aabb& _aabb, const void* _vertices, uint32_t _numVertices, uint32_t _stride) -{ - float min[3], max[3]; - uint8_t* vertex = (uint8_t*)_vertices; - float* position = (float*)vertex; - min[0] = max[0] = position[0]; - min[1] = max[1] = position[1]; - min[2] = max[2] = position[2]; - vertex += _stride; - - for (uint32_t ii = 1; ii < _numVertices; ++ii) - { - position = (float*)vertex; - vertex += _stride; - - float xx = position[0]; - float yy = position[1]; - float zz = position[2]; - min[0] = fmin(xx, min[0]); - min[1] = fmin(yy, min[1]); - min[2] = fmin(zz, min[2]); - max[0] = fmax(xx, max[0]); - max[1] = fmax(yy, max[1]); - max[2] = fmax(zz, max[2]); - } - - _aabb.m_min[0] = min[0]; - _aabb.m_min[1] = min[1]; - _aabb.m_min[2] = min[2]; - _aabb.m_max[0] = max[0]; - _aabb.m_max[1] = max[1]; - _aabb.m_max[2] = max[2]; -} - -void calcAabb(Aabb& _aabb, const float* _mtx, const void* _vertices, uint32_t _numVertices, uint32_t _stride) -{ - float min[3], max[3]; - uint8_t* vertex = (uint8_t*)_vertices; - - float position[3]; - vec3MulMtx(position, (float*)vertex, _mtx); - min[0] = max[0] = position[0]; - min[1] = max[1] = position[1]; - min[2] = max[2] = position[2]; - vertex += _stride; - - for (uint32_t ii = 1; ii < _numVertices; ++ii) - { - vec3MulMtx(position, (float*)vertex, _mtx); - vertex += _stride; - - float xx = position[0]; - float yy = position[1]; - float zz = position[2]; - min[0] = fmin(xx, min[0]); - min[1] = fmin(yy, min[1]); - min[2] = fmin(zz, min[2]); - max[0] = fmax(xx, max[0]); - max[1] = fmax(yy, max[1]); - max[2] = fmax(zz, max[2]); - } - - _aabb.m_min[0] = min[0]; - _aabb.m_min[1] = min[1]; - _aabb.m_min[2] = min[2]; - _aabb.m_max[0] = max[0]; - _aabb.m_max[1] = max[1]; - _aabb.m_max[2] = max[2]; -} - -void calcObb(Obb& _obb, const void* _vertices, uint32_t _numVertices, uint32_t _stride, uint32_t _steps) -{ - Aabb aabb; - calcAabb(aabb, _vertices, _numVertices, _stride); - float minArea = calcAreaAabb(aabb); - - Obb best; - aabbToObb(best, aabb); - - float angleStep = float(M_PI_2/_steps); - float ax = 0.0f; - float mtx[16]; - - for (uint32_t ii = 0; ii < _steps; ++ii) - { - float ay = 0.0f; - - for (uint32_t jj = 0; jj < _steps; ++jj) - { - float az = 0.0f; - - for (uint32_t kk = 0; kk < _steps; ++kk) - { - mtxRotateXYZ(mtx, ax, ay, az); - - float mtxT[16]; - mtxTranspose(mtxT, mtx); - calcAabb(aabb, mtxT, _vertices, _numVertices, _stride); - - float area = calcAreaAabb(aabb); - if (area < minArea) - { - minArea = area; - aabbTransformToObb(best, aabb, mtx); - } - - az += angleStep; - } - - ay += angleStep; - } - - ax += angleStep; - } - - memcpy(&_obb, &best, sizeof(Obb) ); -} - -void calcMaxBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride) -{ - Aabb aabb; - calcAabb(aabb, _vertices, _numVertices, _stride); - - float center[3]; - center[0] = (aabb.m_min[0] + aabb.m_max[0]) * 0.5f; - center[1] = (aabb.m_min[1] + aabb.m_max[1]) * 0.5f; - center[2] = (aabb.m_min[2] + aabb.m_max[2]) * 0.5f; - - float maxDistSq = 0.0f; - uint8_t* vertex = (uint8_t*)_vertices; - - for (uint32_t ii = 0; ii < _numVertices; ++ii) - { - float* position = (float*)vertex; - vertex += _stride; - - float xx = position[0] - center[0]; - float yy = position[1] - center[1]; - float zz = position[2] - center[2]; - - float distSq = xx*xx + yy*yy + zz*zz; - maxDistSq = fmax(distSq, maxDistSq); - } - - _sphere.m_center[0] = center[0]; - _sphere.m_center[1] = center[1]; - _sphere.m_center[2] = center[2]; - _sphere.m_radius = sqrtf(maxDistSq); -} - -void calcMinBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride, float _step) -{ - bx::RngMwc rng; - - uint8_t* vertex = (uint8_t*)_vertices; - - float center[3]; - float* position = (float*)&vertex[0]; - center[0] = position[0]; - center[1] = position[1]; - center[2] = position[2]; - - position = (float*)&vertex[1*_stride]; - center[0] += position[0]; - center[1] += position[1]; - center[2] += position[2]; - - center[0] *= 0.5f; - center[1] *= 0.5f; - center[2] *= 0.5f; - - float xx = position[0] - center[0]; - float yy = position[1] - center[1]; - float zz = position[2] - center[2]; - float maxDistSq = xx*xx + yy*yy + zz*zz; - - float radiusStep = _step * 0.37f; - - bool done; - do - { - done = true; - for (uint32_t ii = 0, index = rng.gen()%_numVertices; ii < _numVertices; ++ii, index = (index + 1)%_numVertices) - { - position = (float*)&vertex[index*_stride]; - - float xx = position[0] - center[0]; - float yy = position[1] - center[1]; - float zz = position[2] - center[2]; - float distSq = xx*xx + yy*yy + zz*zz; - - if (distSq > maxDistSq) - { - done = false; - - center[0] += xx * radiusStep; - center[1] += yy * radiusStep; - center[2] += zz * radiusStep; - maxDistSq = flerp(maxDistSq, distSq, _step); - - break; - } - } - - } while (!done); - - _sphere.m_center[0] = center[0]; - _sphere.m_center[1] = center[1]; - _sphere.m_center[2] = center[2]; - _sphere.m_radius = sqrtf(maxDistSq); -} +} + +void calcAabb(Aabb& _aabb, const void* _vertices, uint32_t _numVertices, uint32_t _stride) +{ + float min[3], max[3]; + uint8_t* vertex = (uint8_t*)_vertices; + float* position = (float*)vertex; + min[0] = max[0] = position[0]; + min[1] = max[1] = position[1]; + min[2] = max[2] = position[2]; + vertex += _stride; + + for (uint32_t ii = 1; ii < _numVertices; ++ii) + { + position = (float*)vertex; + vertex += _stride; + + float xx = position[0]; + float yy = position[1]; + float zz = position[2]; + min[0] = fmin(xx, min[0]); + min[1] = fmin(yy, min[1]); + min[2] = fmin(zz, min[2]); + max[0] = fmax(xx, max[0]); + max[1] = fmax(yy, max[1]); + max[2] = fmax(zz, max[2]); + } + + _aabb.m_min[0] = min[0]; + _aabb.m_min[1] = min[1]; + _aabb.m_min[2] = min[2]; + _aabb.m_max[0] = max[0]; + _aabb.m_max[1] = max[1]; + _aabb.m_max[2] = max[2]; +} + +void calcAabb(Aabb& _aabb, const float* _mtx, const void* _vertices, uint32_t _numVertices, uint32_t _stride) +{ + float min[3], max[3]; + uint8_t* vertex = (uint8_t*)_vertices; + + float position[3]; + vec3MulMtx(position, (float*)vertex, _mtx); + min[0] = max[0] = position[0]; + min[1] = max[1] = position[1]; + min[2] = max[2] = position[2]; + vertex += _stride; + + for (uint32_t ii = 1; ii < _numVertices; ++ii) + { + vec3MulMtx(position, (float*)vertex, _mtx); + vertex += _stride; + + float xx = position[0]; + float yy = position[1]; + float zz = position[2]; + min[0] = fmin(xx, min[0]); + min[1] = fmin(yy, min[1]); + min[2] = fmin(zz, min[2]); + max[0] = fmax(xx, max[0]); + max[1] = fmax(yy, max[1]); + max[2] = fmax(zz, max[2]); + } + + _aabb.m_min[0] = min[0]; + _aabb.m_min[1] = min[1]; + _aabb.m_min[2] = min[2]; + _aabb.m_max[0] = max[0]; + _aabb.m_max[1] = max[1]; + _aabb.m_max[2] = max[2]; +} + +void calcObb(Obb& _obb, const void* _vertices, uint32_t _numVertices, uint32_t _stride, uint32_t _steps) +{ + Aabb aabb; + calcAabb(aabb, _vertices, _numVertices, _stride); + float minArea = calcAreaAabb(aabb); + + Obb best; + aabbToObb(best, aabb); + + float angleStep = float(M_PI_2/_steps); + float ax = 0.0f; + float mtx[16]; + + for (uint32_t ii = 0; ii < _steps; ++ii) + { + float ay = 0.0f; + + for (uint32_t jj = 0; jj < _steps; ++jj) + { + float az = 0.0f; + + for (uint32_t kk = 0; kk < _steps; ++kk) + { + mtxRotateXYZ(mtx, ax, ay, az); + + float mtxT[16]; + mtxTranspose(mtxT, mtx); + calcAabb(aabb, mtxT, _vertices, _numVertices, _stride); + + float area = calcAreaAabb(aabb); + if (area < minArea) + { + minArea = area; + aabbTransformToObb(best, aabb, mtx); + } + + az += angleStep; + } + + ay += angleStep; + } + + ax += angleStep; + } + + memcpy(&_obb, &best, sizeof(Obb) ); +} + +void calcMaxBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride) +{ + Aabb aabb; + calcAabb(aabb, _vertices, _numVertices, _stride); + + float center[3]; + center[0] = (aabb.m_min[0] + aabb.m_max[0]) * 0.5f; + center[1] = (aabb.m_min[1] + aabb.m_max[1]) * 0.5f; + center[2] = (aabb.m_min[2] + aabb.m_max[2]) * 0.5f; + + float maxDistSq = 0.0f; + uint8_t* vertex = (uint8_t*)_vertices; + + for (uint32_t ii = 0; ii < _numVertices; ++ii) + { + float* position = (float*)vertex; + vertex += _stride; + + float xx = position[0] - center[0]; + float yy = position[1] - center[1]; + float zz = position[2] - center[2]; + + float distSq = xx*xx + yy*yy + zz*zz; + maxDistSq = fmax(distSq, maxDistSq); + } + + _sphere.m_center[0] = center[0]; + _sphere.m_center[1] = center[1]; + _sphere.m_center[2] = center[2]; + _sphere.m_radius = sqrtf(maxDistSq); +} + +void calcMinBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride, float _step) +{ + bx::RngMwc rng; + + uint8_t* vertex = (uint8_t*)_vertices; + + float center[3]; + float* position = (float*)&vertex[0]; + center[0] = position[0]; + center[1] = position[1]; + center[2] = position[2]; + + position = (float*)&vertex[1*_stride]; + center[0] += position[0]; + center[1] += position[1]; + center[2] += position[2]; + + center[0] *= 0.5f; + center[1] *= 0.5f; + center[2] *= 0.5f; + + float xx = position[0] - center[0]; + float yy = position[1] - center[1]; + float zz = position[2] - center[2]; + float maxDistSq = xx*xx + yy*yy + zz*zz; + + float radiusStep = _step * 0.37f; + + bool done; + do + { + done = true; + for (uint32_t ii = 0, index = rng.gen()%_numVertices; ii < _numVertices; ++ii, index = (index + 1)%_numVertices) + { + position = (float*)&vertex[index*_stride]; + + float xx = position[0] - center[0]; + float yy = position[1] - center[1]; + float zz = position[2] - center[2]; + float distSq = xx*xx + yy*yy + zz*zz; + + if (distSq > maxDistSq) + { + done = false; + + center[0] += xx * radiusStep; + center[1] += yy * radiusStep; + center[2] += zz * radiusStep; + maxDistSq = flerp(maxDistSq, distSq, _step); + + break; + } + } + + } while (!done); + + _sphere.m_center[0] = center[0]; + _sphere.m_center[1] = center[1]; + _sphere.m_center[2] = center[2]; + _sphere.m_radius = sqrtf(maxDistSq); +} diff --git a/tools/geometryc/bounds.h b/tools/geometryc/bounds.h index 7850b25a..bee82480 100644 --- a/tools/geometryc/bounds.h +++ b/tools/geometryc/bounds.h @@ -1,47 +1,47 @@ -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#ifndef __BOUNDS_H__ -#define __BOUNDS_H__ - -struct Aabb -{ - float m_min[3]; - float m_max[3]; -}; - -struct Obb -{ - float m_mtx[16]; -}; - -struct Sphere -{ - float m_center[3]; - float m_radius; -}; - -/// Convert axis aligned bounding box to oriented bounding box. -void aabbToObb(Obb& _obb, const Aabb& _aabb); - -/// Calculate surface area of axis aligned bounding box. -float calcAabbArea(Aabb& _aabb); - -/// Calculate axis aligned bounding box. -void calcAabb(Aabb& _aabb, const void* _vertices, uint32_t _numVertices, uint32_t _stride); - -/// Transform vertices and calculate axis aligned bounding box. -void calcAabb(Aabb& _aabb, const float* _mtx, const void* _vertices, uint32_t _numVertices, uint32_t _stride); - -/// Calculate oriented bounding box. -void calcObb(Obb& _obb, const void* _vertices, uint32_t _numVertices, uint32_t _stride, uint32_t _steps = 17); - -/// Calculate maximum bounding sphere. -void calcMaxBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride); - -/// Calculate minimum bounding sphere. -void calcMinBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride, float _step = 0.01f); - -#endif // __BOUNDS_H__ +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#ifndef __BOUNDS_H__ +#define __BOUNDS_H__ + +struct Aabb +{ + float m_min[3]; + float m_max[3]; +}; + +struct Obb +{ + float m_mtx[16]; +}; + +struct Sphere +{ + float m_center[3]; + float m_radius; +}; + +/// Convert axis aligned bounding box to oriented bounding box. +void aabbToObb(Obb& _obb, const Aabb& _aabb); + +/// Calculate surface area of axis aligned bounding box. +float calcAabbArea(Aabb& _aabb); + +/// Calculate axis aligned bounding box. +void calcAabb(Aabb& _aabb, const void* _vertices, uint32_t _numVertices, uint32_t _stride); + +/// Transform vertices and calculate axis aligned bounding box. +void calcAabb(Aabb& _aabb, const float* _mtx, const void* _vertices, uint32_t _numVertices, uint32_t _stride); + +/// Calculate oriented bounding box. +void calcObb(Obb& _obb, const void* _vertices, uint32_t _numVertices, uint32_t _stride, uint32_t _steps = 17); + +/// Calculate maximum bounding sphere. +void calcMaxBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride); + +/// Calculate minimum bounding sphere. +void calcMinBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride, float _step = 0.01f); + +#endif // __BOUNDS_H__ diff --git a/tools/geometryc/math.h b/tools/geometryc/math.h index 508e1c0b..fdcd45b8 100644 --- a/tools/geometryc/math.h +++ b/tools/geometryc/math.h @@ -12,21 +12,21 @@ #include <math.h> #include <string.h> -inline float fmin(float _a, float _b) -{ - return _a < _b ? _a : _b; -} - -inline float fmax(float _a, float _b) -{ - return _a > _b ? _a : _b; -} - -inline float flerp(float _a, float _b, float _t) -{ - return _a + (_b - _a) * _t; -} - +inline float fmin(float _a, float _b) +{ + return _a < _b ? _a : _b; +} + +inline float fmax(float _a, float _b) +{ + return _a > _b ? _a : _b; +} + +inline float flerp(float _a, float _b, float _t) +{ + return _a + (_b - _a) * _t; +} + inline void vec3Add(float* __restrict _result, const float* __restrict _a, const float* __restrict _b) { _result[0] = _a[0] + _b[0]; @@ -224,13 +224,13 @@ inline void mtxRotateXYZ(float* _result, float _ax, float _ay, float _az) _result[ 0] = cy*cz; _result[ 1] = -cy*sz; _result[ 2] = sy; - _result[ 4] = cz*sx*sy + cx*sz; - _result[ 5] = cx*cz - sx*sy*sz; + _result[ 4] = cz*sx*sy + cx*sz; + _result[ 5] = cx*cz - sx*sy*sz; _result[ 6] = -cy*sx; - _result[ 8] = -cx*cz*sy + sx*sz; - _result[ 9] = cz*sx + cx*sy*sz; - _result[10] = cx*cy; - _result[15] = 1.0f; + _result[ 8] = -cx*cz*sy + sx*sz; + _result[ 9] = cz*sx + cx*sy*sz; + _result[10] = cx*cy; + _result[15] = 1.0f; } inline void mtxRotateZYX(float* _result, float _ax, float _ay, float _az) @@ -243,16 +243,16 @@ inline void mtxRotateZYX(float* _result, float _ax, float _ay, float _az) float cz = cosf(_az); memset(_result, 0, sizeof(float)*16); - _result[ 0] = cy*cz; - _result[ 1] = cz*sx*sy-cx*sz; - _result[ 2] = cx*cz*sy+sx*sz; - _result[ 4] = cy*sz; - _result[ 5] = cx*cz + sx*sy*sz; - _result[ 6] = -cz*sx + cx*sy*sz; - _result[ 8] = -sy; - _result[ 9] = cy*sx; - _result[10] = cx*cy; - _result[15] = 1.0f; + _result[ 0] = cy*cz; + _result[ 1] = cz*sx*sy-cx*sz; + _result[ 2] = cx*cz*sy+sx*sz; + _result[ 4] = cy*sz; + _result[ 5] = cx*cz + sx*sy*sz; + _result[ 6] = -cz*sx + cx*sy*sz; + _result[ 8] = -sy; + _result[ 9] = cy*sx; + _result[10] = cx*cy; + _result[15] = 1.0f; }; inline void vec3MulMtx(float* __restrict _result, const float* __restrict _vec, const float* __restrict _mat) diff --git a/tools/geometryc/tokenizecmd.cpp b/tools/geometryc/tokenizecmd.cpp index 0bf61ac5..7396cf03 100644 --- a/tools/geometryc/tokenizecmd.cpp +++ b/tools/geometryc/tokenizecmd.cpp @@ -1,201 +1,201 @@ -/* - * Copyright 2012 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include <stdint.h> -#include <stdio.h> -#include <ctype.h> -#include "tokenizecmd.h" - -// Reference: -// http://msdn.microsoft.com/en-us/library/a1y7w461.aspx -const char* tokenizeCommandLine(const char* _commandLine, char* _buffer, uint32_t& _bufferSize, int& _argc, char* _argv[], int _maxArgvs, char _term) -{ - int argc = 0; - const char* curr = _commandLine; - char* currOut = _buffer; - char term = ' '; - bool sub = false; - - enum ParserState - { - SkipWhitespace, - SetTerm, - Copy, - Escape, - End, - }; - - ParserState state = SkipWhitespace; - - while ('\0' != *curr - && _term != *curr - && argc < _maxArgvs) - { - switch (state) - { - case SkipWhitespace: - for (; isspace(*curr); ++curr); // skip whitespace - state = SetTerm; - break; - - case SetTerm: - if ('"' == *curr) - { - term = '"'; - ++curr; // skip begining quote - } - else - { - term = ' '; - } - - _argv[argc] = currOut; - ++argc; - - state = Copy; - break; - - case Copy: - if ('\\' == *curr) - { - state = Escape; - } - else if ('"' == *curr - && '"' != term) - { - sub = !sub; - } - else if (isspace(*curr) && !sub) - { - state = End; - } - else if (term != *curr || sub) - { - *currOut = *curr; - ++currOut; - } - else - { - state = End; - } - ++curr; - break; - - case Escape: - { - const char* start = --curr; - for (; '\\' == *curr; ++curr); - - if ('"' != *curr) - { - int count = (int)(curr-start); - - curr = start; - for (int ii = 0; ii < count; ++ii) - { - *currOut = *curr; - ++currOut; - ++curr; - } - } - else - { - curr = start+1; - *currOut = *curr; - ++currOut; - ++curr; - } - } - state = Copy; - break; - - case End: - *currOut = '\0'; - ++currOut; - state = SkipWhitespace; - break; - } - } - - *currOut = '\0'; - if (0 < argc - && '\0' == _argv[argc-1][0]) - { - --argc; - } - - _bufferSize = (uint32_t)(currOut - _buffer); - _argc = argc; - - if ('\0' != *curr) - { - ++curr; - } - - return curr; -} - -#if 0 - -#include <string.h> - -int main(int _argc, const char** _argv) -{ - const char* input[7] = - { - " ", - "\\", - "\"a b c\" d e", - "\"ab\\\"c\" \"\\\\\" d", - "a\\\\\\b d\"e f\"g h", - "a\\\\\\\"b c d", - "a\\\\\\\\\"b c\" d e", - }; - - const int expected_argc[7] = - { - 0, 0, 3, 3, 3, 3, 3 - }; - - const char* expected_results[] = - { - "a b c", "d", "e", - "ab\"c", "\\", "d", - "a\\\\\\b", "de fg", "h", - "a\\\"b", "c", "d", - "a\\\\b c", "d", "e", - }; - - const char** expected_argv[7] = - { - NULL, - NULL, - &expected_results[0], - &expected_results[3], - &expected_results[6], - &expected_results[9], - &expected_results[12], - }; - - for (int ii = 0; ii < 7; ++ii) - { - char commandLine[1024]; - unsigned int size = 1023; - char* argv[50]; - int argc = tokenizeCommandLine(input[ii], commandLine, size, argv, 50); - printf("\n%d (%d): %s %s\n", ii, argc, input[ii], expected_argc[ii]==argc?"":"FAILED!"); - for (int jj = 0; jj < argc; ++jj) - { - printf("\t%d: {%s} %s\n" - , jj - , argv[jj] - , jj<argc?(0==strcmp(argv[jj], expected_argv[ii][jj])?"":"FAILED!"):"FAILED!" - ); - } - } - - return 0; -} -#endif // 0 +/* + * Copyright 2012 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include <stdint.h> +#include <stdio.h> +#include <ctype.h> +#include "tokenizecmd.h" + +// Reference: +// http://msdn.microsoft.com/en-us/library/a1y7w461.aspx +const char* tokenizeCommandLine(const char* _commandLine, char* _buffer, uint32_t& _bufferSize, int& _argc, char* _argv[], int _maxArgvs, char _term) +{ + int argc = 0; + const char* curr = _commandLine; + char* currOut = _buffer; + char term = ' '; + bool sub = false; + + enum ParserState + { + SkipWhitespace, + SetTerm, + Copy, + Escape, + End, + }; + + ParserState state = SkipWhitespace; + + while ('\0' != *curr + && _term != *curr + && argc < _maxArgvs) + { + switch (state) + { + case SkipWhitespace: + for (; isspace(*curr); ++curr); // skip whitespace + state = SetTerm; + break; + + case SetTerm: + if ('"' == *curr) + { + term = '"'; + ++curr; // skip begining quote + } + else + { + term = ' '; + } + + _argv[argc] = currOut; + ++argc; + + state = Copy; + break; + + case Copy: + if ('\\' == *curr) + { + state = Escape; + } + else if ('"' == *curr + && '"' != term) + { + sub = !sub; + } + else if (isspace(*curr) && !sub) + { + state = End; + } + else if (term != *curr || sub) + { + *currOut = *curr; + ++currOut; + } + else + { + state = End; + } + ++curr; + break; + + case Escape: + { + const char* start = --curr; + for (; '\\' == *curr; ++curr); + + if ('"' != *curr) + { + int count = (int)(curr-start); + + curr = start; + for (int ii = 0; ii < count; ++ii) + { + *currOut = *curr; + ++currOut; + ++curr; + } + } + else + { + curr = start+1; + *currOut = *curr; + ++currOut; + ++curr; + } + } + state = Copy; + break; + + case End: + *currOut = '\0'; + ++currOut; + state = SkipWhitespace; + break; + } + } + + *currOut = '\0'; + if (0 < argc + && '\0' == _argv[argc-1][0]) + { + --argc; + } + + _bufferSize = (uint32_t)(currOut - _buffer); + _argc = argc; + + if ('\0' != *curr) + { + ++curr; + } + + return curr; +} + +#if 0 + +#include <string.h> + +int main(int _argc, const char** _argv) +{ + const char* input[7] = + { + " ", + "\\", + "\"a b c\" d e", + "\"ab\\\"c\" \"\\\\\" d", + "a\\\\\\b d\"e f\"g h", + "a\\\\\\\"b c d", + "a\\\\\\\\\"b c\" d e", + }; + + const int expected_argc[7] = + { + 0, 0, 3, 3, 3, 3, 3 + }; + + const char* expected_results[] = + { + "a b c", "d", "e", + "ab\"c", "\\", "d", + "a\\\\\\b", "de fg", "h", + "a\\\"b", "c", "d", + "a\\\\b c", "d", "e", + }; + + const char** expected_argv[7] = + { + NULL, + NULL, + &expected_results[0], + &expected_results[3], + &expected_results[6], + &expected_results[9], + &expected_results[12], + }; + + for (int ii = 0; ii < 7; ++ii) + { + char commandLine[1024]; + unsigned int size = 1023; + char* argv[50]; + int argc = tokenizeCommandLine(input[ii], commandLine, size, argv, 50); + printf("\n%d (%d): %s %s\n", ii, argc, input[ii], expected_argc[ii]==argc?"":"FAILED!"); + for (int jj = 0; jj < argc; ++jj) + { + printf("\t%d: {%s} %s\n" + , jj + , argv[jj] + , jj<argc?(0==strcmp(argv[jj], expected_argv[ii][jj])?"":"FAILED!"):"FAILED!" + ); + } + } + + return 0; +} +#endif // 0 diff --git a/tools/geometryc/tokenizecmd.h b/tools/geometryc/tokenizecmd.h index 536111ed..4155ae1e 100644 --- a/tools/geometryc/tokenizecmd.h +++ b/tools/geometryc/tokenizecmd.h @@ -1,11 +1,11 @@ -/* - * Copyright 2012 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#ifndef __TOKENIZE_CMD_H__ -#define __TOKENIZE_CMD_H__ - -const char* tokenizeCommandLine(const char* _commandLine, char* _buffer, uint32_t& _bufferSize, int& _argc, char* _argv[], int _maxArgvs, char _term = '\0'); - -#endif // __TOKENIZE_CMD_H__ +/* + * Copyright 2012 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#ifndef __TOKENIZE_CMD_H__ +#define __TOKENIZE_CMD_H__ + +const char* tokenizeCommandLine(const char* _commandLine, char* _buffer, uint32_t& _bufferSize, int& _argc, char* _argv[], int _maxArgvs, char _term = '\0'); + +#endif // __TOKENIZE_CMD_H__ diff --git a/tools/makedisttex.cpp b/tools/makedisttex.cpp index c08318d3..12330e73 100644 --- a/tools/makedisttex.cpp +++ b/tools/makedisttex.cpp @@ -1,193 +1,193 @@ -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include <stdio.h> -#include <stdlib.h> -#include <stdint.h> -#include <string.h> - -#include <edtaa3func.h> -#include <stb_image.c> - -#define BX_NAMESPACE 1 -#include <bx/bx.h> -#include <bx/commandline.h> -#include <bx/uint32_t.h> - -long int fsize(FILE* _file) -{ - long int pos = ftell(_file); - fseek(_file, 0L, SEEK_END); - long int size = ftell(_file); - fseek(_file, pos, SEEK_SET); - return size; -} - -void edtaa3(double* _img, uint16_t _width, uint16_t _height, double* _out) -{ - uint32_t size = _width*_height; - - short* xdist = (short*)malloc(size*sizeof(short) ); - short* ydist = (short*)malloc(size*sizeof(short) ); - double* gx = (double*)malloc(size*sizeof(double) ); - double* gy = (double*)malloc(size*sizeof(double) ); - - computegradient(_img, _width, _height, gx, gy); - edtaa3(_img, gx, gy, _width, _height, xdist, ydist, _out); - - for (uint32_t ii = 0; ii < size; ++ii) - { - if (_out[ii] < 0.0) - { - _out[ii] = 0.0; - } - } - - free(xdist); - free(ydist); - free(gx); - free(gy); -} - -void saveTga(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _pitch, bool _grayscale, const void* _data) -{ - FILE* file = fopen(_filePath, "wb"); - if ( NULL != file ) - { - uint8_t type = _grayscale ? 3 : 2; - uint8_t bpp = _grayscale ? 8 : 32; - uint8_t xorig = 0; - uint8_t yorig = 0; - - putc(0, file); - putc(0, file); - putc(type, file); - putc(0, file); - putc(0, file); - putc(0, file); - putc(0, file); - putc(0, file); - putc(0, file); - putc(xorig, file); - putc(0, file); - putc(yorig, file); - putc(_width&0xff, file); - putc( (_width>>8)&0xff, file); - putc(_height&0xff, file); - putc( (_height>>8)&0xff, file); - putc(bpp, file); - putc(32, file); - - uint32_t width = _width * bpp / 8; - uint8_t* data = (uint8_t*)_data; - for (uint32_t yy = 0; yy < _height; ++yy) - { - fwrite(data, width, 1, file); - data += _pitch; - } - - fclose(file); - } -} - -inline double min(double _a, double _b) -{ - return _a > _b ? _b : _a; -} - -inline double max(double _a, double _b) -{ - return _a > _b ? _a : _b; -} - -inline double clamp(double _val, double _min, double _max) -{ - return max(min(_val, _max), _min); -} - -inline double saturate(double _val) -{ - return clamp(_val, 0.0, 1.0); -} - -int main(int _argc, const char* _argv[]) -{ - CommandLine cmdLine(_argc, _argv); - - const char* inFilePath = cmdLine.findOption('i'); - if (NULL == inFilePath) - { - fprintf(stderr, "Input file name must be specified.\n"); - return EXIT_FAILURE; - } - - const char* outFilePath = cmdLine.findOption('o'); - if (NULL == outFilePath) - { - fprintf(stderr, "Output file name must be specified.\n"); - return EXIT_FAILURE; - } - - double edge = 16.0; - const char* edgeOpt = cmdLine.findOption('e'); - if (NULL != edgeOpt) - { - edge = atof(edgeOpt); - } - - int width; - int height; - int comp; - - stbi_uc* img = stbi_load(inFilePath, &width, &height, &comp, 1); - - if (NULL == img) - { - fprintf(stderr, "Failed to load %s.\n", inFilePath); - return EXIT_FAILURE; - } - - uint32_t size = width*height; - - double* imgIn = (double*)malloc(size*sizeof(double) ); - double* outside = (double*)malloc(size*sizeof(double) ); - double* inside = (double*)malloc(size*sizeof(double) ); - - for (uint32_t ii = 0; ii < size; ++ii) - { - imgIn[ii] = double(img[ii])/255.0; - } - - edtaa3(imgIn, width, height, outside); - - for (uint32_t ii = 0; ii < size; ++ii) - { - imgIn[ii] = 1.0 - imgIn[ii]; - } - - edtaa3(imgIn, width, height, inside); - - free(imgIn); - - uint8_t* grayscale = (uint8_t*)malloc(size); - - double edgeOffset = edge*0.5; - double invEdge = 1.0/edge; - - for (uint32_t ii = 0; ii < size; ++ii) - { - double dist = saturate( ( (outside[ii] - inside[ii])+edgeOffset) * invEdge); - grayscale[ii] = 255-uint8_t(dist * 255.0); - } - - free(inside); - free(outside); - - saveTga(outFilePath, width, height, width, true, grayscale); - - free(grayscale); - - return EXIT_SUCCESS; -} +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <string.h> + +#include <edtaa3func.h> +#include <stb_image.c> + +#define BX_NAMESPACE 1 +#include <bx/bx.h> +#include <bx/commandline.h> +#include <bx/uint32_t.h> + +long int fsize(FILE* _file) +{ + long int pos = ftell(_file); + fseek(_file, 0L, SEEK_END); + long int size = ftell(_file); + fseek(_file, pos, SEEK_SET); + return size; +} + +void edtaa3(double* _img, uint16_t _width, uint16_t _height, double* _out) +{ + uint32_t size = _width*_height; + + short* xdist = (short*)malloc(size*sizeof(short) ); + short* ydist = (short*)malloc(size*sizeof(short) ); + double* gx = (double*)malloc(size*sizeof(double) ); + double* gy = (double*)malloc(size*sizeof(double) ); + + computegradient(_img, _width, _height, gx, gy); + edtaa3(_img, gx, gy, _width, _height, xdist, ydist, _out); + + for (uint32_t ii = 0; ii < size; ++ii) + { + if (_out[ii] < 0.0) + { + _out[ii] = 0.0; + } + } + + free(xdist); + free(ydist); + free(gx); + free(gy); +} + +void saveTga(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _pitch, bool _grayscale, const void* _data) +{ + FILE* file = fopen(_filePath, "wb"); + if ( NULL != file ) + { + uint8_t type = _grayscale ? 3 : 2; + uint8_t bpp = _grayscale ? 8 : 32; + uint8_t xorig = 0; + uint8_t yorig = 0; + + putc(0, file); + putc(0, file); + putc(type, file); + putc(0, file); + putc(0, file); + putc(0, file); + putc(0, file); + putc(0, file); + putc(0, file); + putc(xorig, file); + putc(0, file); + putc(yorig, file); + putc(_width&0xff, file); + putc( (_width>>8)&0xff, file); + putc(_height&0xff, file); + putc( (_height>>8)&0xff, file); + putc(bpp, file); + putc(32, file); + + uint32_t width = _width * bpp / 8; + uint8_t* data = (uint8_t*)_data; + for (uint32_t yy = 0; yy < _height; ++yy) + { + fwrite(data, width, 1, file); + data += _pitch; + } + + fclose(file); + } +} + +inline double min(double _a, double _b) +{ + return _a > _b ? _b : _a; +} + +inline double max(double _a, double _b) +{ + return _a > _b ? _a : _b; +} + +inline double clamp(double _val, double _min, double _max) +{ + return max(min(_val, _max), _min); +} + +inline double saturate(double _val) +{ + return clamp(_val, 0.0, 1.0); +} + +int main(int _argc, const char* _argv[]) +{ + CommandLine cmdLine(_argc, _argv); + + const char* inFilePath = cmdLine.findOption('i'); + if (NULL == inFilePath) + { + fprintf(stderr, "Input file name must be specified.\n"); + return EXIT_FAILURE; + } + + const char* outFilePath = cmdLine.findOption('o'); + if (NULL == outFilePath) + { + fprintf(stderr, "Output file name must be specified.\n"); + return EXIT_FAILURE; + } + + double edge = 16.0; + const char* edgeOpt = cmdLine.findOption('e'); + if (NULL != edgeOpt) + { + edge = atof(edgeOpt); + } + + int width; + int height; + int comp; + + stbi_uc* img = stbi_load(inFilePath, &width, &height, &comp, 1); + + if (NULL == img) + { + fprintf(stderr, "Failed to load %s.\n", inFilePath); + return EXIT_FAILURE; + } + + uint32_t size = width*height; + + double* imgIn = (double*)malloc(size*sizeof(double) ); + double* outside = (double*)malloc(size*sizeof(double) ); + double* inside = (double*)malloc(size*sizeof(double) ); + + for (uint32_t ii = 0; ii < size; ++ii) + { + imgIn[ii] = double(img[ii])/255.0; + } + + edtaa3(imgIn, width, height, outside); + + for (uint32_t ii = 0; ii < size; ++ii) + { + imgIn[ii] = 1.0 - imgIn[ii]; + } + + edtaa3(imgIn, width, height, inside); + + free(imgIn); + + uint8_t* grayscale = (uint8_t*)malloc(size); + + double edgeOffset = edge*0.5; + double invEdge = 1.0/edge; + + for (uint32_t ii = 0; ii < size; ++ii) + { + double dist = saturate( ( (outside[ii] - inside[ii])+edgeOffset) * invEdge); + grayscale[ii] = 255-uint8_t(dist * 255.0); + } + + free(inside); + free(outside); + + saveTga(outFilePath, width, height, width, true, grayscale); + + free(grayscale); + + return EXIT_SUCCESS; +} diff --git a/tools/shaderc/shaderc.cpp b/tools/shaderc/shaderc.cpp index 7e126238..85f0503c 100644 --- a/tools/shaderc/shaderc.cpp +++ b/tools/shaderc/shaderc.cpp @@ -30,8 +30,8 @@ extern "C" # define BX_TRACE(_format, ...) fprintf(stderr, "" _format "\n", ##__VA_ARGS__) #endif // DEBUG -#define BGFX_CHUNK_MAGIC_VSH BX_MAKEFOURCC('V', 'S', 'H', 0x1) -#define BGFX_CHUNK_MAGIC_FSH BX_MAKEFOURCC('F', 'S', 'H', 0x1) +#define BGFX_CHUNK_MAGIC_VSH BX_MAKEFOURCC('V', 'S', 'H', 0x1) +#define BGFX_CHUNK_MAGIC_FSH BX_MAKEFOURCC('F', 'S', 'H', 0x1) #include <bx/bx.h> @@ -1247,11 +1247,11 @@ void help(const char* _error = NULL) " --bin2c <file path> Generate C header file.\n" " --depends <file path> Generate makefile style depends file.\n" " --platform <platform> Target platform.\n" - " android\n" - " ios\n" - " linux\n" - " nacl\n" - " osx\n" + " android\n" + " ios\n" + " linux\n" + " nacl\n" + " osx\n" " windows\n" " --type <type> Shader type (vertex, fragment)\n" " --varyingdef <file path> Path to varying.def.sc file.\n" @@ -1483,23 +1483,23 @@ int main(int _argc, const char* _argv[]) } } - const size_t padding = 16; - uint32_t size = (uint32_t)fsize(file); - char* data = new char[size+padding+1]; - size = (uint32_t)fread(data, 1, size, file); - // Compiler generates "error X3000: syntax error: unexpected end of file" - // if input doesn't have empty line at EOF. - data[size] = '\n'; - memset(&data[size+1], 0, padding); - fclose(file); - - char* entry = strstr(data, "void main()"); - if (NULL == entry) - { - fprintf(stderr, "Shader entry point 'void main()' is not found.\n"); - } - else - { + const size_t padding = 16; + uint32_t size = (uint32_t)fsize(file); + char* data = new char[size+padding+1]; + size = (uint32_t)fread(data, 1, size, file); + // Compiler generates "error X3000: syntax error: unexpected end of file" + // if input doesn't have empty line at EOF. + data[size] = '\n'; + memset(&data[size+1], 0, padding); + fclose(file); + + char* entry = strstr(data, "void main()"); + if (NULL == entry) + { + fprintf(stderr, "Shader entry point 'void main()' is not found.\n"); + } + else + { InOut shaderInputs; InOut shaderOutputs; uint32_t inputHash = 0; diff --git a/tools/texturec/texturec.cpp b/tools/texturec/texturec.cpp index ee7bc627..0d99316a 100644 --- a/tools/texturec/texturec.cpp +++ b/tools/texturec/texturec.cpp @@ -1,188 +1,188 @@ -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -// Just hacking DDS loading code in here. -#include "bgfx_p.h" -using namespace bgfx; - -#include "dds.h" - -#if 0 -# define BX_TRACE(_format, ...) fprintf(stderr, "" _format "\n", ##__VA_ARGS__) -#endif // DEBUG - -#include <bx/bx.h> -#include <bx/commandline.h> -#include <bx/uint32_t.h> - -namespace bgfx -{ - const Memory* alloc(uint32_t _size) - { - Memory* mem = (Memory*)::realloc(NULL, sizeof(Memory) + _size); - mem->size = _size; - mem->data = (uint8_t*)mem + sizeof(Memory); - return mem; - } - - void saveTga(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, bool _grayscale, bool _yflip) - { - FILE* file = fopen(_filePath, "wb"); - if ( NULL != file ) - { - uint8_t type = _grayscale ? 3 : 2; - uint8_t bpp = _grayscale ? 8 : 32; - - putc(0, file); - putc(0, file); - putc(type, file); - putc(0, file); - putc(0, file); - putc(0, file); - putc(0, file); - putc(0, file); - putc(0, file); - putc(0, file); - putc(0, file); - putc(0, file); - putc(_width&0xff, file); - putc( (_width>>8)&0xff, file); - putc(_height&0xff, file); - putc( (_height>>8)&0xff, file); - putc(bpp, file); - putc(32, file); - - uint32_t dstPitch = _width*bpp/8; - if (_yflip) - { - uint8_t* data = (uint8_t*)_src + dstPitch*_height - _srcPitch; - for (uint32_t yy = 0; yy < _height; ++yy) - { - fwrite(data, dstPitch, 1, file); - data -= _srcPitch; - } - } - else - { - uint8_t* data = (uint8_t*)_src; - for (uint32_t yy = 0; yy < _height; ++yy) - { - fwrite(data, dstPitch, 1, file); - data += _srcPitch; - } - } - - fclose(file); - } - } -} - -long int fsize(FILE* _file) -{ - long int pos = ftell(_file); - fseek(_file, 0L, SEEK_END); - long int size = ftell(_file); - fseek(_file, pos, SEEK_SET); - return size; -} - -int main(int _argc, const char* _argv[]) -{ - bx::CommandLine cmdLine(_argc, _argv); - - FILE* file = fopen(_argv[1], "rb"); - uint32_t size = fsize(file); - const Memory* mem = alloc(size); - size_t readSize = fread(mem->data, 1, size, file); - BX_UNUSED(readSize); - fclose(file); - - Dds dds; - - if (parseDds(dds, mem) ) - { - bool decompress = cmdLine.hasArg('d'); - - if (decompress - || 0 == dds.m_type) - { - for (uint8_t side = 0, numSides = dds.m_cubeMap ? 6 : 1; side < numSides; ++side) - { - uint32_t width = dds.m_width; - uint32_t height = dds.m_height; - - for (uint32_t lod = 0, num = dds.m_numMips; lod < num; ++lod) - { - width = uint32_max(1, width); - height = uint32_max(1, height); - - Mip mip; - if (getRawImageData(dds, side, lod, mem, mip) ) - { - uint32_t dstpitch = width*4; - uint8_t* bits = (uint8_t*)malloc(dstpitch*height); - - if (width != mip.m_width - || height != mip.m_height) - { - uint8_t* temp = (uint8_t*)realloc(NULL, mip.m_width*mip.m_height*4); - mip.decode(temp); - uint32_t srcpitch = mip.m_width*4; - - for (uint32_t yy = 0; yy < height; ++yy) - { - uint8_t* src = &temp[yy*srcpitch]; - uint8_t* dst = &bits[yy*dstpitch]; - - for (uint32_t xx = 0; xx < width; ++xx) - { - memcpy(dst, src, 4); - dst += 4; - src += 4; - } - } - - free(temp); - } - else - { - mip.decode(bits); - } - - char filePath[256]; - bx::snprintf(filePath, sizeof(filePath), "mip%d_%d.tga", side, lod); - - saveTga(filePath, width, height, dstpitch, bits); - free(bits); - } - - width >>= 1; - height >>= 1; - } - } - } - else - { - for (uint32_t lod = 0, num = dds.m_numMips; lod < num; ++lod) - { - Mip mip; - if (getRawImageData(dds, 0, lod, mem, mip) ) - { - char filePath[256]; - bx::snprintf(filePath, sizeof(filePath), "mip%d.bin", lod); - file = fopen(filePath, "wb"); - fwrite(mip.m_data, 1, mip.m_size, file); - fclose(file); - } - } - } - } - - return EXIT_SUCCESS; -} +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +// Just hacking DDS loading code in here. +#include "bgfx_p.h" +using namespace bgfx; + +#include "dds.h" + +#if 0 +# define BX_TRACE(_format, ...) fprintf(stderr, "" _format "\n", ##__VA_ARGS__) +#endif // DEBUG + +#include <bx/bx.h> +#include <bx/commandline.h> +#include <bx/uint32_t.h> + +namespace bgfx +{ + const Memory* alloc(uint32_t _size) + { + Memory* mem = (Memory*)::realloc(NULL, sizeof(Memory) + _size); + mem->size = _size; + mem->data = (uint8_t*)mem + sizeof(Memory); + return mem; + } + + void saveTga(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, bool _grayscale, bool _yflip) + { + FILE* file = fopen(_filePath, "wb"); + if ( NULL != file ) + { + uint8_t type = _grayscale ? 3 : 2; + uint8_t bpp = _grayscale ? 8 : 32; + + putc(0, file); + putc(0, file); + putc(type, file); + putc(0, file); + putc(0, file); + putc(0, file); + putc(0, file); + putc(0, file); + putc(0, file); + putc(0, file); + putc(0, file); + putc(0, file); + putc(_width&0xff, file); + putc( (_width>>8)&0xff, file); + putc(_height&0xff, file); + putc( (_height>>8)&0xff, file); + putc(bpp, file); + putc(32, file); + + uint32_t dstPitch = _width*bpp/8; + if (_yflip) + { + uint8_t* data = (uint8_t*)_src + dstPitch*_height - _srcPitch; + for (uint32_t yy = 0; yy < _height; ++yy) + { + fwrite(data, dstPitch, 1, file); + data -= _srcPitch; + } + } + else + { + uint8_t* data = (uint8_t*)_src; + for (uint32_t yy = 0; yy < _height; ++yy) + { + fwrite(data, dstPitch, 1, file); + data += _srcPitch; + } + } + + fclose(file); + } + } +} + +long int fsize(FILE* _file) +{ + long int pos = ftell(_file); + fseek(_file, 0L, SEEK_END); + long int size = ftell(_file); + fseek(_file, pos, SEEK_SET); + return size; +} + +int main(int _argc, const char* _argv[]) +{ + bx::CommandLine cmdLine(_argc, _argv); + + FILE* file = fopen(_argv[1], "rb"); + uint32_t size = fsize(file); + const Memory* mem = alloc(size); + size_t readSize = fread(mem->data, 1, size, file); + BX_UNUSED(readSize); + fclose(file); + + Dds dds; + + if (parseDds(dds, mem) ) + { + bool decompress = cmdLine.hasArg('d'); + + if (decompress + || 0 == dds.m_type) + { + for (uint8_t side = 0, numSides = dds.m_cubeMap ? 6 : 1; side < numSides; ++side) + { + uint32_t width = dds.m_width; + uint32_t height = dds.m_height; + + for (uint32_t lod = 0, num = dds.m_numMips; lod < num; ++lod) + { + width = uint32_max(1, width); + height = uint32_max(1, height); + + Mip mip; + if (getRawImageData(dds, side, lod, mem, mip) ) + { + uint32_t dstpitch = width*4; + uint8_t* bits = (uint8_t*)malloc(dstpitch*height); + + if (width != mip.m_width + || height != mip.m_height) + { + uint8_t* temp = (uint8_t*)realloc(NULL, mip.m_width*mip.m_height*4); + mip.decode(temp); + uint32_t srcpitch = mip.m_width*4; + + for (uint32_t yy = 0; yy < height; ++yy) + { + uint8_t* src = &temp[yy*srcpitch]; + uint8_t* dst = &bits[yy*dstpitch]; + + for (uint32_t xx = 0; xx < width; ++xx) + { + memcpy(dst, src, 4); + dst += 4; + src += 4; + } + } + + free(temp); + } + else + { + mip.decode(bits); + } + + char filePath[256]; + bx::snprintf(filePath, sizeof(filePath), "mip%d_%d.tga", side, lod); + + saveTga(filePath, width, height, dstpitch, bits); + free(bits); + } + + width >>= 1; + height >>= 1; + } + } + } + else + { + for (uint32_t lod = 0, num = dds.m_numMips; lod < num; ++lod) + { + Mip mip; + if (getRawImageData(dds, 0, lod, mem, mip) ) + { + char filePath[256]; + bx::snprintf(filePath, sizeof(filePath), "mip%d.bin", lod); + file = fopen(filePath, "wb"); + fwrite(mip.m_data, 1, mip.m_size, file); + fclose(file); + } + } + } + } + + return EXIT_SUCCESS; +}