mirror of https://github.com/buggins/dlangui.git
optimize OpenGL backend -- use batch drawing; part 5 for #271
This commit is contained in:
parent
cd4f101723
commit
fef5858955
|
|
@ -116,6 +116,13 @@ struct Rect {
|
||||||
right = x1;
|
right = x1;
|
||||||
bottom = y1;
|
bottom = y1;
|
||||||
}
|
}
|
||||||
|
/// constructs rectangle using two points - (left, top), (right, bottom) coordinates
|
||||||
|
this(Point pt0, Point pt1) {
|
||||||
|
left = pt0.x;
|
||||||
|
top = pt0.y;
|
||||||
|
right = pt1.x;
|
||||||
|
bottom = pt1.y;
|
||||||
|
}
|
||||||
/// returns true if rectangle is empty (right <= left || bottom <= top)
|
/// returns true if rectangle is empty (right <= left || bottom <= top)
|
||||||
@property bool empty() {
|
@property bool empty() {
|
||||||
return right <= left || bottom <= top;
|
return right <= left || bottom <= top;
|
||||||
|
|
|
||||||
|
|
@ -713,7 +713,7 @@ public:
|
||||||
_color = color;
|
_color = color;
|
||||||
}
|
}
|
||||||
override void draw() {
|
override void draw() {
|
||||||
glSupport.drawLine(_p1, _p2, _color, _color);
|
glSupport.drawLines([Rect(_p1, _p2)], [_color, _color]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -900,29 +900,64 @@ final class GLSupport {
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawLine(Point p1, Point p2, uint color1, uint color2) {
|
/// one rect is one line (left, top) - (right, bottom); for one line there are two color items
|
||||||
Color[2] colors;
|
void drawLines(Rect[] lines, uint[] vertexColors) {
|
||||||
FillColor(color1, colors[0..1]);
|
Color[] colors;
|
||||||
FillColor(color2, colors[1..2]);
|
colors.length = vertexColors.length;
|
||||||
float x0 = cast(float)(p1.x);
|
for (uint i = 0; i < vertexColors.length; i++)
|
||||||
float y0 = cast(float)(bufferDy-p1.y);
|
FillColor(vertexColors[i], colors[i .. i + 1]);
|
||||||
float x1 = cast(float)(p2.x);
|
|
||||||
float y1 = cast(float)(bufferDy-p2.y);
|
|
||||||
|
|
||||||
// don't flip for framebuffer
|
float[] vertexArray;
|
||||||
if (currentFBO) {
|
vertexArray.assumeSafeAppend();
|
||||||
y0 = cast(float)(p1.y);
|
for (uint i = 0; i < lines.length; i++) {
|
||||||
y1 = cast(float)(p2.y);
|
Rect rc = lines[i];
|
||||||
|
|
||||||
|
float x0 = cast(float)(rc.left);
|
||||||
|
float y0 = cast(float)(bufferDy-rc.top);
|
||||||
|
float x1 = cast(float)(rc.right);
|
||||||
|
float y1 = cast(float)(bufferDy-rc.bottom);
|
||||||
|
|
||||||
|
// don't flip for framebuffer
|
||||||
|
if (currentFBO) {
|
||||||
|
y0 = cast(float)(rc.top);
|
||||||
|
y1 = cast(float)(rc.bottom);
|
||||||
|
}
|
||||||
|
|
||||||
|
vertexArray ~= x0;
|
||||||
|
vertexArray ~= y0;
|
||||||
|
vertexArray ~= Z_2D;
|
||||||
|
vertexArray ~= x1;
|
||||||
|
vertexArray ~= y1;
|
||||||
|
vertexArray ~= Z_2D;
|
||||||
}
|
}
|
||||||
|
|
||||||
float[3 * 2] vertices = [
|
int[] indexes = makeLineIndexesArray(lines.length);
|
||||||
x0,y0,Z_2D,
|
|
||||||
x1,y1,Z_2D
|
if (_legacyMode) {
|
||||||
];
|
static if (SUPPORT_LEGACY_OPENGL) {
|
||||||
if (_lineProgram !is null) {
|
glColor4f(1,1,1,1);
|
||||||
_lineProgram.execute(vertices, cast(float[])colors);
|
glDisable(GL_CULL_FACE);
|
||||||
} else
|
glEnable(GL_BLEND);
|
||||||
Log.e("No program");
|
glDisable(GL_ALPHA_TEST);
|
||||||
|
checkgl!glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
checkgl!glEnableClientState(GL_VERTEX_ARRAY);
|
||||||
|
checkgl!glEnableClientState(GL_COLOR_ARRAY);
|
||||||
|
checkgl!glVertexPointer(3, GL_FLOAT, 0, cast(void*)vertexArray.ptr);
|
||||||
|
checkgl!glColorPointer(4, GL_FLOAT, 0, cast(void*)colors.ptr);
|
||||||
|
|
||||||
|
checkgl!glDrawElements(GL_LINES, cast(int)indexes.length, GL_UNSIGNED_INT, cast(void*)indexes.ptr);
|
||||||
|
|
||||||
|
glDisableClientState(GL_COLOR_ARRAY);
|
||||||
|
glDisableClientState(GL_VERTEX_ARRAY);
|
||||||
|
glDisable(GL_ALPHA_TEST);
|
||||||
|
glDisable(GL_BLEND);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (_lineProgram !is null) {
|
||||||
|
_lineProgram.execute(vertexArray, cast(float[])colors, indexes);
|
||||||
|
} else
|
||||||
|
Log.e("No program");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static immutable float Z_2D = -2.0f;
|
static immutable float Z_2D = -2.0f;
|
||||||
|
|
@ -942,6 +977,17 @@ final class GLSupport {
|
||||||
return indexes;
|
return indexes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// make indexes for LINES
|
||||||
|
protected int[] makeLineIndexesArray(size_t lineCount) {
|
||||||
|
int[] indexes;
|
||||||
|
indexes.assumeSafeAppend();
|
||||||
|
for (uint i = 0; i < lineCount; i++) {
|
||||||
|
indexes ~= i * 2 + 0;
|
||||||
|
indexes ~= i * 2 + 1;
|
||||||
|
}
|
||||||
|
return indexes;
|
||||||
|
}
|
||||||
|
|
||||||
void drawSolidFillRects(Rect[] rects, uint[] vertexColors) {
|
void drawSolidFillRects(Rect[] rects, uint[] vertexColors) {
|
||||||
Color[] colors;
|
Color[] colors;
|
||||||
colors.length = vertexColors.length;
|
colors.length = vertexColors.length;
|
||||||
|
|
@ -985,16 +1031,11 @@ final class GLSupport {
|
||||||
checkgl!glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
checkgl!glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
checkgl!glEnableClientState(GL_VERTEX_ARRAY);
|
checkgl!glEnableClientState(GL_VERTEX_ARRAY);
|
||||||
checkgl!glEnableClientState(GL_COLOR_ARRAY);
|
checkgl!glEnableClientState(GL_COLOR_ARRAY);
|
||||||
//checkgl!glEnableClientState(GL_INDEX_ARRAY);
|
|
||||||
checkgl!glVertexPointer(3, GL_FLOAT, 0, cast(void*)vertexArray.ptr);
|
checkgl!glVertexPointer(3, GL_FLOAT, 0, cast(void*)vertexArray.ptr);
|
||||||
checkgl!glColorPointer(4, GL_FLOAT, 0, cast(void*)colors.ptr);
|
checkgl!glColorPointer(4, GL_FLOAT, 0, cast(void*)colors.ptr);
|
||||||
//checkgl!glIndexPointer(GL_INT, 0, cast(void*)indexes.ptr);
|
|
||||||
|
|
||||||
//checkgl!glDrawArrays(GL_TRIANGLES, 0, cast(int)indexes.length);
|
|
||||||
//checkgl!glDrawElements(GL_TRIANGLES, cast(int)indexes.length, GL_UNSIGNED_INT, cast(void*)null);
|
|
||||||
checkgl!glDrawElements(GL_TRIANGLES, cast(int)indexes.length, GL_UNSIGNED_INT, cast(void*)indexes.ptr);
|
checkgl!glDrawElements(GL_TRIANGLES, cast(int)indexes.length, GL_UNSIGNED_INT, cast(void*)indexes.ptr);
|
||||||
|
|
||||||
//glDisableClientState(GL_INDEX_ARRAY);
|
|
||||||
glDisableClientState(GL_COLOR_ARRAY);
|
glDisableClientState(GL_COLOR_ARRAY);
|
||||||
glDisableClientState(GL_VERTEX_ARRAY);
|
glDisableClientState(GL_VERTEX_ARRAY);
|
||||||
glDisable(GL_ALPHA_TEST);
|
glDisable(GL_ALPHA_TEST);
|
||||||
|
|
@ -1066,17 +1107,12 @@ final class GLSupport {
|
||||||
checkgl!glEnableClientState(GL_COLOR_ARRAY);
|
checkgl!glEnableClientState(GL_COLOR_ARRAY);
|
||||||
checkgl!glEnableClientState(GL_VERTEX_ARRAY);
|
checkgl!glEnableClientState(GL_VERTEX_ARRAY);
|
||||||
checkgl!glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
checkgl!glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
//checkgl!glEnableClientState(GL_INDEX_ARRAY);
|
|
||||||
checkgl!glVertexPointer(3, GL_FLOAT, 0, cast(void*)vertexArray.ptr);
|
checkgl!glVertexPointer(3, GL_FLOAT, 0, cast(void*)vertexArray.ptr);
|
||||||
checkgl!glTexCoordPointer(2, GL_FLOAT, 0, cast(void*)txcoordArray.ptr);
|
checkgl!glTexCoordPointer(2, GL_FLOAT, 0, cast(void*)txcoordArray.ptr);
|
||||||
checkgl!glColorPointer(4, GL_FLOAT, 0, cast(void*)colors.ptr);
|
checkgl!glColorPointer(4, GL_FLOAT, 0, cast(void*)colors.ptr);
|
||||||
//checkgl!glIndexPointer(GL_INT, 0, cast(void*)indexes.ptr);
|
|
||||||
|
|
||||||
//checkgl!glDrawArrays(GL_TRIANGLES, 0, cast(int)indexes.length);
|
|
||||||
//checkgl!glDrawElements(GL_TRIANGLES, cast(int)indexes.length, GL_UNSIGNED_INT, cast(void*)null);
|
|
||||||
checkgl!glDrawElements(GL_TRIANGLES, cast(int)indexes.length, GL_UNSIGNED_INT, cast(void*)indexes.ptr);
|
checkgl!glDrawElements(GL_TRIANGLES, cast(int)indexes.length, GL_UNSIGNED_INT, cast(void*)indexes.ptr);
|
||||||
|
|
||||||
//glDisableClientState(GL_INDEX_ARRAY);
|
|
||||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
glDisableClientState(GL_VERTEX_ARRAY);
|
glDisableClientState(GL_VERTEX_ARRAY);
|
||||||
glDisableClientState(GL_COLOR_ARRAY);
|
glDisableClientState(GL_COLOR_ARRAY);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue