Skip to content
This repository has been archived by the owner on Aug 15, 2023. It is now read-only.

Commit

Permalink
gl.Finish() before drawing image in a window (#94)
Browse files Browse the repository at this point in the history
Solves #93
  • Loading branch information
elgopher authored Mar 15, 2020
1 parent acfbbbc commit b83f669
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 30 deletions.
2 changes: 2 additions & 0 deletions gl/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ type API interface {
GetError() uint32
// ReadPixels reads a block of pixels from the frame buffer
ReadPixels(x int32, y int32, width int32, height int32, format uint32, xtype uint32, pixels unsafe.Pointer)
// Finish blocks until all GL execution is complete
Finish()
// Ptr takes a slice or pointer (to a singular scalar value or the first
// element of an array or slice) and returns its GL-compatible address.
//
Expand Down
28 changes: 20 additions & 8 deletions gl/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,30 @@ func (c *AcceleratedCommand) Run(output image.AcceleratedImageSelection, selecti
panic("output image created in a different OpenGL context than program")
}

c.program.use()
c.api.Enable(scissorTest)
c.api.BindFramebuffer(framebuffer, img.frameBufferID)
loc := output.Location
locHeight := loc.Height
if locHeight > img.height {
locHeight = img.height
if loc.X >= img.width {
return
}
if loc.Y >= img.height {
return
}
x := int32(loc.X)
y := int32(img.height - locHeight - loc.Y)
y := int32(loc.Y)
w := int32(loc.Width)
h := int32(locHeight)
h := int32(loc.Height)
if x < 0 {
w += x
x = 0
}
if y < 0 {
h += y
y = 0
}
y = int32(img.height) - h - y

c.program.use()
c.api.Enable(scissorTest)
c.api.BindFramebuffer(framebuffer, img.frameBufferID)
c.api.Scissor(x, y, w, h)
c.api.Viewport(x, y, w, h)

Expand Down
1 change: 1 addition & 0 deletions gl/gl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,5 +535,6 @@ func (a apiStub) GetTexImage(target uint32, level int32, format uint32, xtype ui
func (a apiStub) GetError() uint32 { return 0 }
func (a apiStub) ReadPixels(x int32, y int32, width int32, height int32, format uint32, xtype uint32, pixels unsafe.Pointer) {
}
func (a apiStub) Finish() {}
func (a apiStub) Ptr(data interface{}) unsafe.Pointer { return nil }
func (a apiStub) PtrOffset(offset int) unsafe.Pointer { return nil }
34 changes: 32 additions & 2 deletions gl/integration/glfw/opengl_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,16 +510,46 @@ func TestAcceleratedCommand_Run(t *testing.T) {
location: image.AcceleratedImageLocation{},
expectedColors: []image.Color{image.Transparent},
},
"x out of bounds": {
"x equal image width": {
width: 1, height: 1,
location: image.AcceleratedImageLocation{X: 1, Width: 1, Height: 1},
expectedColors: []image.Color{image.Transparent},
},
"y out of bounds": {
"x higher than image width": {
width: 1, height: 1,
location: image.AcceleratedImageLocation{X: 2, Width: 1, Height: 1},
expectedColors: []image.Color{image.Transparent},
},
"negative x": {
width: 1, height: 1,
location: image.AcceleratedImageLocation{X: -1, Width: 1, Height: 1},
expectedColors: []image.Color{image.Transparent},
},
"negative x, width 2": {
width: 1, height: 1,
location: image.AcceleratedImageLocation{X: -1, Width: 2, Height: 1},
expectedColors: []image.Color{color},
},
"y equal image height": {
width: 1, height: 1,
location: image.AcceleratedImageLocation{Y: 1, Width: 1, Height: 1},
expectedColors: []image.Color{image.Transparent},
},
"y higher than image height": {
width: 1, height: 1,
location: image.AcceleratedImageLocation{Y: 2, Width: 1, Height: 1},
expectedColors: []image.Color{image.Transparent},
},
"negative y": {
width: 1, height: 1,
location: image.AcceleratedImageLocation{Y: -1, Width: 1, Height: 1},
expectedColors: []image.Color{image.Transparent},
},
"negative y, height 2": {
width: 1, height: 1,
location: image.AcceleratedImageLocation{Y: -1, Width: 1, Height: 2},
expectedColors: []image.Color{color},
},
"whole image": {
width: 1, height: 1,
location: image.AcceleratedImageLocation{Width: 1, Height: 1},
Expand Down
7 changes: 7 additions & 0 deletions glfw/gl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,13 @@ func (g *context) ReadPixels(x int32, y int32, width int32, height int32, format
})
}

// Finish blocks until all GL execution is complete
func (g *context) Finish() {
g.run(func() {
gl.Finish()
})
}

// Ptr takes a slice or pointer (to a singular scalar value or the first
// element of an array or slice) and returns its GL-compatible address.
//
Expand Down
16 changes: 8 additions & 8 deletions glfw/glfw.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,13 @@ func (g *OpenGL) OpenWindow(width, height int, options ...WindowOption) (*Window
screenAcceleratedImage := g.context.NewAcceleratedImage(width, height)
screenImage := image.New(width, height, screenAcceleratedImage)
win := &Window{
mainThreadLoop: g.mainThreadLoop,
keyboardEvents: keyboardEvents,
requestedWidth: width,
requestedHeight: height,
screenAcceleratedImage: screenAcceleratedImage,
screenImage: screenImage,
zoom: 1,
mainThreadLoop: g.mainThreadLoop,
keyboardEvents: keyboardEvents,
requestedWidth: width,
requestedHeight: height,
screenImage: screenImage,
screenContextAPI: g.context.API(),
zoom: 1,
}
var err error
g.mainThreadLoop.Execute(func() {
Expand Down Expand Up @@ -247,7 +247,7 @@ func (g *OpenGL) OpenWindow(width, height int, options ...WindowOption) (*Window
}
// in this window context there is only one program used with one texture
win.api.UseProgram(win.program.ID())
win.api.BindTexture(gl33.TEXTURE_2D, win.screenAcceleratedImage.TextureID())
win.api.BindTexture(gl33.TEXTURE_2D, screenAcceleratedImage.TextureID())
return win, nil
}

Expand Down
25 changes: 13 additions & 12 deletions glfw/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@ import (

// Window is an implementation of loop.Screen and keyboard.EventSource
type Window struct {
glfwWindow *glfw.Window
mainThreadLoop *MainThreadLoop
screenPolygon *screenPolygon
keyboardEvents *internal.KeyboardEvents
requestedWidth int
requestedHeight int
zoom int
screenImage *image.Image
screenAcceleratedImage *gl.AcceleratedImage
api gl.API
context *gl.Context
program *gl.Program
glfwWindow *glfw.Window
mainThreadLoop *MainThreadLoop
screenPolygon *screenPolygon
keyboardEvents *internal.KeyboardEvents
requestedWidth int
requestedHeight int
zoom int
screenImage *image.Image
screenContextAPI gl.API
api gl.API
context *gl.Context
program *gl.Program
}

// Draw draws a screen image to the invisible buffer. It will be shown in window
// after SwapImages is called.
func (w *Window) Draw() {
w.screenImage.Upload()
w.screenContextAPI.Finish()
var width, height int
w.mainThreadLoop.Execute(func() {
width, height = w.glfwWindow.GetFramebufferSize()
Expand Down

0 comments on commit b83f669

Please sign in to comment.