commit e133f2247eebcdf20685a4badbd47f21a9da51f2
parent 279d42bec6c595c2250f586450946f2af536d757
Author: Brian Swetland <swetland@frotz.net>
Date: Fri, 13 Sep 2013 00:56:53 -0700
textgrid: deal with radeon GPUs, add color support
Radeon GPUs apparently cannot deal with attributes of size byte
with a count or stride less than 4. Since we have to pad out to
four bytes, use the other three for per-character RGB color.
Diffstat:
3 files changed, 27 insertions(+), 15 deletions(-)
diff --git a/common/assets/textgrid.glsl b/common/assets/textgrid.glsl
@@ -3,27 +3,28 @@
-- vertex
-layout (location = 0) in vec4 aPosition;
+layout (location = 0) in vec2 aPosition;
layout (location = 1) in vec2 aTexCoord;
-layout (location = 2) in uint aCharacter;
+layout (location = 2) in uvec4 aCharacter;
out vec2 vTexCoord;
+out vec3 vColor;
void main() {
ivec2 cell = uTextGrid.xy;
ivec2 dims = uTextGrid.zw;
- vec4 pos = aPosition;
- int id = gl_InstanceID;
+ vec4 pos = vec4(aPosition,0,1);
+ uint id = gl_InstanceID;
// translate cell to destination
pos.xy += vec2(id % dims.x, id / dims.x) * cell;
// adjust unit texture coord to font cell rectangle
- vec2 tadj = vec2(aCharacter % uint(16), aCharacter / uint(16)) / 16.0;
+ vec2 tadj = vec2(aCharacter.x % uint(16), aCharacter.x / uint(16)) / 16.0;
// translate texture coordinates to character position
vTexCoord = aTexCoord + tadj + vec2(1.0/256.0,1.0/256.0);
-
+ vColor = aCharacter.yzw / 255.0;
pos = uOrtho * pos;
// discard via clipping
@@ -34,10 +35,12 @@ void main() {
-- fragment
-uniform sampler2D uTexture0;
+uniform sampler2D sampler0;
in vec2 vTexCoord;
+in vec3 vColor;
void main() {
- gl_FragColor = texture2D(uTexture0, vTexCoord);
+ float alpha = texture2D(sampler0, vTexCoord).r;
+ gl_FragColor = vec4(vColor.xyz * step(1.0,alpha), alpha);
}
diff --git a/common/textgrid.cc b/common/textgrid.cc
@@ -26,7 +26,7 @@
static VertexAttrDesc layout[] = {
{ 0, SRC_FLOAT, DST_FLOAT, 2, 0, 16, 0 },
{ 1, SRC_FLOAT, DST_FLOAT, 2, 8, 16, 0 },
- { 2, SRC_UINT8, DST_INTEGER, 1, 0, 1, 1 },
+ { 2, SRC_UINT8, DST_INTEGER, 4, 0, 4, 1 },
};
static float unit_box_2d[] = {
@@ -47,7 +47,7 @@ void TextGrid::resize(int columns, int rows) {
if (grid)
delete[] grid;
- grid = new unsigned char[width * height];
+ grid = new unsigned[width * height * 4];
clear();
}
@@ -76,16 +76,21 @@ int TextGrid::init(int cellw, int cellh, int columns, int rows) {
return -1;
vtx.load(box_2d, sizeof(box_2d));
- cbuf.load(grid, width * height);
+ cbuf.load(grid, width * height * sizeof(unsigned));
attr.init(layout, data, sizeof(layout) / sizeof(layout[0]));
+ color = 0xFFFFFF00;
return 0;
}
+void TextGrid::setColor(unsigned rgba) {
+ color = rgba << 8;
+}
+
void TextGrid::render(void) {
if (dirty) {
dirty = 0;
- cbuf.load(grid, width * height);
+ cbuf.load(grid, width * height * sizeof(unsigned));
}
attr.use();
texture.use(0);
@@ -94,7 +99,7 @@ void TextGrid::render(void) {
}
void TextGrid::clear(void) {
- memset(grid, 0, width * height);
+ memset(grid, 0, width * height * sizeof(unsigned));
}
void TextGrid::printf(int x, int y, const char *fmt, ...) {
@@ -107,6 +112,8 @@ void TextGrid::printf(int x, int y, const char *fmt, ...) {
buf[127] = 0;
if (len > 127) len = 127;
if (y < 0) y = height + y;
- memcpy(grid + y * width + x, buf, len); // TODO rangecheck
+ for (int n = 0; n < len; n++) {
+ grid[y * width + x + n] = buf[n] | color;
+ }
dirty = 1;
}
diff --git a/common/textgrid.h b/common/textgrid.h
@@ -25,11 +25,13 @@ public:
void render(void);
void clear(void);
void printf(int x, int y, const char *fmt, ...);
+ void setColor(unsigned rgba);
private:
int width;
int height;
int dirty;
+ unsigned color;
UniformBuffer ubuf;
VertexBuffer vtx;
@@ -38,7 +40,7 @@ private:
Texture2D texture;
VertexAttributes attr;
- unsigned char *grid;
+ unsigned *grid;
};
#endif