From 808861a6d38cc016b59d32ff813c8d44e4818caf Mon Sep 17 00:00:00 2001 From: gazer Date: Fri, 11 Dec 2015 15:32:47 +0300 Subject: [PATCH 01/14] fix github highlighting --- src/dlangui/graphics/glsupport.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dlangui/graphics/glsupport.d b/src/dlangui/graphics/glsupport.d index 53599bc5..04e6e0d9 100644 --- a/src/dlangui/graphics/glsupport.d +++ b/src/dlangui/graphics/glsupport.d @@ -185,7 +185,7 @@ class GLProgram { Log.d("Program compiled successfully"); //glDetachShader(program, vertexShader); //glDetachShader(program, fragmentShader); - Log.v("trying glUseProgram(0)"); + Log.v("trying glUseProgram with 0"); glUseProgram(0); Log.v("before useProgram"); glUseProgram(program); From 15c11108288cc265e5a2f8000c63d468cc877fed Mon Sep 17 00:00:00 2001 From: gazer Date: Fri, 11 Dec 2015 15:54:06 +0300 Subject: [PATCH 02/14] few errors checking fixes --- src/dlangui/graphics/glsupport.d | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/dlangui/graphics/glsupport.d b/src/dlangui/graphics/glsupport.d index 04e6e0d9..0324d122 100644 --- a/src/dlangui/graphics/glsupport.d +++ b/src/dlangui/graphics/glsupport.d @@ -69,7 +69,9 @@ static this() { 0x0500: "GL_INVALID_ENUM", 0x0501: "GL_INVALID_VALUE", 0x0502: "GL_INVALID_OPERATION", - 0x0505: "GL_OUT_OF_MEMORY" + 0x0505: "GL_OUT_OF_MEMORY", + 0x0506: "GL_INVALID_FRAMEBUFFER_OPERATION", + 0x0507: "GL_CONTEXT_LOST" ]; } /** @@ -81,7 +83,7 @@ bool checkError(string context="", string file=__FILE__, int line=__LINE__) GLenum err = glGetError(); if (err != GL_NO_ERROR) { - Log.e("OpenGL error ", err in errors ? errors[err] : to!string(err), " at ", file, ":", line, " -- ", context); + Log.e("OpenGL error ", errors.get(err, to!string(err)), " at ", file, ":", line, " -- ", context); return true; } return false; From 8015acaca23d55041b8786856d474ecc42be8e95 Mon Sep 17 00:00:00 2001 From: gazer Date: Fri, 11 Dec 2015 16:33:55 +0300 Subject: [PATCH 03/14] fix shader locations --- src/dlangui/graphics/glsupport.d | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/dlangui/graphics/glsupport.d b/src/dlangui/graphics/glsupport.d index 0324d122..e520d899 100644 --- a/src/dlangui/graphics/glsupport.d +++ b/src/dlangui/graphics/glsupport.d @@ -300,15 +300,15 @@ class SolidFillProgram : GLProgram { matrixLocation = glGetUniformLocation(program, "matrix"); checkError("glGetUniformLocation matrix"); - if (matrixLocation == 0) + if (matrixLocation == -1) Log.e("glGetUniformLocation failed for matrixLocation"); vertexLocation = glGetAttribLocation(program, "vertex"); checkError("glGetAttribLocation vertex"); - if (vertexLocation == 0) + if (vertexLocation == -1) Log.e("glGetUniformLocation failed for vertexLocation"); colAttrLocation = glGetAttribLocation(program, "colAttr"); checkError("glGetAttribLocation colAttr"); - if (colAttrLocation == 0) + if (colAttrLocation == -1) Log.e("glGetUniformLocation failed for colAttrLocation"); return res && matrixLocation >= 0 && vertexLocation >= 0 && colAttrLocation >= 0; } From a3ec66569d6d279b395fb3b76b034d08a27f6970 Mon Sep 17 00:00:00 2001 From: gazer Date: Fri, 11 Dec 2015 17:50:49 +0300 Subject: [PATCH 04/14] clean shader compilation --- src/dlangui/graphics/glsupport.d | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/dlangui/graphics/glsupport.d b/src/dlangui/graphics/glsupport.d index e520d899..f194f491 100644 --- a/src/dlangui/graphics/glsupport.d +++ b/src/dlangui/graphics/glsupport.d @@ -124,10 +124,9 @@ class GLProgram { compatibilityFixes(sourceCode, type); Log.d("compileShader glsl=", glslversion, " type:", (type == GL_VERTEX_SHADER ? "GL_VERTEX_SHADER" : (type == GL_FRAGMENT_SHADER ? "GL_FRAGMENT_SHADER" : "UNKNOWN")), " code:\n", sourceCode); - GLuint shader = glCreateShader(type);//GL_VERTEX_SHADER + GLuint shader = glCreateShader(type); const char * psrc = sourceCode.toStringz; - GLuint len = cast(uint)sourceCode.length; - glShaderSource(shader, 1, &psrc, cast(const(int)*)&len); + glShaderSource(shader, 1, &psrc, null); glCompileShader(shader); GLint compiled; glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); @@ -141,9 +140,8 @@ class GLProgram { if (blen > 1) { GLchar[] msg = new GLchar[blen + 1]; - GLchar * pmsg = &msg[0]; - glGetShaderInfoLog(shader, blen, &slen, pmsg); - Log.d("Shader compilation error: ", fromStringz(pmsg)); + glGetShaderInfoLog(shader, blen, &slen, msg.ptr); + Log.d("Shader compilation error: ", fromStringz(msg.ptr)); } return 0; } @@ -178,15 +176,12 @@ class GLProgram { GLint maxLength = 0; glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength); GLchar[] msg = new GLchar[maxLength + 1]; - GLchar * pmsg = &msg[0]; - glGetProgramInfoLog(program, maxLength, &maxLength, pmsg); - Log.e("Error while linking program: ", fromStringz(pmsg)); + glGetProgramInfoLog(program, maxLength, &maxLength, msg.ptr); + Log.e("Error while linking program: ", fromStringz(msg.ptr)); error = true; return false; } - Log.d("Program compiled successfully"); - //glDetachShader(program, vertexShader); - //glDetachShader(program, fragmentShader); + Log.d("Program linked successfully"); Log.v("trying glUseProgram with 0"); glUseProgram(0); Log.v("before useProgram"); From 2ae058e1f7fd9835b92161c8b1c1a25649d6baea Mon Sep 17 00:00:00 2001 From: gazer Date: Sat, 12 Dec 2015 00:59:52 +0300 Subject: [PATCH 05/14] check() shader; fixes --- src/dlangui/graphics/glsupport.d | 98 ++++++++++++++------------------ 1 file changed, 43 insertions(+), 55 deletions(-) diff --git a/src/dlangui/graphics/glsupport.d b/src/dlangui/graphics/glsupport.d index f194f491..3967fbf4 100644 --- a/src/dlangui/graphics/glsupport.d +++ b/src/dlangui/graphics/glsupport.d @@ -31,12 +31,12 @@ import std.array; derelict.util.exception.ShouldThrow gl3MissingSymFunc( string symName ) { import std.algorithm : equal; - foreach(s; ["glGetError", "glShaderSource", "glCompileShader", - "glGetShaderiv", "glGetShaderInfoLog", "glGetString", - "glCreateProgram", "glUseProgram", "glDeleteProgram", - "glDeleteShader", "glEnable", "glDisable", "glBlendFunc", - "glUniformMatrix4fv", "glGetAttribLocation", "glGetUniformLocation", - "glGenVertexArrays", "glBindVertexArray", "glBufferData", + foreach(s; ["glGetError", "glShaderSource", "glCompileShader", + "glGetShaderiv", "glGetShaderInfoLog", "glGetString", + "glCreateProgram", "glUseProgram", "glDeleteProgram", + "glDeleteShader", "glEnable", "glDisable", "glBlendFunc", + "glUniformMatrix4fv", "glGetAttribLocation", "glGetUniformLocation", + "glGenVertexArrays", "glBindVertexArray", "glBufferData", "glBindBuffer", "glBufferSubData"]) { if (symName.equal(s)) // Symbol is used return derelict.util.exception.ShouldThrow.Yes; @@ -74,7 +74,7 @@ static this() { 0x0507: "GL_CONTEXT_LOST" ]; } -/** +/** * Convenient wrapper around glGetError() * TODO use one of the DEBUG extensions instead */ @@ -103,7 +103,7 @@ class GLProgram { protected char[] glslversionString; this() { } - + private void compatibilityFixes(ref char[] code, GLuint type) { if (glslversionInt < 150) { code = replace(code, " texture(", " texture2D("); @@ -111,7 +111,7 @@ class GLProgram { code = replace(code, "out ", ""); } } - + private GLuint compileShader(string src, GLuint type) { import core.stdc.stdlib; import std.string; @@ -122,7 +122,7 @@ class GLProgram { sourceCode ~= "\n"; sourceCode ~= src; compatibilityFixes(sourceCode, type); - + Log.d("compileShader glsl=", glslversion, " type:", (type == GL_VERTEX_SHADER ? "GL_VERTEX_SHADER" : (type == GL_FRAGMENT_SHADER ? "GL_FRAGMENT_SHADER" : "UNKNOWN")), " code:\n", sourceCode); GLuint shader = glCreateShader(type); const char * psrc = sourceCode.toStringz; @@ -134,15 +134,15 @@ class GLProgram { // compiled successfully return shader; } else { - GLint blen = 0; + GLint blen = 0; GLsizei slen = 0; - glGetShaderiv(shader, GL_INFO_LOG_LENGTH , &blen); + glGetShaderiv(shader, GL_INFO_LOG_LENGTH , &blen); if (blen > 1) { GLchar[] msg = new GLchar[blen + 1]; glGetShaderInfoLog(shader, blen, &slen, msg.ptr); Log.d("Shader compilation error: ", fromStringz(msg.ptr)); - } + } return 0; } } @@ -260,12 +260,22 @@ class SolidFillProgram : GLProgram { }; } + bool check() + { + if (error) + return false; + if (!initialized) + if (!compile()) + return false; + return true; + } + void beforeExecute() { glEnable(GL_BLEND); glDisable(GL_CULL_FACE); checkError("glDisable(GL_CULL_FACE)"); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); checkError("glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)"); bind(); //glUniformMatrix4fv(matrixLocation, 1, false, m.value_ptr); @@ -281,18 +291,9 @@ class SolidFillProgram : GLProgram { protected GLint matrixLocation; protected GLint vertexLocation; protected GLint colAttrLocation; - protected GLuint vertexBuffer; - protected GLuint colAttrBuffer; override bool initLocations() { bool res = super.initLocations(); - //glGenBuffers(1, &vertexBuffer); - //glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); - //glBufferData(GL_ARRAY_BUFFER, float.sizeof * 3 * 6, null, GL_DYNAMIC_DRAW); - //glGenBuffers(1, &colAttrBuffer); - //glBindBuffer(GL_ARRAY_BUFFER, colAttrBuffer); - //glBufferData(GL_ARRAY_BUFFER, float.sizeof * 4 * 6, null, GL_DYNAMIC_DRAW); - matrixLocation = glGetUniformLocation(program, "matrix"); checkError("glGetUniformLocation matrix"); if (matrixLocation == -1) @@ -309,11 +310,8 @@ class SolidFillProgram : GLProgram { } bool execute(float[] vertices, float[] colors) { - if (error) + if(!check()) return false; - if (!initialized) - if (!compile()) - return false; beforeExecute(); GLuint vao; @@ -369,11 +367,8 @@ class SolidFillProgram : GLProgram { class LineProgram : SolidFillProgram { override bool execute(float[] vertices, float[] colors) { - if (error) + if(!check()) return false; - if (!initialized) - if (!compile()) - return false; beforeExecute(); GLuint vao; @@ -396,7 +391,7 @@ class LineProgram : SolidFillProgram { glBufferSubData( GL_ARRAY_BUFFER, vertices.length * vertices[0].sizeof, - colors.length * colors[0].sizeof, + colors.length * colors[0].sizeof, colors.ptr); glEnableVertexAttribArray(vertexLocation); @@ -467,11 +462,8 @@ class TextureProgram : SolidFillProgram { } bool execute(float[] vertices, float[] texcoords, float[] colors, uint textureId, bool linear) { - if (error) + if(!check()) return false; - if (!initialized) - if (!compile()) - return false; beforeExecute(); glActiveTexture(GL_TEXTURE0); checkError("glActiveTexture GL_TEXTURE0"); @@ -600,11 +592,8 @@ class FontProgram : SolidFillProgram { } bool execute(float[] vertices, float[] texcoords, float[] colors, uint textureId, bool linear) { - if (error) + if(!check()) return false; - if (!initialized) - if (!compile()) - return false; beforeExecute(); glActiveTexture(GL_TEXTURE0); checkError("glActiveTexture GL_TEXTURE0"); @@ -847,10 +836,10 @@ class GLSupport { checkError("glVertexPointer(3, GL_FLOAT, 0, vertices)"); glColorPointer(4, GL_FLOAT, 0, cast(void*)colors); checkError("glColorPointer(4, GL_FLOAT, 0, colors)"); - + glDrawArrays(GL_TRIANGLES, 0, 6); checkError("glDrawArrays(GL_TRIANGLES, 0, 6)"); - + glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); glDisable(GL_ALPHA_TEST); @@ -887,7 +876,7 @@ class GLSupport { float srcy0 = srcy / cast(float)tdy; float srcx1 = (srcx + srcdx) / cast(float)tdx; float srcy1 = (srcy + srcdy) / cast(float)tdy; - float[3 * 6] vertices = + float[3 * 6] vertices = [dstx0, dsty0, Z_2D, dstx0, dsty1, Z_2D, dstx1, dsty1, Z_2D, @@ -909,14 +898,14 @@ class GLSupport { checkError("drawColorAndTextureRect - glTexParameteri"); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, linear ? GL_LINEAR : GL_NEAREST); checkError("drawColorAndTextureRect - glTexParameteri"); - + glColor4f(1,1,1,1); glDisable(GL_ALPHA_TEST); - + glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); checkError("glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)"); - + glEnableClientState(GL_COLOR_ARRAY); checkError("glEnableClientState(GL_COLOR_ARRAY)"); glEnableClientState(GL_VERTEX_ARRAY); @@ -929,10 +918,10 @@ class GLSupport { checkError("glTexCoordPointer(2, GL_FLOAT, 0, texcoords)"); glColorPointer(4, GL_FLOAT, 0, cast(void*)colors.ptr); checkError("glColorPointer(4, GL_FLOAT, 0, colors)"); - + glDrawArrays(GL_TRIANGLES, 0, 6); checkError("glDrawArrays"); - + glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); @@ -988,14 +977,14 @@ class GLSupport { checkError("drawColorAndTextureRect - glTexParameteri"); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, linear ? GL_LINEAR : GL_NEAREST); checkError("drawColorAndTextureRect - glTexParameteri"); - + glColor4f(1,1,1,1); glDisable(GL_ALPHA_TEST); - + glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); checkError("glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)"); - + glEnableClientState(GL_COLOR_ARRAY); checkError("glEnableClientState(GL_COLOR_ARRAY)"); glEnableClientState(GL_VERTEX_ARRAY); @@ -1008,10 +997,10 @@ class GLSupport { checkError("glTexCoordPointer(2, GL_FLOAT, 0, texcoords)"); glColorPointer(4, GL_FLOAT, 0, cast(void*)colors.ptr); checkError("glColorPointer(4, GL_FLOAT, 0, colors)"); - + glDrawArrays(GL_TRIANGLES, 0, 6); checkError("glDrawArrays"); - + glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); @@ -1121,10 +1110,9 @@ class GLSupport { private uint currentFramebufferId; /// returns texture ID for buffer, 0 if failed - bool createFramebuffer(ref uint textureId, ref uint framebufferId, int dx, int dy) { + bool createFramebuffer(out uint textureId, out uint framebufferId, int dx, int dy) { checkError("before createFramebuffer"); bool res = true; - textureId = framebufferId = 0; textureId = genTexture(); if (!textureId) return false; From bfbdd462fd68cdac19a7b48380c13319e77a1b32 Mon Sep 17 00:00:00 2001 From: gazer Date: Sat, 12 Dec 2015 01:02:17 +0300 Subject: [PATCH 06/14] OpenGL object template --- src/dlangui/graphics/glsupport.d | 59 ++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/dlangui/graphics/glsupport.d b/src/dlangui/graphics/glsupport.d index 3967fbf4..8cb39fe5 100644 --- a/src/dlangui/graphics/glsupport.d +++ b/src/dlangui/graphics/glsupport.d @@ -1289,3 +1289,62 @@ class GLSupport { } } + +enum GLObjectTypes { Buffer, VertexArray, Texture, Framebuffer }; +class GLObject(GLObjectTypes type, GLuint target = 0) { + @property auto ID() const { return id; } + //alias ID this; // good, but it confuses destroy() + + private GLuint id; + + this() { + mixin("glGen" ~ to!string(type) ~ "s(1, &id);"); + checkError("glGen" ~ to!string(type)); + bind(); + } + + ~this() { + unbind(); + mixin("glDelete" ~ to!string(type) ~ "s(1, &id);"); + checkError("glDelete" ~ to!string(type)); + } + + void bind() { + static if(target != 0) + mixin("glBind" ~ to!string(type) ~ "(" ~ to!string(target) ~ ", id);"); + else + mixin("glBind" ~ to!string(type) ~ "(id);"); + } + + void unbind() { + static if(target != 0) + mixin("glBind" ~ to!string(type) ~ "(" ~ to!string(target) ~ ", 0);"); + else + mixin("glBind" ~ to!string(type) ~ "(0);"); + checkError("unbind " ~ to!string(type)); + } + + static if(type == GLObjectTypes.Texture) + { + void setSamplerParams(bool linear, bool clamp = false) { + glTexParameteri(target, GL_TEXTURE_MAG_FILTER, linear ? GL_LINEAR : GL_NEAREST); + glTexParameteri(target, GL_TEXTURE_MIN_FILTER, linear ? GL_LINEAR : GL_NEAREST); + checkError("filtering - glTexParameteri"); + if(clamp) { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + checkError("clamp - glTexParameteri"); + } + } + + void setup(GLuint binding = 0) { + glActiveTexture(GL_TEXTURE0 + binding); + glBindTexture(target, id); + checkError("setup texture"); + } + } +} +alias VAO = GLObject!(GLObjectTypes.VertexArray); +alias VBO = GLObject!(GLObjectTypes.Buffer, GL_ARRAY_BUFFER); +alias Tex2D = GLObject!(GLObjectTypes.Texture, GL_TEXTURE_2D); +alias FBO = GLObject!(GLObjectTypes.Framebuffer, GL_FRAMEBUFFER); From 3f2b14f4e33dc51107fd9fd5d82738bcd3d9cb9e Mon Sep 17 00:00:00 2001 From: gazer Date: Sat, 12 Dec 2015 01:04:37 +0300 Subject: [PATCH 07/14] VAO and VBO with GLObject --- src/dlangui/graphics/glsupport.d | 104 ++++++++++--------------------- 1 file changed, 32 insertions(+), 72 deletions(-) diff --git a/src/dlangui/graphics/glsupport.d b/src/dlangui/graphics/glsupport.d index 8cb39fe5..665989d3 100644 --- a/src/dlangui/graphics/glsupport.d +++ b/src/dlangui/graphics/glsupport.d @@ -314,13 +314,9 @@ class SolidFillProgram : GLProgram { return false; beforeExecute(); - GLuint vao; - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); + VAO vao = new VAO(); - GLuint vbo; - glGenBuffers(1, &vbo); - glBindBuffer(GL_ARRAY_BUFFER, vbo); + VBO vbo = new VBO(); glBufferData( GL_ARRAY_BUFFER, vertices.length * vertices[0].sizeof + colors.length * colors[0].sizeof, @@ -336,31 +332,22 @@ class SolidFillProgram : GLProgram { vertices.length * vertices[0].sizeof, colors.length * colors[0].sizeof, colors.ptr); - glEnableVertexAttribArray(vertexLocation); - checkError("glEnableVertexAttribArray"); glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, cast(void*) 0); - checkError("glVertexAttribPointer"); + glVertexAttribPointer(colAttrLocation, 4, GL_FLOAT, GL_FALSE, 0, cast(void*) (vertices.length * vertices[0].sizeof)); + glEnableVertexAttribArray(vertexLocation); glEnableVertexAttribArray(colAttrLocation); - checkError("glEnableVertexAttribArray"); - glVertexAttribPointer(colAttrLocation, 4, GL_FLOAT, GL_FALSE, 0, cast(void*) (float.sizeof*3*6)); - checkError("glVertexAttribPointer"); - glDrawArrays(GL_TRIANGLES, 0, 6); + glDrawArrays(GL_TRIANGLES, 0, cast(int)vertices.length/3); checkError("glDrawArrays"); glDisableVertexAttribArray(vertexLocation); - checkError("glDisableVertexAttribArray"); glDisableVertexAttribArray(colAttrLocation); - checkError("glDisableVertexAttribArray"); afterExecute(); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glDeleteBuffers(1, &vbo); - - glBindVertexArray(0); - glDeleteVertexArrays(1, &vao); + destroy(vbo); + destroy(vao); return true; } } @@ -371,13 +358,9 @@ class LineProgram : SolidFillProgram { return false; beforeExecute(); - GLuint vao; - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); + VAO vao = new VAO(); - GLuint vbo; - glGenBuffers(1, &vbo); - glBindBuffer(GL_ARRAY_BUFFER, vbo); + VBO vbo = new VBO(); glBufferData( GL_ARRAY_BUFFER, vertices.length * vertices[0].sizeof + colors.length * colors[0].sizeof, @@ -394,31 +377,22 @@ class LineProgram : SolidFillProgram { colors.length * colors[0].sizeof, colors.ptr); - glEnableVertexAttribArray(vertexLocation); - checkError("glEnableVertexAttribArray"); glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, cast(void*) 0); - checkError("glVertexAttribPointer"); + glVertexAttribPointer(colAttrLocation, 4, GL_FLOAT, GL_FALSE, 0, cast(void*) (vertices.length * vertices[0].sizeof)); + glEnableVertexAttribArray(vertexLocation); glEnableVertexAttribArray(colAttrLocation); - checkError("glEnableVertexAttribArray"); - glVertexAttribPointer(colAttrLocation, 4, GL_FLOAT, GL_FALSE, 0, cast(void*) (float.sizeof*3*2)); - checkError("glVertexAttribPointer"); - glDrawArrays(GL_LINES, 0, 2); + glDrawArrays(GL_LINES, 0, cast(int)vertices.length/3); checkError("glDrawArrays"); glDisableVertexAttribArray(vertexLocation); - checkError("glDisableVertexAttribArray"); glDisableVertexAttribArray(colAttrLocation); - checkError("glDisableVertexAttribArray"); afterExecute(); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glDeleteBuffers(1, &vbo); - - glBindVertexArray(0); - glDeleteVertexArrays(1, &vao); + destroy(vbo); + destroy(vao); return true; } } @@ -474,13 +448,9 @@ class TextureProgram : SolidFillProgram { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, linear ? GL_LINEAR : GL_NEAREST); checkError("drawColorAndTextureRect - glTexParameteri"); - GLuint vao; - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); + VAO vao = new VAO(); - GLuint vbo; - glGenBuffers(1, &vbo); - glBindBuffer(GL_ARRAY_BUFFER, vbo); + VBO vbo = new VBO(); glBufferData( GL_ARRAY_BUFFER, vertices.length * vertices[0].sizeof + @@ -504,15 +474,15 @@ class TextureProgram : SolidFillProgram { texcoords.length * texcoords[0].sizeof, texcoords.ptr); - glEnableVertexAttribArray(vertexLocation); - glEnableVertexAttribArray(colAttrLocation); - glEnableVertexAttribArray(texCoordLocation); - glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, cast(void*) 0); glVertexAttribPointer(colAttrLocation, 4, GL_FLOAT, GL_FALSE, 0, cast(void*) (vertices.length * vertices[0].sizeof)); glVertexAttribPointer(texCoordLocation, 2, GL_FLOAT, GL_FALSE, 0, cast(void*) (vertices.length * vertices[0].sizeof + colors.length * colors[0].sizeof)); - glDrawArrays(GL_TRIANGLES, 0, 6); + glEnableVertexAttribArray(vertexLocation); + glEnableVertexAttribArray(colAttrLocation); + glEnableVertexAttribArray(texCoordLocation); + + glDrawArrays(GL_TRIANGLES, 0, cast(int)vertices.length/3); checkError("glDrawArrays"); glDisableVertexAttribArray(vertexLocation); @@ -521,11 +491,8 @@ class TextureProgram : SolidFillProgram { afterExecute(); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glDeleteBuffers(1, &vbo); - - glBindVertexArray(0); - glDeleteVertexArrays(1, &vao); + destroy(vbo); + destroy(vao); glBindTexture(GL_TEXTURE_2D, 0); checkError("glBindTexture"); @@ -604,13 +571,9 @@ class FontProgram : SolidFillProgram { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, linear ? GL_LINEAR : GL_NEAREST); checkError("drawColorAndTextureRect - glTexParameteri"); - GLuint vao; - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); + VAO vao = new VAO(); - GLuint vbo; - glGenBuffers(1, &vbo); - glBindBuffer(GL_ARRAY_BUFFER, vbo); + VBO vbo = new VBO(); glBufferData( GL_ARRAY_BUFFER, vertices.length * vertices[0].sizeof + @@ -634,15 +597,15 @@ class FontProgram : SolidFillProgram { texcoords.length * texcoords[0].sizeof, texcoords.ptr); - glEnableVertexAttribArray(vertexLocation); - glEnableVertexAttribArray(colAttrLocation); - glEnableVertexAttribArray(texCoordLocation); - glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, cast(void*) 0); glVertexAttribPointer(colAttrLocation, 4, GL_FLOAT, GL_FALSE, 0, cast(void*) (vertices.length * vertices[0].sizeof)); glVertexAttribPointer(texCoordLocation, 2, GL_FLOAT, GL_FALSE, 0, cast(void*) (vertices.length * vertices[0].sizeof + colors.length * colors[0].sizeof)); - glDrawArrays(GL_TRIANGLES, 0, 6); + glEnableVertexAttribArray(vertexLocation); + glEnableVertexAttribArray(colAttrLocation); + glEnableVertexAttribArray(texCoordLocation); + + glDrawArrays(GL_TRIANGLES, 0, cast(int)vertices.length/3); checkError("glDrawArrays"); glDisableVertexAttribArray(vertexLocation); @@ -651,11 +614,8 @@ class FontProgram : SolidFillProgram { afterExecute(); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glDeleteBuffers(1, &vbo); - - glBindVertexArray(0); - glDeleteVertexArrays(1, &vao); + destroy(vbo); + destroy(vao); glBindTexture(GL_TEXTURE_2D, 0); checkError("glBindTexture"); From 989649201816cb9ced0ac9f35282b1da6fd7192b Mon Sep 17 00:00:00 2001 From: gazer Date: Sat, 12 Dec 2015 01:24:39 +0300 Subject: [PATCH 08/14] FBO with GLObject --- src/dlangui/graphics/glsupport.d | 71 ++++++++++++-------------------- 1 file changed, 26 insertions(+), 45 deletions(-) diff --git a/src/dlangui/graphics/glsupport.d b/src/dlangui/graphics/glsupport.d index 665989d3..7f3594ac 100644 --- a/src/dlangui/graphics/glsupport.d +++ b/src/dlangui/graphics/glsupport.d @@ -737,7 +737,7 @@ class GLSupport { float y1 = cast(float)(bufferDy-p2.y); // don't flip for framebuffer - if (currentFramebufferId) { + if (currentFBO) { y0 = cast(float)(p1.y); y1 = cast(float)(p2.y); } @@ -768,7 +768,7 @@ class GLSupport { float y1 = cast(float)(bufferDy-rc.bottom); // don't flip for framebuffer - if (currentFramebufferId) { + if (currentFBO) { y0 = cast(float)(rc.top); y1 = cast(float)(rc.bottom); } @@ -827,7 +827,7 @@ class GLSupport { float dsty1 = cast(float)(bufferDy - (yy + dy)); // don't flip for framebuffer - if (currentFramebufferId) { + if (currentFBO) { dsty0 = cast(float)((yy)); dsty1 = cast(float)((yy + dy)); } @@ -908,7 +908,7 @@ class GLSupport { float dsty1 = cast(float)(bufferDy - (yy + dy)); // don't flip for framebuffer - if (currentFramebufferId) { + if (currentFBO) { dsty0 = cast(float)((yy)); dsty1 = cast(float)((yy + dy)); } @@ -1067,37 +1067,27 @@ class GLSupport { return true; } - private uint currentFramebufferId; + private FBO currentFBO; - /// returns texture ID for buffer, 0 if failed - bool createFramebuffer(out uint textureId, out uint framebufferId, int dx, int dy) { + /// returns texture for buffer, null if failed + bool createFramebuffer(out Tex2D texture, out FBO fbo, int dx, int dy) { checkError("before createFramebuffer"); bool res = true; - textureId = genTexture(); - if (!textureId) + texture = new Tex2D(); + if (!texture.ID) return false; - GLuint fid = 0; - glGenFramebuffers(1, &fid); - if (checkError("createFramebuffer glGenFramebuffersOES")) return false; - framebufferId = fid; - glBindFramebuffer(GL_FRAMEBUFFER, framebufferId); - if (checkError("createFramebuffer glBindFramebuffer")) return false; + checkError("glBindTexture GL_TEXTURE_2D"); + FBO f = new FBO(); + if (!f.ID) + return false; + fbo = f; - glBindTexture(GL_TEXTURE_2D, textureId); - checkError("glBindTexture(GL_TEXTURE_2D, _textureId)"); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, dx, dy, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, null); checkError("glTexImage2D"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - checkError("texParameter"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - checkError("texParameter"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - checkError("texParameter"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - checkError("texParameter"); + texture.setSamplerParams(true, true); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureId, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.ID, 0); checkError("glFramebufferTexture2D"); // Always check that our framebuffer is ok if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { @@ -1112,35 +1102,26 @@ class GLSupport { checkError("glClear"); checkError("after createFramebuffer"); //CRLog::trace("CRGLSupportImpl::createFramebuffer %d,%d texture=%d, buffer=%d", dx, dy, textureId, framebufferId); - currentFramebufferId = framebufferId; + currentFBO = fbo; - glBindTexture(GL_TEXTURE_2D, 0); - checkError("createFramebuffer - glBindTexture(0)"); - glBindFramebuffer(GL_FRAMEBUFFER, 0); - checkError("createFramebuffer - glBindFramebuffer(0)"); + texture.unbind(); + fbo.unbind(); return res; } - void deleteFramebuffer(ref uint framebufferId) { + void deleteFramebuffer(ref FBO fbo) { //CRLog::debug("GLDrawBuf::deleteFramebuffer"); - if (framebufferId != 0) { - glBindFramebuffer(GL_FRAMEBUFFER, 0); - checkError("deleteFramebuffer - glBindFramebuffer"); - GLuint fid = framebufferId; - glDeleteFramebuffers(1, &fid); - checkError("deleteFramebuffer - glDeleteFramebuffer"); + if (fbo.ID != 0) { + destroy(fbo); } - //CRLog::trace("CRGLSupportImpl::deleteFramebuffer(%d)", framebufferId); - framebufferId = 0; - checkError("after deleteFramebuffer"); - currentFramebufferId = 0; + currentFBO = null; } - bool bindFramebuffer(uint framebufferId) { + bool bindFramebuffer(FBO fbo) { //CRLog::trace("CRGLSupportImpl::bindFramebuffer(%d)", framebufferId); - glBindFramebuffer(GL_FRAMEBUFFER, framebufferId); - currentFramebufferId = framebufferId; + fbo.bind(); + currentFBO = fbo; return !checkError("glBindFramebuffer"); } From 8336f9c9cbb1994468a5e19e0eb541b7f92c868e Mon Sep 17 00:00:00 2001 From: gazer Date: Sat, 12 Dec 2015 01:34:24 +0300 Subject: [PATCH 09/14] textures with GLObject --- src/dlangui/graphics/gldrawbuf.d | 80 ++++++++--------- src/dlangui/graphics/glsupport.d | 150 ++++++++----------------------- 2 files changed, 71 insertions(+), 159 deletions(-) diff --git a/src/dlangui/graphics/gldrawbuf.d b/src/dlangui/graphics/gldrawbuf.d index a10b59e5..9f69a788 100644 --- a/src/dlangui/graphics/gldrawbuf.d +++ b/src/dlangui/graphics/gldrawbuf.d @@ -105,8 +105,8 @@ class GLDrawBuf : DrawBuf, GLConfigCallback { if (!isFullyTransparentColor(color) && applyClipping(rc)) _scene.add(new SolidRectSceneItem(rc, color)); } - /// draw pixel at (x, y) with specified color - override void drawPixel(int x, int y, uint color) { + /// draw pixel at (x, y) with specified color + override void drawPixel(int x, int y, uint color) { assert(_scene !is null); if (!_clipRect.isPointInside(x, y)) return; @@ -114,7 +114,7 @@ class GLDrawBuf : DrawBuf, GLConfigCallback { if (isFullyTransparentColor(color)) return; _scene.add(new SolidRectSceneItem(Rect(x, y, x + 1, y + 1), color)); - } + } /// draw 8bit alpha image - usually font glyph using specified color (clipping is applied) override void drawGlyph(int x, int y, Glyph * glyph, uint color) { assert(_scene !is null); @@ -280,7 +280,7 @@ private class GLImageCache { private GLImageCachePage _page; @property GLImageCachePage page() { return _page; } - + uint _objectId; Rect _rc; bool _deleted; @@ -298,7 +298,7 @@ private class GLImageCache { private int _x; private bool _closed; private bool _needUpdateTexture; - private uint _textureId; + private Tex2D _texture; private int _itemCount; this(GLImageCache cache, int dx, int dy) { @@ -314,26 +314,26 @@ private class GLImageCache { destroy(_drawbuf); _drawbuf = null; } - if (_textureId != 0) { - glSupport.deleteTexture(_textureId); - _textureId = 0; + if (_texture.ID != 0) { + destroy(_texture); + _texture = null; } } void updateTexture() { if (_drawbuf is null) return; // no draw buffer!!! - if (_textureId == 0) { - _textureId = glSupport.genTexture(); - Log.d("updateTexture - new texture id=", _textureId); - if (!_textureId) + if (_texture is null || _texture.ID == 0) { + _texture = new Tex2D(); + Log.d("updateTexture - new texture id=", _texture.ID); + if (!_texture.ID) return; } - Log.d("updateTexture for image cache page - setting image ", _drawbuf.width, "x", _drawbuf.height, " tx=", _textureId); + Log.d("updateTexture for image cache page - setting image ", _drawbuf.width, "x", _drawbuf.height, " tx=", _texture.ID); uint * pixels = _drawbuf.scanLine(0); - if (!glSupport.setTextureImage(_textureId, _drawbuf.width, _drawbuf.height, cast(ubyte*)pixels)) { - glSupport.deleteTexture(_textureId); - _textureId = 0; + if (!glSupport.setTextureImage(_texture, _drawbuf.width, _drawbuf.height, cast(ubyte*)pixels)) { + destroy(_texture); + _texture = null; return; } _needUpdateTexture = false; @@ -409,11 +409,7 @@ private class GLImageCache { //CRLog::trace("drawing item at %d,%d %dx%d <= %d,%d %dx%d ", x, y, dx, dy, srcx, srcy, srcdx, srcdy); if (_needUpdateTexture) updateTexture(); - if (_textureId != 0) { - if (!glSupport.isTexture(_textureId)) { - Log.e("Invalid texture ", _textureId); - return; - } + if (_texture.ID != 0) { //rotationAngle = 0; int rx = dstrc.middlex; int ry = dstrc.middley; @@ -442,8 +438,8 @@ private class GLImageCache { dstrc.bottom -= clip.bottom; } if (!dstrc.empty) - glSupport.drawColorAndTextureRect(_textureId, _tdx, _tdy, srcrc, dstrc, color, srcrc.width() != dstrc.width() || srcrc.height() != dstrc.height()); - //drawColorAndTextureRect(vertices, texcoords, color, _textureId); + glSupport.drawColorAndTextureRect(_texture, _tdx, _tdy, srcrc, dstrc, color, srcrc.width() != dstrc.width() || srcrc.height() != dstrc.height()); + //drawColorAndTextureRect(vertices, texcoords, color, _texture); if (rotationAngle) { // unset rotation @@ -482,7 +478,7 @@ private class GLImageCache { private void updateTextureSize() { if (!tdx) { // TODO - tdx = tdy = 1024; //getMaxTextureSize(); + tdx = tdy = 1024; //getMaxTextureSize(); if (tdx > 1024) tdx = tdy = 1024; } @@ -627,7 +623,7 @@ private class GLGlyphCache { private int _x; private bool _closed; private bool _needUpdateTexture; - private uint _textureId; + private Tex2D _texture; private int _itemCount; this(GLGlyphCache cache, int dx, int dy) { @@ -643,26 +639,26 @@ private class GLGlyphCache { destroy(_drawbuf); _drawbuf = null; } - if (_textureId != 0) { - glSupport.deleteTexture(_textureId); - _textureId = 0; + if (_texture.ID != 0) { + destroy(_texture); + _texture = null; } } void updateTexture() { if (_drawbuf is null) return; // no draw buffer!!! - if (_textureId == 0) { - _textureId = glSupport.genTexture(); - //Log.d("updateTexture - new texture ", _textureId); - if (!_textureId) + if (_texture is null || _texture.ID == 0) { + _texture = new Tex2D(); + //Log.d("updateTexture - new texture ", _texture.ID); + if (!_texture.ID) return; } - //Log.d("updateTexture for font glyph page - setting image ", _drawbuf.width, "x", _drawbuf.height, " tx=", _textureId); + //Log.d("updateTexture for font glyph page - setting image ", _drawbuf.width, "x", _drawbuf.height, " tx=", _texture.ID); int len = _drawbuf.width * _drawbuf.height; - if (!glSupport.setTextureImage(_textureId, _drawbuf.width, _drawbuf.height, cast(ubyte *)_drawbuf.scanLine(0))) { - glSupport.deleteTexture(_textureId); - _textureId = 0; + if (!glSupport.setTextureImage(_texture, _drawbuf.width, _drawbuf.height, cast(ubyte *)_drawbuf.scanLine(0))) { + destroy(_texture); + _texture = null; return; } _needUpdateTexture = false; @@ -724,11 +720,7 @@ private class GLGlyphCache { //CRLog::trace("drawing item at %d,%d %dx%d <= %d,%d %dx%d ", x, y, dx, dy, srcx, srcy, srcdx, srcdy); if (_needUpdateTexture) updateTexture(); - if (_textureId != 0) { - if (!glSupport.isTexture(_textureId)) { - Log.e("Invalid texture ", _textureId); - return; - } + if (_texture.ID != 0) { // convert coordinates to cached texture srcrc.offset(item._rc.left, item._rc.top); if (clip) { @@ -751,8 +743,8 @@ private class GLGlyphCache { } if (!dstrc.empty) { //Log.d("drawing glyph with color ", color); - glSupport.drawColorAndTextureGlyphRect(_textureId, _tdx, _tdy, srcrc, dstrc, color); - //glSupport.drawColorAndTextureRect(_textureId, _tdx, _tdy, srcrc, dstrc, color, false); + glSupport.drawColorAndTextureGlyphRect(_texture, _tdx, _tdy, srcrc, dstrc, color); + //glSupport.drawColorAndTextureRect(_texture, _tdx, _tdy, srcrc, dstrc, color, false); } } @@ -782,7 +774,7 @@ private class GLGlyphCache { private void updateTextureSize() { if (!tdx) { // TODO - tdx = tdy = 1024; //getMaxTextureSize(); + tdx = tdy = 1024; //getMaxTextureSize(); if (tdx > 1024) tdx = tdy = 1024; } diff --git a/src/dlangui/graphics/glsupport.d b/src/dlangui/graphics/glsupport.d index 7f3594ac..f1e4e70b 100644 --- a/src/dlangui/graphics/glsupport.d +++ b/src/dlangui/graphics/glsupport.d @@ -435,18 +435,13 @@ class TextureProgram : SolidFillProgram { return res && texCoordLocation >= 0; } - bool execute(float[] vertices, float[] texcoords, float[] colors, uint textureId, bool linear) { + bool execute(float[] vertices, float[] texcoords, float[] colors, Tex2D texture, bool linear) { if(!check()) return false; beforeExecute(); - glActiveTexture(GL_TEXTURE0); - checkError("glActiveTexture GL_TEXTURE0"); - glBindTexture(GL_TEXTURE_2D, textureId); - checkError("glBindTexture"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, linear ? GL_LINEAR : GL_NEAREST); - checkError("drawColorAndTextureRect - glTexParameteri"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, linear ? GL_LINEAR : GL_NEAREST); - checkError("drawColorAndTextureRect - glTexParameteri"); + + texture.setup(); + texture.setSamplerParams(linear); VAO vao = new VAO(); @@ -494,8 +489,7 @@ class TextureProgram : SolidFillProgram { destroy(vbo); destroy(vao); - glBindTexture(GL_TEXTURE_2D, 0); - checkError("glBindTexture"); + texture.unbind(); return true; } } @@ -558,18 +552,13 @@ class FontProgram : SolidFillProgram { super.afterExecute(); } - bool execute(float[] vertices, float[] texcoords, float[] colors, uint textureId, bool linear) { + bool execute(float[] vertices, float[] texcoords, float[] colors, Tex2D texture, bool linear) { if(!check()) return false; beforeExecute(); - glActiveTexture(GL_TEXTURE0); - checkError("glActiveTexture GL_TEXTURE0"); - glBindTexture(GL_TEXTURE_2D, textureId); - checkError("glBindTexture"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, linear ? GL_LINEAR : GL_NEAREST); - checkError("drawColorAndTextureRect - glTexParameteri"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, linear ? GL_LINEAR : GL_NEAREST); - checkError("drawColorAndTextureRect - glTexParameteri"); + + texture.setup(); + texture.setSamplerParams(linear); VAO vao = new VAO(); @@ -617,8 +606,7 @@ class FontProgram : SolidFillProgram { destroy(vbo); destroy(vao); - glBindTexture(GL_TEXTURE_2D, 0); - checkError("glBindTexture"); + texture.unbind(); return true; } } @@ -813,12 +801,12 @@ class GLSupport { } } - void drawColorAndTextureGlyphRect(uint textureId, int tdx, int tdy, Rect srcrc, Rect dstrc, uint color) { - //Log.v("drawColorAndGlyphRect tx=", textureId, " src=", srcrc, " dst=", dstrc); - drawColorAndTextureGlyphRect(textureId, tdx, tdy, srcrc.left, srcrc.top, srcrc.width(), srcrc.height(), dstrc.left, dstrc.top, dstrc.width(), dstrc.height(), color); + void drawColorAndTextureGlyphRect(Tex2D texture, int tdx, int tdy, Rect srcrc, Rect dstrc, uint color) { + //Log.v("drawColorAndGlyphRect tx=", texture.ID, " src=", srcrc, " dst=", dstrc); + drawColorAndTextureGlyphRect(texture, tdx, tdy, srcrc.left, srcrc.top, srcrc.width(), srcrc.height(), dstrc.left, dstrc.top, dstrc.width(), dstrc.height(), color); } - void drawColorAndTextureGlyphRect(uint textureId, int tdx, int tdy, int srcx, int srcy, int srcdx, int srcdy, int xx, int yy, int dx, int dy, uint color) { + void drawColorAndTextureGlyphRect(Tex2D texture, int tdx, int tdy, int srcx, int srcy, int srcdx, int srcdy, int xx, int yy, int dx, int dy, uint color) { float[6*4] colors; LVGLFillColor(color, colors.ptr, 6); float dstx0 = cast(float)xx; @@ -848,16 +836,9 @@ class GLSupport { if (_legacyMode) { bool linear = dx != srcdx || dy != srcdy; glDisable(GL_CULL_FACE); - glActiveTexture(GL_TEXTURE0); - checkError("glActiveTexture"); glEnable(GL_TEXTURE_2D); - checkError("glEnable(GL_TEXTURE_2D)"); - glBindTexture(GL_TEXTURE_2D, textureId); - checkError("glBindTexture"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, linear ? GL_LINEAR : GL_NEAREST); - checkError("drawColorAndTextureRect - glTexParameteri"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, linear ? GL_LINEAR : GL_NEAREST); - checkError("drawColorAndTextureRect - glTexParameteri"); + texture.setup(); + texture.setSamplerParams(linear); glColor4f(1,1,1,1); glDisable(GL_ALPHA_TEST); @@ -889,17 +870,17 @@ class GLSupport { glDisable(GL_ALPHA_TEST); glDisable(GL_TEXTURE_2D); } else { - _fontProgram.execute(vertices, texcoords, colors, textureId, false); + _fontProgram.execute(vertices, texcoords, colors, texture, false); } - //drawColorAndTextureRect(vertices, texcoords, colors, textureId, linear); + //drawColorAndTextureRect(vertices, texcoords, colors, texture, linear); } - void drawColorAndTextureRect(uint textureId, int tdx, int tdy, Rect srcrc, Rect dstrc, uint color, bool linear) { - //Log.v("drawColorAndTextureRect tx=", textureId, " src=", srcrc, " dst=", dstrc); - drawColorAndTextureRect(textureId, tdx, tdy, srcrc.left, srcrc.top, srcrc.width(), srcrc.height(), dstrc.left, dstrc.top, dstrc.width(), dstrc.height(), color, linear); + void drawColorAndTextureRect(Tex2D texture, int tdx, int tdy, Rect srcrc, Rect dstrc, uint color, bool linear) { + //Log.v("drawColorAndTextureRect tx=", texture.ID, " src=", srcrc, " dst=", dstrc); + drawColorAndTextureRect(texture, tdx, tdy, srcrc.left, srcrc.top, srcrc.width(), srcrc.height(), dstrc.left, dstrc.top, dstrc.width(), dstrc.height(), color, linear); } - void drawColorAndTextureRect(uint textureId, int tdx, int tdy, int srcx, int srcy, int srcdx, int srcdy, int xx, int yy, int dx, int dy, uint color, bool linear) { + void drawColorAndTextureRect(Tex2D texture, int tdx, int tdy, int srcx, int srcy, int srcdx, int srcdy, int xx, int yy, int dx, int dy, uint color, bool linear) { float[6*4] colors; LVGLFillColor(color, colors.ptr, 6); float dstx0 = cast(float)xx; @@ -927,16 +908,9 @@ class GLSupport { if (_legacyMode) { glDisable(GL_CULL_FACE); - glActiveTexture(GL_TEXTURE0); - checkError("glActiveTexture"); glEnable(GL_TEXTURE_2D); - checkError("glEnable(GL_TEXTURE_2D)"); - glBindTexture(GL_TEXTURE_2D, textureId); - checkError("glBindTexture"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, linear ? GL_LINEAR : GL_NEAREST); - checkError("drawColorAndTextureRect - glTexParameteri"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, linear ? GL_LINEAR : GL_NEAREST); - checkError("drawColorAndTextureRect - glTexParameteri"); + texture.setup(); + texture.setSamplerParams(linear); glColor4f(1,1,1,1); glDisable(GL_ALPHA_TEST); @@ -968,30 +942,9 @@ class GLSupport { glDisable(GL_ALPHA_TEST); glDisable(GL_TEXTURE_2D); } else { - _textureProgram.execute(vertices, texcoords, colors, textureId, linear); + _textureProgram.execute(vertices, texcoords, colors, texture, linear); } - //drawColorAndTextureRect(vertices, texcoords, colors, textureId, linear); - } - - /// generate new texture ID - uint genTexture() { - GLuint textureId = 0; - glGenTextures(1, &textureId); - return textureId; - } - - /// delete OpenGL texture - void deleteTexture(ref uint textureId) { - if (!textureId) - return; - if (glIsTexture(textureId) != GL_TRUE) { - Log.e("Invalid texture ", textureId); - return; - } - GLuint id = textureId; - glDeleteTextures(1, &id); - checkError("glDeleteTextures"); - textureId = 0; + //drawColorAndTextureRect(vertices, texcoords, colors, texture, linear); } /// call glFlush @@ -1000,32 +953,16 @@ class GLSupport { checkError("glFlush"); } - bool setTextureImage(uint textureId, int dx, int dy, ubyte * pixels) { + bool setTextureImage(Tex2D texture, int dx, int dy, ubyte * pixels) { //checkError("before setTextureImage"); - glActiveTexture(GL_TEXTURE0); - checkError("updateTexture - glActiveTexture"); - glBindTexture(GL_TEXTURE_2D, 0); - checkError("updateTexture - glBindTexture(0)"); - glBindTexture(GL_TEXTURE_2D, textureId); - checkError("updateTexture - glBindTexture"); + texture.setup(); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); checkError("updateTexture - glPixelStorei"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - checkError("updateTexture - glTexParameteri"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - checkError("updateTexture - glTexParameteri"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - checkError("updateTexture - glTexParameteri"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - checkError("updateTexture - glTexParameteri"); - - if (!glIsTexture(textureId)) - Log.e("second test - invalid texture passed to CRGLSupportImpl::setTextureImage"); + texture.setSamplerParams(true, true); // ORIGINAL: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dx, dy, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dx, dy, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); - checkError("updateTexture - glTexImage2D"); - if (glGetError() != GL_NO_ERROR) { + if (checkError("updateTexture - glTexImage2D")) { Log.e("Cannot set image for texture"); return false; } @@ -1033,36 +970,19 @@ class GLSupport { return true; } - bool setTextureImageAlpha(uint textureId, int dx, int dy, ubyte * pixels) { + bool setTextureImageAlpha(Tex2D texture, int dx, int dy, ubyte * pixels) { checkError("before setTextureImageAlpha"); - glActiveTexture(GL_TEXTURE0); - checkError("updateTexture - glActiveTexture"); - glBindTexture(GL_TEXTURE_2D, 0); - checkError("updateTexture - glBindTexture(0)"); - glBindTexture(GL_TEXTURE_2D, textureId); - checkError("setTextureImageAlpha - glBindTexture"); + texture.setup(); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); checkError("setTextureImageAlpha - glPixelStorei"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - checkError("setTextureImageAlpha - glTexParameteri"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - checkError("setTextureImageAlpha - glTexParameteri"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - checkError("setTextureImageAlpha - glTexParameteri"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - checkError("setTextureImageAlpha - glTexParameteri"); - - if (!glIsTexture(textureId)) - Log.e("second test: invalid texture passed to CRGLSupportImpl::setTextureImageAlpha"); + texture.setSamplerParams(true, true); glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, dx, dy, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels); - checkError("setTextureImageAlpha - glTexImage2D"); - if (glGetError() != GL_NO_ERROR) { + if (checkError("setTextureImageAlpha - glTexImage2D")) { Log.e("Cannot set image for texture"); return false; } - glBindTexture(GL_TEXTURE_2D, 0); - checkError("updateTexture - glBindTexture(0)"); + texture.unbind(); checkError("after setTextureImageAlpha"); return true; } From a7aee918139fa62b5d43a852919b2ad63df86689 Mon Sep 17 00:00:00 2001 From: gazer Date: Sat, 12 Dec 2015 14:11:24 +0300 Subject: [PATCH 10/14] vbo fill method --- src/dlangui/graphics/glsupport.d | 102 ++++++++----------------------- 1 file changed, 25 insertions(+), 77 deletions(-) diff --git a/src/dlangui/graphics/glsupport.d b/src/dlangui/graphics/glsupport.d index f1e4e70b..a0be22fd 100644 --- a/src/dlangui/graphics/glsupport.d +++ b/src/dlangui/graphics/glsupport.d @@ -317,20 +317,7 @@ class SolidFillProgram : GLProgram { VAO vao = new VAO(); VBO vbo = new VBO(); - glBufferData( - GL_ARRAY_BUFFER, - vertices.length * vertices[0].sizeof + colors.length * colors[0].sizeof, - null, - GL_STREAM_DRAW); - glBufferSubData( - GL_ARRAY_BUFFER, - 0, - vertices.length * vertices[0].sizeof, - vertices.ptr); - glBufferSubData( - GL_ARRAY_BUFFER, - vertices.length * vertices[0].sizeof, - colors.length * colors[0].sizeof, colors.ptr); + vbo.fill([vertices, colors]); glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, cast(void*) 0); glVertexAttribPointer(colAttrLocation, 4, GL_FLOAT, GL_FALSE, 0, cast(void*) (vertices.length * vertices[0].sizeof)); @@ -361,21 +348,7 @@ class LineProgram : SolidFillProgram { VAO vao = new VAO(); VBO vbo = new VBO(); - glBufferData( - GL_ARRAY_BUFFER, - vertices.length * vertices[0].sizeof + colors.length * colors[0].sizeof, - null, - GL_STREAM_DRAW); - glBufferSubData( - GL_ARRAY_BUFFER, - 0, - vertices.length * vertices[0].sizeof, - vertices.ptr); - glBufferSubData( - GL_ARRAY_BUFFER, - vertices.length * vertices[0].sizeof, - colors.length * colors[0].sizeof, - colors.ptr); + vbo.fill([vertices, colors]); glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, cast(void*) 0); glVertexAttribPointer(colAttrLocation, 4, GL_FLOAT, GL_FALSE, 0, cast(void*) (vertices.length * vertices[0].sizeof)); @@ -446,28 +419,7 @@ class TextureProgram : SolidFillProgram { VAO vao = new VAO(); VBO vbo = new VBO(); - glBufferData( - GL_ARRAY_BUFFER, - vertices.length * vertices[0].sizeof + - colors.length * colors[0].sizeof + - texcoords.length * texcoords[0].sizeof, - null, - GL_STREAM_DRAW); - glBufferSubData( - GL_ARRAY_BUFFER, - 0, - vertices.length * vertices[0].sizeof, - vertices.ptr); - glBufferSubData( - GL_ARRAY_BUFFER, - vertices.length * vertices[0].sizeof, - colors.length * colors[0].sizeof, - colors.ptr); - glBufferSubData( - GL_ARRAY_BUFFER, - vertices.length * vertices[0].sizeof + colors.length * colors[0].sizeof, - texcoords.length * texcoords[0].sizeof, - texcoords.ptr); + vbo.fill([vertices, colors, texcoords]); glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, cast(void*) 0); glVertexAttribPointer(colAttrLocation, 4, GL_FLOAT, GL_FALSE, 0, cast(void*) (vertices.length * vertices[0].sizeof)); @@ -563,28 +515,7 @@ class FontProgram : SolidFillProgram { VAO vao = new VAO(); VBO vbo = new VBO(); - glBufferData( - GL_ARRAY_BUFFER, - vertices.length * vertices[0].sizeof + - colors.length * colors[0].sizeof + - texcoords.length * texcoords[0].sizeof, - null, - GL_STREAM_DRAW); - glBufferSubData( - GL_ARRAY_BUFFER, - 0, - vertices.length * vertices[0].sizeof, - vertices.ptr); - glBufferSubData( - GL_ARRAY_BUFFER, - vertices.length * vertices[0].sizeof, - colors.length * colors[0].sizeof, - colors.ptr); - glBufferSubData( - GL_ARRAY_BUFFER, - vertices.length * vertices[0].sizeof + colors.length * colors[0].sizeof, - texcoords.length * texcoords[0].sizeof, - texcoords.ptr); + vbo.fill([vertices, colors, texcoords]); glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, cast(void*) 0); glVertexAttribPointer(colAttrLocation, 4, GL_FLOAT, GL_FALSE, 0, cast(void*) (vertices.length * vertices[0].sizeof)); @@ -691,10 +622,6 @@ class GLSupport { return true; } - bool isTexture(uint textureId) { - return glIsTexture(textureId) == GL_TRUE; - } - void setRotation(int x, int y, int rotationAngle) { /* this->rotationAngle = rotationAngle; @@ -1184,6 +1111,27 @@ class GLObject(GLObjectTypes type, GLuint target = 0) { mixin("glBind" ~ to!string(type) ~ "(0);"); checkError("unbind " ~ to!string(type)); } + + static if(type == GLObjectTypes.Buffer) + { + void fill(float[][] buffs) { + int length; + foreach(b; buffs) + length += b.length; + glBufferData(target, + length * float.sizeof, + null, + GL_STREAM_DRAW); + int offset; + foreach(b; buffs) { + glBufferSubData(target, + offset, + b.length * float.sizeof, + b.ptr); + offset += b.length * float.sizeof; + } + } + } static if(type == GLObjectTypes.Texture) { From 184fa4db50e55effd33aad477e8afcbd07d88b6f Mon Sep 17 00:00:00 2001 From: gazer Date: Sat, 12 Dec 2015 20:22:04 +0300 Subject: [PATCH 11/14] new errors checking function --- src/dlangui/graphics/glsupport.d | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/dlangui/graphics/glsupport.d b/src/dlangui/graphics/glsupport.d index a0be22fd..df09018f 100644 --- a/src/dlangui/graphics/glsupport.d +++ b/src/dlangui/graphics/glsupport.d @@ -76,14 +76,20 @@ static this() { } /** * Convenient wrapper around glGetError() - * TODO use one of the DEBUG extensions instead + * TODO use one of the DEBUG extensions */ -bool checkError(string context="", string file=__FILE__, int line=__LINE__) +/// Using: checkgl!glFunction(funcParams); +auto checkgl(alias func, string functionName=__FUNCTION__, int line=__LINE__, Args...)(Args args) +{ + scope(success) checkError(func.stringof, functionName, line); + return func(args); +} +bool checkError(string context="", string functionName=__FUNCTION__, int line=__LINE__) { GLenum err = glGetError(); if (err != GL_NO_ERROR) { - Log.e("OpenGL error ", errors.get(err, to!string(err)), " at ", file, ":", line, " -- ", context); + Log.e("OpenGL error ", errors.get(err, to!string(err)), " at ", functionName, ":", line, " -- ", context); return true; } return false; @@ -280,8 +286,8 @@ class SolidFillProgram : GLProgram { bind(); //glUniformMatrix4fv(matrixLocation, 1, false, m.value_ptr); //glUniformMatrix4fv(matrixLocation, 1, false, matrix.ptr); - glUniformMatrix4fv(matrixLocation, 1, false, glSupport.qtmatrix.ptr); - checkError("glUniformMatrix4fv"); + checkgl!glUniformMatrix4fv(matrixLocation, 1, false, glSupport.qtmatrix.ptr); + //checkError("glUniformMatrix4fv"); } void afterExecute() { @@ -495,8 +501,8 @@ class FontProgram : SolidFillProgram { //glBlendFunc(GL_ONE_MINUS_SRC_COLOR, GL_SRC_COLOR); checkError("glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR)"); bind(); - glUniformMatrix4fv(matrixLocation, 1, false, glSupport.qtmatrix.ptr); - checkError("glUniformMatrix4fv"); + checkgl!glUniformMatrix4fv(matrixLocation, 1, false, glSupport.qtmatrix.ptr); + //checkError("glUniformMatrix4fv"); } override void afterExecute() { From ba5a27ce9e7fa3710c20ee439e1cc073a91bcc33 Mon Sep 17 00:00:00 2001 From: gazer Date: Sat, 12 Dec 2015 20:42:32 +0300 Subject: [PATCH 12/14] checkError -> checkgl --- src/dlangui/graphics/gldrawbuf.d | 3 +- src/dlangui/graphics/glsupport.d | 170 +++++++++++-------------------- 2 files changed, 59 insertions(+), 114 deletions(-) diff --git a/src/dlangui/graphics/gldrawbuf.d b/src/dlangui/graphics/gldrawbuf.d index 9f69a788..81e8865d 100644 --- a/src/dlangui/graphics/gldrawbuf.d +++ b/src/dlangui/graphics/gldrawbuf.d @@ -445,8 +445,7 @@ private class GLImageCache { // unset rotation glSupport.setRotation(rx, ry, 0); // glMatrixMode(GL_PROJECTION); - // glPopMatrix(); - // checkError("pop matrix"); + // checkgl!glPopMatrix(); } } diff --git a/src/dlangui/graphics/glsupport.d b/src/dlangui/graphics/glsupport.d index df09018f..1eef9993 100644 --- a/src/dlangui/graphics/glsupport.d +++ b/src/dlangui/graphics/glsupport.d @@ -200,8 +200,7 @@ class GLProgram { } initialized = true; Log.v("Program is initialized successfully"); - glUseProgram(0); - checkError("glUseProgram " ~ to!string(program)); + checkgl!glUseProgram(0); return !error; } bool initLocations() { @@ -217,8 +216,7 @@ class GLProgram { return true; } void release() { - glUseProgram(0); - checkError("glUseProgram(0)"); + checkgl!glUseProgram(0); } ~this() { clear(); @@ -278,16 +276,13 @@ class SolidFillProgram : GLProgram { void beforeExecute() { glEnable(GL_BLEND); - glDisable(GL_CULL_FACE); - checkError("glDisable(GL_CULL_FACE)"); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + checkgl!glDisable(GL_CULL_FACE); + checkgl!glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - checkError("glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)"); bind(); //glUniformMatrix4fv(matrixLocation, 1, false, m.value_ptr); //glUniformMatrix4fv(matrixLocation, 1, false, matrix.ptr); - checkgl!glUniformMatrix4fv(matrixLocation, 1, false, glSupport.qtmatrix.ptr); - //checkError("glUniformMatrix4fv"); + checkgl!glUniformMatrix4fv(matrixLocation, 1, false, glSupport.qtmatrix.ptr); } void afterExecute() { @@ -300,16 +295,13 @@ class SolidFillProgram : GLProgram { override bool initLocations() { bool res = super.initLocations(); - matrixLocation = glGetUniformLocation(program, "matrix"); - checkError("glGetUniformLocation matrix"); + matrixLocation = checkgl!glGetUniformLocation(program, toStringz("matrix")); if (matrixLocation == -1) Log.e("glGetUniformLocation failed for matrixLocation"); - vertexLocation = glGetAttribLocation(program, "vertex"); - checkError("glGetAttribLocation vertex"); + vertexLocation = checkgl!glGetAttribLocation(program, toStringz("vertex")); if (vertexLocation == -1) Log.e("glGetUniformLocation failed for vertexLocation"); - colAttrLocation = glGetAttribLocation(program, "colAttr"); - checkError("glGetAttribLocation colAttr"); + colAttrLocation = checkgl!glGetAttribLocation(program, toStringz("colAttr")); if (colAttrLocation == -1) Log.e("glGetUniformLocation failed for colAttrLocation"); return res && matrixLocation >= 0 && vertexLocation >= 0 && colAttrLocation >= 0; @@ -331,8 +323,7 @@ class SolidFillProgram : GLProgram { glEnableVertexAttribArray(vertexLocation); glEnableVertexAttribArray(colAttrLocation); - glDrawArrays(GL_TRIANGLES, 0, cast(int)vertices.length/3); - checkError("glDrawArrays"); + checkgl!glDrawArrays(GL_TRIANGLES, 0, cast(int)vertices.length/3); glDisableVertexAttribArray(vertexLocation); glDisableVertexAttribArray(colAttrLocation); @@ -362,8 +353,7 @@ class LineProgram : SolidFillProgram { glEnableVertexAttribArray(vertexLocation); glEnableVertexAttribArray(colAttrLocation); - glDrawArrays(GL_LINES, 0, cast(int)vertices.length/3); - checkError("glDrawArrays"); + checkgl!glDrawArrays(GL_LINES, 0, cast(int)vertices.length/3); glDisableVertexAttribArray(vertexLocation); glDisableVertexAttribArray(colAttrLocation); @@ -435,8 +425,7 @@ class TextureProgram : SolidFillProgram { glEnableVertexAttribArray(colAttrLocation); glEnableVertexAttribArray(texCoordLocation); - glDrawArrays(GL_TRIANGLES, 0, cast(int)vertices.length/3); - checkError("glDrawArrays"); + checkgl!glDrawArrays(GL_TRIANGLES, 0, cast(int)vertices.length/3); glDisableVertexAttribArray(vertexLocation); glDisableVertexAttribArray(colAttrLocation); @@ -492,17 +481,14 @@ class FontProgram : SolidFillProgram { override void beforeExecute() { glEnable(GL_BLEND); - glDisable(GL_CULL_FACE); - checkError("glDisable(GL_CULL_FACE)"); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + checkgl!glDisable(GL_CULL_FACE); + checkgl!glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //glBlendFunc(GL_ONE, GL_SRC_COLOR); //glBlendFunc(GL_ONE, GL_SRC_COLOR); //glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR); //glBlendFunc(GL_ONE_MINUS_SRC_COLOR, GL_SRC_COLOR); - checkError("glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR)"); bind(); - checkgl!glUniformMatrix4fv(matrixLocation, 1, false, glSupport.qtmatrix.ptr); - //checkError("glUniformMatrix4fv"); + checkgl!glUniformMatrix4fv(matrixLocation, 1, false, glSupport.qtmatrix.ptr); } override void afterExecute() { @@ -531,8 +517,7 @@ class FontProgram : SolidFillProgram { glEnableVertexAttribArray(colAttrLocation); glEnableVertexAttribArray(texCoordLocation); - glDrawArrays(GL_TRIANGLES, 0, cast(int)vertices.length/3); - checkError("glDrawArrays"); + checkgl!glDrawArrays(GL_TRIANGLES, 0, cast(int)vertices.length/3); glDisableVertexAttribArray(vertexLocation); glDisableVertexAttribArray(colAttrLocation); @@ -707,19 +692,13 @@ class GLSupport { glDisable(GL_CULL_FACE); glEnable(GL_BLEND); glDisable(GL_ALPHA_TEST); - glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - checkError("glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)"); - glEnableClientState(GL_VERTEX_ARRAY); - checkError("glEnableClientState(GL_VERTEX_ARRAY)"); - glEnableClientState(GL_COLOR_ARRAY); - checkError("glEnableClientState(GL_COLOR_ARRAY)"); - glVertexPointer(3, GL_FLOAT, 0, cast(void*)vertices.ptr); - checkError("glVertexPointer(3, GL_FLOAT, 0, vertices)"); - glColorPointer(4, GL_FLOAT, 0, cast(void*)colors); - checkError("glColorPointer(4, GL_FLOAT, 0, colors)"); + 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*)vertices.ptr); + checkgl!glColorPointer(4, GL_FLOAT, 0, cast(void*)colors); - glDrawArrays(GL_TRIANGLES, 0, 6); - checkError("glDrawArrays(GL_TRIANGLES, 0, 6)"); + checkgl!glDrawArrays(GL_TRIANGLES, 0, 6); glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); @@ -777,24 +756,16 @@ class GLSupport { glDisable(GL_ALPHA_TEST); glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - checkError("glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)"); + checkgl!glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnableClientState(GL_COLOR_ARRAY); - checkError("glEnableClientState(GL_COLOR_ARRAY)"); - glEnableClientState(GL_VERTEX_ARRAY); - checkError("glEnableClientState(GL_VERTEX_ARRAY)"); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - checkError("glEnableClientState(GL_TEXTURE_COORD_ARRAY)"); - glVertexPointer(3, GL_FLOAT, 0, cast(void*)vertices.ptr); - checkError("glVertexPointer(3, GL_FLOAT, 0, vertices)"); - glTexCoordPointer(2, GL_FLOAT, 0, cast(void*)texcoords.ptr); - checkError("glTexCoordPointer(2, GL_FLOAT, 0, texcoords)"); - glColorPointer(4, GL_FLOAT, 0, cast(void*)colors.ptr); - checkError("glColorPointer(4, GL_FLOAT, 0, colors)"); + checkgl!glEnableClientState(GL_COLOR_ARRAY); + checkgl!glEnableClientState(GL_VERTEX_ARRAY); + checkgl!glEnableClientState(GL_TEXTURE_COORD_ARRAY); + checkgl!glVertexPointer(3, GL_FLOAT, 0, cast(void*)vertices.ptr); + checkgl!glTexCoordPointer(2, GL_FLOAT, 0, cast(void*)texcoords.ptr); + checkgl!glColorPointer(4, GL_FLOAT, 0, cast(void*)colors.ptr); - glDrawArrays(GL_TRIANGLES, 0, 6); - checkError("glDrawArrays"); + checkgl!glDrawArrays(GL_TRIANGLES, 0, 6); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); @@ -849,24 +820,16 @@ class GLSupport { glDisable(GL_ALPHA_TEST); glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - checkError("glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)"); + checkgl!glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnableClientState(GL_COLOR_ARRAY); - checkError("glEnableClientState(GL_COLOR_ARRAY)"); - glEnableClientState(GL_VERTEX_ARRAY); - checkError("glEnableClientState(GL_VERTEX_ARRAY)"); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - checkError("glEnableClientState(GL_TEXTURE_COORD_ARRAY)"); - glVertexPointer(3, GL_FLOAT, 0, cast(void*)vertices.ptr); - checkError("glVertexPointer(3, GL_FLOAT, 0, vertices)"); - glTexCoordPointer(2, GL_FLOAT, 0, cast(void*)texcoords.ptr); - checkError("glTexCoordPointer(2, GL_FLOAT, 0, texcoords)"); - glColorPointer(4, GL_FLOAT, 0, cast(void*)colors.ptr); - checkError("glColorPointer(4, GL_FLOAT, 0, colors)"); + checkgl!glEnableClientState(GL_COLOR_ARRAY); + checkgl!glEnableClientState(GL_VERTEX_ARRAY); + checkgl!glEnableClientState(GL_TEXTURE_COORD_ARRAY); + checkgl!glVertexPointer(3, GL_FLOAT, 0, cast(void*)vertices.ptr); + checkgl!glTexCoordPointer(2, GL_FLOAT, 0, cast(void*)texcoords.ptr); + checkgl!glColorPointer(4, GL_FLOAT, 0, cast(void*)colors.ptr); - glDrawArrays(GL_TRIANGLES, 0, 6); - checkError("glDrawArrays"); + checkgl!glDrawArrays(GL_TRIANGLES, 0, 6); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); @@ -882,15 +845,13 @@ class GLSupport { /// call glFlush void flushGL() { - glFlush(); - checkError("glFlush"); + checkgl!glFlush(); } bool setTextureImage(Tex2D texture, int dx, int dy, ubyte * pixels) { - //checkError("before setTextureImage"); + checkError("before setTextureImage"); texture.setup(); - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - checkError("updateTexture - glPixelStorei"); + checkgl!glPixelStorei(GL_UNPACK_ALIGNMENT, 1); texture.setSamplerParams(true, true); // ORIGINAL: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dx, dy, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); @@ -899,15 +860,13 @@ class GLSupport { Log.e("Cannot set image for texture"); return false; } - checkError("after setTextureImage"); return true; } bool setTextureImageAlpha(Tex2D texture, int dx, int dy, ubyte * pixels) { checkError("before setTextureImageAlpha"); texture.setup(); - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - checkError("setTextureImageAlpha - glPixelStorei"); + checkgl!glPixelStorei(GL_UNPACK_ALIGNMENT, 1); texture.setSamplerParams(true, true); glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, dx, dy, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels); @@ -916,7 +875,6 @@ class GLSupport { return false; } texture.unbind(); - checkError("after setTextureImageAlpha"); return true; } @@ -935,25 +893,19 @@ class GLSupport { return false; fbo = f; - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, dx, dy, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, null); - checkError("glTexImage2D"); + checkgl!glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, dx, dy, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, null); texture.setSamplerParams(true, true); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.ID, 0); - checkError("glFramebufferTexture2D"); + checkgl!glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.ID, 0); // Always check that our framebuffer is ok - if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { + if(checkgl!glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { Log.e("glFramebufferTexture2D failed"); res = false; } - checkError("glCheckFramebufferStatus"); //glClearColor(0.5f, 0, 0, 1); - glClearColor(0.5f, 0.5f, 0.5f, 1.0f); - checkError("glClearColor"); - glClear(GL_COLOR_BUFFER_BIT); - checkError("glClear"); - checkError("after createFramebuffer"); + checkgl!glClearColor(0.5f, 0.5f, 0.5f, 1.0f); + checkgl!glClear(GL_COLOR_BUFFER_BIT); //CRLog::trace("CRGLSupportImpl::createFramebuffer %d,%d texture=%d, buffer=%d", dx, dy, textureId, framebufferId); currentFBO = fbo; @@ -1064,18 +1016,15 @@ class GLSupport { if (_legacyMode) { glMatrixMode(GL_PROJECTION); - //glPushMatrix(); - //checkError("glPushMatrix"); + //checkgl!glPushMatrix(); //glLoadIdentity(); glLoadMatrixf(qtmatrix.ptr); //glOrthof(0, _dx, 0, _dy, -1.0f, 1.0f); glMatrixMode(GL_MODELVIEW); - //glPushMatrix(); - //checkError("glPushMatrix"); + //checkgl!glPushMatrix(); glLoadIdentity(); } - glViewport(view.left, view.top, view.right, view.bottom); - checkError("glViewport"); + checkgl!glViewport(view.left, view.top, view.right, view.bottom); } void setPerspectiveProjection(float fieldOfView, float aspectRatio, float nearPlane, float farPlane) { @@ -1092,15 +1041,13 @@ class GLObject(GLObjectTypes type, GLuint target = 0) { private GLuint id; this() { - mixin("glGen" ~ to!string(type) ~ "s(1, &id);"); - checkError("glGen" ~ to!string(type)); + mixin("checkgl!glGen" ~ to!string(type) ~ "s(1, &id);"); bind(); - } + } ~this() { unbind(); - mixin("glDelete" ~ to!string(type) ~ "s(1, &id);"); - checkError("glDelete" ~ to!string(type)); + mixin("checkgl!glDelete" ~ to!string(type) ~ "s(1, &id);"); } void bind() { @@ -1112,11 +1059,10 @@ class GLObject(GLObjectTypes type, GLuint target = 0) { void unbind() { static if(target != 0) - mixin("glBind" ~ to!string(type) ~ "(" ~ to!string(target) ~ ", 0);"); + mixin("checkgl!glBind" ~ to!string(type) ~ "(" ~ to!string(target) ~ ", 0);"); else - mixin("glBind" ~ to!string(type) ~ "(0);"); - checkError("unbind " ~ to!string(type)); - } + mixin("checkgl!glBind" ~ to!string(type) ~ "(0);"); + } static if(type == GLObjectTypes.Buffer) { @@ -1125,9 +1071,9 @@ class GLObject(GLObjectTypes type, GLuint target = 0) { foreach(b; buffs) length += b.length; glBufferData(target, - length * float.sizeof, - null, - GL_STREAM_DRAW); + length * float.sizeof, + null, + GL_STREAM_DRAW); int offset; foreach(b; buffs) { glBufferSubData(target, From 2280b56171d61c5aff50582c02f5b37c9e8f2be9 Mon Sep 17 00:00:00 2001 From: gazer Date: Sat, 12 Dec 2015 22:05:59 +0300 Subject: [PATCH 13/14] checkgl should be only in debug --- src/dlangui/graphics/glsupport.d | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/dlangui/graphics/glsupport.d b/src/dlangui/graphics/glsupport.d index 1eef9993..5345553f 100644 --- a/src/dlangui/graphics/glsupport.d +++ b/src/dlangui/graphics/glsupport.d @@ -79,10 +79,14 @@ static this() { * TODO use one of the DEBUG extensions */ /// Using: checkgl!glFunction(funcParams); -auto checkgl(alias func, string functionName=__FUNCTION__, int line=__LINE__, Args...)(Args args) +template checkgl(alias func) { - scope(success) checkError(func.stringof, functionName, line); - return func(args); + debug auto checkgl(alias func, string functionName=__FUNCTION__, int line=__LINE__, Args...)(Args args) + { + scope(success) checkError(func.stringof, functionName, line); + return func(args); + } else + alias checkgl = func; } bool checkError(string context="", string functionName=__FUNCTION__, int line=__LINE__) { From b7be028039e973820d11e815071739e453caff57 Mon Sep 17 00:00:00 2001 From: gazer Date: Sat, 12 Dec 2015 23:26:11 +0300 Subject: [PATCH 14/14] fix --- src/dlangui/graphics/glsupport.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dlangui/graphics/glsupport.d b/src/dlangui/graphics/glsupport.d index 5345553f..7c9d3b45 100644 --- a/src/dlangui/graphics/glsupport.d +++ b/src/dlangui/graphics/glsupport.d @@ -81,7 +81,7 @@ static this() { /// Using: checkgl!glFunction(funcParams); template checkgl(alias func) { - debug auto checkgl(alias func, string functionName=__FUNCTION__, int line=__LINE__, Args...)(Args args) + debug auto checkgl(string functionName=__FUNCTION__, int line=__LINE__, Args...)(Args args) { scope(success) checkError(func.stringof, functionName, line); return func(args);