Use VBO to draw UI, just because that's how it's supposed to be done.

This commit is contained in:
Henrik Rydgård 2013-01-10 10:14:39 +01:00
parent 54fdd6ea00
commit 20c37d8d0e
2 changed files with 41 additions and 11 deletions

View File

@ -21,12 +21,34 @@ DrawBuffer::DrawBuffer() : count_(0), atlas(0) {
verts_ = new Vertex[MAX_VERTS];
fontscalex = 1.0f;
fontscaley = 1.0f;
inited_ = false;
register_gl_resource_holder(this);
}
DrawBuffer::~DrawBuffer() {
unregister_gl_resource_holder(this);
delete [] verts_;
}
void DrawBuffer::Init() {
if (inited_)
return;
inited_ = true;
glGenBuffers(1, (GLuint *)&vbo_);
}
void DrawBuffer::Shutdown() {
glDeleteBuffers(1, (GLuint *)&vbo_);
vbo_ = 0;
inited_ = false;
}
void DrawBuffer::GLLost() {
inited_ = false;
Init();
}
void DrawBuffer::Begin(DrawBufferMode dbmode) {
Init();
count_ = 0;
mode_ = dbmode;
}
@ -38,11 +60,12 @@ void DrawBuffer::End() {
void DrawBuffer::Flush(const GLSLProgram *program, bool set_blend_state) {
if (count_ == 0)
return;
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, vbo_);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * count_, verts_, GL_STREAM_DRAW);
if (set_blend_state) {
glstate.blend.enable();
glstate.blendFunc.set(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glstate.blend.enable();
glstate.blendFunc.set(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
glUniform1i(program->sampler0, 0);
glEnableVertexAttribArray(program->a_position);
@ -50,10 +73,10 @@ void DrawBuffer::Flush(const GLSLProgram *program, bool set_blend_state) {
if (program->a_texcoord0 != -1)
glEnableVertexAttribArray(program->a_texcoord0);
GL_CHECK();
glVertexAttribPointer(program->a_position, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), &verts_[0].x);
glVertexAttribPointer(program->a_color, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), &verts_[0].rgba);
glVertexAttribPointer(program->a_position, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)offsetof(Vertex, x));
glVertexAttribPointer(program->a_color, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (void *)offsetof(Vertex, rgba));
if (program->a_texcoord0 != -1)
glVertexAttribPointer(program->a_texcoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), &verts_[0].u);
glVertexAttribPointer(program->a_texcoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)offsetof(Vertex, u));
glDrawArrays(mode_ == DBMODE_LINES ? GL_LINES : GL_TRIANGLES, 0, count_);
GL_CHECK();
glDisableVertexAttribArray(program->a_position);
@ -61,6 +84,7 @@ void DrawBuffer::Flush(const GLSLProgram *program, bool set_blend_state) {
if (program->a_texcoord0 != -1)
glDisableVertexAttribArray(program->a_texcoord0);
GL_CHECK();
glBindBuffer(GL_ARRAY_BUFFER, 0);
count_ = 0;
}

View File

@ -5,6 +5,7 @@
#include "base/basictypes.h"
#include "base/color.h"
#include "base/display.h"
#include "gfx/gl_lost_manager.h"
struct Atlas;
@ -36,13 +37,12 @@ enum DrawBufferMode {
DBMODE_LINES = 1
};
struct GradientStop
{
struct GradientStop {
float t;
uint32_t color;
};
class DrawBuffer {
class DrawBuffer : public GfxResourceHolder {
public:
DrawBuffer();
~DrawBuffer();
@ -50,10 +50,15 @@ public:
void Begin(DrawBufferMode mode = DBMODE_NORMAL);
void End();
// TODO: Enforce these. Now Init is autocalled and shutdown not called.
void Init();
void Shutdown();
virtual void GLLost();
int Count() const { return count_; }
void Flush(const GLSLProgram *program, bool set_blend_state=true);
void Rect(float x, float y, float w, float h, uint32 color, int align = ALIGN_TOPLEFT);
void hLine(float x1, float y, float x2, uint32 color) { Rect(x1, y, x2 - x1, pixel_in_dps, color); }
void vLine(float x, float y1, float y2, uint32 color) { Rect(x, y1, pixel_in_dps, y2 - y1, color); }
@ -121,7 +126,6 @@ public:
void SetClipRect(float x1, float y1, float x2, float y2);
void NoClip();
private:
void DoAlign(int flags, float *x, float *y, float *w, float *h);
struct Vertex {
@ -130,11 +134,13 @@ private:
float u, v;
};
int vbo_;
Vertex *verts_;
int count_;
DrawBufferMode mode_;
const Atlas *atlas;
bool inited_;
float fontscalex;
float fontscaley;
};