From cd9b6c96db8cf349f880ccffd67da1c6f8d01567 Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Fri, 21 Jul 2023 20:09:08 +0100 Subject: [PATCH 1/4] Resolve an issue with bad optimisation or memory corruption on context share on some Windows GPU Fixes #4000 --- internal/painter/gl/draw.go | 26 +++++++++---------- internal/painter/gl/shaders.go | 4 +-- internal/painter/gl/shaders/rectangle.vert | 2 +- internal/painter/gl/shaders/rectangle_es.vert | 3 +-- 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/internal/painter/gl/draw.go b/internal/painter/gl/draw.go index 04d60826d4..680f554c91 100644 --- a/internal/painter/gl/draw.go +++ b/internal/painter/gl/draw.go @@ -106,7 +106,7 @@ func (p *painter) drawRectangle(rect *canvas.Rectangle, pos fyne.Position, frame } // Vertex: BEG - points := p.vecRectCoords(pos, rect, frame) + bounds, points := p.vecRectCoords(pos, rect, frame) p.ctx.UseProgram(program) vbo := p.createBuffer(points) p.defineVertexArray(program, "vert", 2, 4, 0) @@ -122,7 +122,7 @@ func (p *painter) drawRectangle(rect *canvas.Rectangle, pos fyne.Position, frame p.ctx.Uniform2f(frameSizeUniform, frameWidthScaled, frameHeightScaled) rectCoordsUniform := p.ctx.GetUniformLocation(program, "rect_coords") - x1Scaled, x2Scaled, y1Scaled, y2Scaled := p.scaleRectCoords(points[0], points[4], points[1], points[9]) + x1Scaled, x2Scaled, y1Scaled, y2Scaled := p.scaleRectCoords(bounds[0], bounds[2], bounds[1], bounds[3]) p.ctx.Uniform4f(rectCoordsUniform, x1Scaled, x2Scaled, y1Scaled, y2Scaled) strokeWidthScaled := roundToPixel(rect.StrokeWidth*p.pixScale, 1.0) @@ -337,7 +337,7 @@ func rectInnerCoords(size fyne.Size, pos fyne.Position, fill canvas.ImageFill, a return size, pos } -func (p *painter) vecRectCoords(pos fyne.Position, rect *canvas.Rectangle, frame fyne.Size) []float32 { +func (p *painter) vecRectCoords(pos fyne.Position, rect *canvas.Rectangle, frame fyne.Size) ([4]float32, []float32) { size := rect.Size() pos1 := rect.Position() @@ -350,21 +350,21 @@ func (p *painter) vecRectCoords(pos fyne.Position, rect *canvas.Rectangle, frame x1Pos := pos1.X x1Norm := -1 + x1Pos*2/frame.Width - x2Pos := (pos1.X + size.Width) + x2Pos := pos1.X + size.Width x2Norm := -1 + x2Pos*2/frame.Width y1Pos := pos1.Y y1Norm := 1 - y1Pos*2/frame.Height - y2Pos := (pos1.Y + size.Height) + y2Pos := pos1.Y + size.Height y2Norm := 1 - y2Pos*2/frame.Height + + // output a norm for the fill and the vert is unused, but we pass 0 to avoid optimisation issues coords := []float32{ - x1Pos, y1Pos, x1Norm, y1Norm, // 1. triangle - x2Pos, y1Pos, x2Norm, y1Norm, - x1Pos, y2Pos, x1Norm, y2Norm, - x1Pos, y2Pos, x1Norm, y2Norm, // 2. triangle - x2Pos, y1Pos, x2Norm, y1Norm, - x2Pos, y2Pos, x2Norm, y2Norm} - - return coords + 0, 0, x1Norm, y1Norm, // first triangle + 0, 0, x2Norm, y1Norm, // second triangle + 0, 0, x1Norm, y2Norm, + 0, 0, x2Norm, y2Norm} + + return [4]float32{x1Pos, y1Pos, x2Pos, y2Pos}, coords } func roundToPixel(v float32, pixScale float32) float32 { diff --git a/internal/painter/gl/shaders.go b/internal/painter/gl/shaders.go index 65fbe8d0c2..fb7cf999b9 100644 --- a/internal/painter/gl/shaders.go +++ b/internal/painter/gl/shaders.go @@ -33,7 +33,7 @@ var shaderRectangleFrag = &fyne.StaticResource{ var shaderRectangleVert = &fyne.StaticResource{ StaticName: "rectangle.vert", StaticContent: []byte( - "#version 110\n\nattribute vec2 vert;\nattribute vec2 normal;\n\nvoid main() {\n gl_Position = vec4(normal, 0, 1);\n}\n"), + "#version 110\n\nattribute vec2 vert;\nattribute vec2 normal;\n\nvoid main() {\n gl_Position = vec4(vert+normal, 0, 1);\n}\n"), } var shaderRectangleesFrag = &fyne.StaticResource{ StaticName: "rectangle_es.frag", @@ -43,7 +43,7 @@ var shaderRectangleesFrag = &fyne.StaticResource{ var shaderRectangleesVert = &fyne.StaticResource{ StaticName: "rectangle_es.vert", StaticContent: []byte( - "#version 100\n\n#ifdef GL_ES\n# ifdef GL_FRAGMENT_PRECISION_HIGH\nprecision highp float;\n# else\nprecision mediump float;\n#endif\nprecision mediump int;\nprecision lowp sampler2D;\n#endif\n\nattribute vec2 vert;\nattribute vec2 normal;\n\nvoid main() {\n vert; //Workaround, because WebGL optimizes away attributes unused\n gl_Position = vec4(normal, 0, 1);\n}\n"), + "#version 100\n\n#ifdef GL_ES\n# ifdef GL_FRAGMENT_PRECISION_HIGH\nprecision highp float;\n# else\nprecision mediump float;\n#endif\nprecision mediump int;\nprecision lowp sampler2D;\n#endif\n\nattribute vec2 vert;\nattribute vec2 normal;\n\nvoid main() {\n gl_Position = vec4(vert+normal, 0, 1);\n}\n"), } var shaderRoundrectangleFrag = &fyne.StaticResource{ StaticName: "round_rectangle.frag", diff --git a/internal/painter/gl/shaders/rectangle.vert b/internal/painter/gl/shaders/rectangle.vert index 1195ff9e43..d2aae9bfb8 100644 --- a/internal/painter/gl/shaders/rectangle.vert +++ b/internal/painter/gl/shaders/rectangle.vert @@ -4,5 +4,5 @@ attribute vec2 vert; attribute vec2 normal; void main() { - gl_Position = vec4(normal, 0, 1); + gl_Position = vec4(vert+normal, 0, 1); } diff --git a/internal/painter/gl/shaders/rectangle_es.vert b/internal/painter/gl/shaders/rectangle_es.vert index eb9690f341..c46159dcdf 100644 --- a/internal/painter/gl/shaders/rectangle_es.vert +++ b/internal/painter/gl/shaders/rectangle_es.vert @@ -14,6 +14,5 @@ attribute vec2 vert; attribute vec2 normal; void main() { - vert; //Workaround, because WebGL optimizes away attributes unused - gl_Position = vec4(normal, 0, 1); + gl_Position = vec4(vert+normal, 0, 1); } From 5314d545da4901a0c5ee200ab5dbbe447a4bd9a6 Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Fri, 21 Jul 2023 20:09:41 +0100 Subject: [PATCH 2/4] Also remove unused shaders --- internal/painter/gl/shaders/gray.frag | 11 ----------- internal/painter/gl/shaders/gray_es.frag | 21 --------------------- 2 files changed, 32 deletions(-) delete mode 100644 internal/painter/gl/shaders/gray.frag delete mode 100644 internal/painter/gl/shaders/gray_es.frag diff --git a/internal/painter/gl/shaders/gray.frag b/internal/painter/gl/shaders/gray.frag deleted file mode 100644 index ae8c2eb258..0000000000 --- a/internal/painter/gl/shaders/gray.frag +++ /dev/null @@ -1,11 +0,0 @@ -#version 110 - -uniform vec4 color; - -uniform sampler2D tex; -varying vec2 fragTexCoord; - -void main() -{ - gl_FragColor = vec4(color.r, color.g, color.b, texture2D(tex, fragTexCoord).r*color.a); -} diff --git a/internal/painter/gl/shaders/gray_es.frag b/internal/painter/gl/shaders/gray_es.frag deleted file mode 100644 index d2dc74c02f..0000000000 --- a/internal/painter/gl/shaders/gray_es.frag +++ /dev/null @@ -1,21 +0,0 @@ -#version 100 - -#ifdef GL_ES -# ifdef GL_FRAGMENT_PRECISION_HIGH -precision highp float; -# else -precision mediump float; -#endif -precision mediump int; -precision lowp sampler2D; -#endif - -uniform vec4 color; - -uniform sampler2D tex; -varying vec2 fragTexCoord; - -void main() -{ - gl_FragColor = vec4(color.r, color.g, color.b, texture2D(tex, fragTexCoord).r*color.a); -} From 27eed691c6ce3087308d0a7667b27455d914ec40 Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Fri, 21 Jul 2023 22:02:28 +0100 Subject: [PATCH 3/4] Missed line for the patch -> PR sorry --- cmd/hello/FyneApp.toml | 6 ------ internal/painter/gl/draw.go | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) delete mode 100644 cmd/hello/FyneApp.toml diff --git a/cmd/hello/FyneApp.toml b/cmd/hello/FyneApp.toml deleted file mode 100644 index 5b28851ceb..0000000000 --- a/cmd/hello/FyneApp.toml +++ /dev/null @@ -1,6 +0,0 @@ -[Details] - Icon = "../../theme/icons/fyne.png" - Name = "Fyne Hello" - ID = "io.fyne.hello" - Version = "2.3.0" - Build = 1 diff --git a/internal/painter/gl/draw.go b/internal/painter/gl/draw.go index 680f554c91..981e02a65a 100644 --- a/internal/painter/gl/draw.go +++ b/internal/painter/gl/draw.go @@ -158,7 +158,7 @@ func (p *painter) drawRectangle(rect *canvas.Rectangle, pos fyne.Position, frame p.logError() // Fragment: END - p.ctx.DrawArrays(triangles, 0, 6) + p.ctx.DrawArrays(triangleStrip, 0, 4) p.logError() p.freeBuffer(vbo) } From 8ddd2ef099a61d11ddac375cf79d4264654eeb39 Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Sat, 22 Jul 2023 19:22:19 +0100 Subject: [PATCH 4/4] Re-add accidental deleted file --- cmd/hello/FyneApp.toml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 cmd/hello/FyneApp.toml diff --git a/cmd/hello/FyneApp.toml b/cmd/hello/FyneApp.toml new file mode 100644 index 0000000000..d59c3ed642 --- /dev/null +++ b/cmd/hello/FyneApp.toml @@ -0,0 +1,6 @@ +[Details] + Icon = "../../theme/icons/fyne.png" + Name = "Fyne Hello" + ID = "io.fyne.hello" + Version = "2.3.0" + Build = 2