forked from joejulian/gio
gpu: fix clip intersection with the D3D backend
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"gioui.org/f32"
|
||||
"gioui.org/op"
|
||||
"gioui.org/op/clip"
|
||||
"gioui.org/op/paint"
|
||||
)
|
||||
|
||||
@@ -39,6 +40,64 @@ func TestHeadless(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestClipping(t *testing.T) {
|
||||
w, release := newTestWindow(t)
|
||||
defer release()
|
||||
|
||||
sz := w.size
|
||||
col := color.RGBA{A: 0xff, R: 0xca, G: 0xfe}
|
||||
col2 := color.RGBA{A: 0xff, R: 0x00, G: 0xfe}
|
||||
var ops op.Ops
|
||||
pop := paint.PaintOp{Rect: f32.Rectangle{Max: f32.Point{
|
||||
X: float32(sz.X),
|
||||
Y: float32(sz.Y),
|
||||
}}}
|
||||
paint.ColorOp{Color: col}.Add(&ops)
|
||||
clip.Rect{
|
||||
Rect: f32.Rectangle{
|
||||
Min: f32.Point{X: 50, Y: 50},
|
||||
Max: f32.Point{X: 250, Y: 250},
|
||||
},
|
||||
SE: 75,
|
||||
}.Op(&ops).Add(&ops)
|
||||
pop.Add(&ops)
|
||||
paint.ColorOp{Color: col2}.Add(&ops)
|
||||
clip.Rect{
|
||||
Rect: f32.Rectangle{
|
||||
Min: f32.Point{X: 100, Y: 100},
|
||||
Max: f32.Point{X: 350, Y: 350},
|
||||
},
|
||||
NW: 75,
|
||||
}.Op(&ops).Add(&ops)
|
||||
pop.Add(&ops)
|
||||
w.Frame(&ops)
|
||||
|
||||
img, err := w.Screenshot()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if *dumpImages {
|
||||
if err := saveImage("clip.png", img); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
bg := color.RGBA{A: 0xff, R: 0xff, G: 0xff, B: 0xff}
|
||||
tests := []struct {
|
||||
x, y int
|
||||
color color.RGBA
|
||||
}{
|
||||
{120, 120, col},
|
||||
{130, 130, col2},
|
||||
{210, 210, col2},
|
||||
{230, 230, bg},
|
||||
}
|
||||
for _, test := range tests {
|
||||
if got := img.RGBAAt(test.x, test.y); got != test.color {
|
||||
t.Errorf("(%d,%d): got color %v, expected %v", test.x, test.y, got, test.color)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newTestWindow(t *testing.T) (*Window, func()) {
|
||||
t.Helper()
|
||||
sz := image.Point{X: 800, Y: 600}
|
||||
|
||||
+7
-1
@@ -538,15 +538,21 @@ func (r *renderer) intersectPath(p *pathOp, clip image.Rectangle) {
|
||||
if !p.path {
|
||||
return
|
||||
}
|
||||
o := p.place.Pos.Add(clip.Min).Sub(p.clip.Min)
|
||||
uv := image.Rectangle{
|
||||
Min: p.place.Pos,
|
||||
Max: p.place.Pos.Add(p.clip.Size()),
|
||||
}
|
||||
o := clip.Min.Sub(p.clip.Min)
|
||||
sub := image.Rectangle{
|
||||
Min: o,
|
||||
Max: o.Add(clip.Size()),
|
||||
}
|
||||
fbo := r.pather.stenciler.cover(p.place.Idx)
|
||||
r.ctx.BindTexture(0, fbo.tex)
|
||||
coverScale, coverOff := texSpaceTransform(toRectF(uv), fbo.size)
|
||||
subScale, subOff := texSpaceTransform(toRectF(sub), p.clip.Size())
|
||||
r.pather.stenciler.iprog.uniforms.vert.uvTransform = [4]float32{coverScale.X, coverScale.Y, coverOff.X, coverOff.Y}
|
||||
r.pather.stenciler.iprog.uniforms.vert.subUVTransform = [4]float32{subScale.X, subScale.Y, subOff.X, subOff.Y}
|
||||
r.pather.stenciler.iprog.prog.UploadUniforms()
|
||||
r.ctx.DrawArrays(backend.DrawModeTriangleStrip, 0, 4)
|
||||
}
|
||||
|
||||
+2
-1
@@ -83,7 +83,8 @@ type stencilUniforms struct {
|
||||
|
||||
type intersectUniforms struct {
|
||||
vert struct {
|
||||
uvTransform [4]float32
|
||||
uvTransform [4]float32
|
||||
subUVTransform [4]float32
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+49
-13
File diff suppressed because one or more lines are too long
@@ -17,6 +17,23 @@ vec3[2] fboTextureTransform() {
|
||||
return t;
|
||||
}
|
||||
|
||||
// fboTransform returns a transformation
|
||||
// that cancels the implied transformation between
|
||||
// the clip space and the framebuffer.
|
||||
// Only two rows are returned. The last is implied
|
||||
// to be [0, 0, 1].
|
||||
vec3[2] fboTransform() {
|
||||
vec3[2] t;
|
||||
#ifdef HLSL
|
||||
t[0] = vec3(1.0, 0.0, 0.0);
|
||||
t[1] = vec3(0.0, 1.0, 0.0);
|
||||
#else
|
||||
t[0] = vec3(1.0, 0.0, 0.0);
|
||||
t[1] = vec3(0.0, -1.0, 0.0);
|
||||
#endif
|
||||
return t;
|
||||
}
|
||||
|
||||
// toClipSpace converts an OpenGL gl_Position value to a
|
||||
// native GPU position.
|
||||
vec4 toClipSpace(vec4 pos) {
|
||||
|
||||
@@ -4,18 +4,25 @@
|
||||
|
||||
precision highp float;
|
||||
|
||||
#include <common.inc>
|
||||
|
||||
layout(location = 0) in vec2 pos;
|
||||
layout(location = 1) in vec2 uv;
|
||||
|
||||
layout(binding = 0) uniform Block {
|
||||
vec4 uvTransform;
|
||||
vec4 subUVTransform;
|
||||
};
|
||||
|
||||
layout(location = 0) out vec2 vUV;
|
||||
|
||||
void main() {
|
||||
vec2 p = pos;
|
||||
p.y = -p.y;
|
||||
gl_Position = vec4(p, 0, 1);
|
||||
vUV = uv*uvTransform.xy + uvTransform.zw;
|
||||
vec3[2] fboTrans = fboTransform();
|
||||
vec3 p = transform3x2(fboTrans, vec3(pos, 1.0));
|
||||
gl_Position = vec4(p, 1);
|
||||
vec3[2] fboTexTrans = fboTextureTransform();
|
||||
vec3 uv3 = transform3x2(fboTexTrans, vec3(uv, 1.0));
|
||||
vUV = uv3.xy*subUVTransform.xy + subUVTransform.zw;
|
||||
vUV = transform3x2(fboTexTrans, vec3(vUV, 1.0)).xy;
|
||||
vUV = vUV*uvTransform.xy + uvTransform.zw;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user