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; }