Code Monkey home page Code Monkey logo

Comments (5)

peterhellberg avatar peterhellberg commented on May 18, 2024

Calling canvas.Clear works

package main

import (
	"image"
	"image/color"
	"image/draw"
	"os"

	"github.com/faiface/pixel"
	"github.com/faiface/pixel/pixelgl"
)

var (
	w, h = 256, 256

	green  = color.RGBA{100, 255, 100, 255}
	orange = color.RGBA{255, 50, 0, 255}

	cfg = pixelgl.WindowConfig{
		Bounds:      pixel.R(0, 0, float64(w), float64(h)),
		VSync:       true,
		Undecorated: true,
	}
)

func run() {
	win, err := pixelgl.NewWindow(cfg)
	if err != nil {
		panic(err)
	}

	canvas := pixelgl.NewCanvas(win.Bounds())

	// This works just fine
	canvas.Clear(green)

	if os.Getenv("CRASH") == "yes" {
		// This results in a SIGSEGV
		canvas.Texture().SetPixels(0, 0, w, h, newUniformImage(w, h, orange).Pix)
	}

	for !win.Closed() {
		win.SetClosed(win.JustPressed(pixelgl.KeyEscape))

		canvas.Draw(win)
		win.Update()
	}
}

func newUniformImage(w, h int, c color.Color) *image.RGBA {
	m := image.NewRGBA(image.Rect(0, 0, w, h))

	draw.Draw(m, m.Bounds(), &image.Uniform{c}, image.ZP, draw.Src)

	return m
}

func main() {
	pixelgl.Run(run)
}

If running this code with CRASH=yes go run setpixels-crash.go it results in a SIGSEGV

fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7fff7d66457c]

runtime stack:
runtime.throw(0x41634f9, 0x2a)
	/usr/local/Cellar/go/1.8.1/libexec/src/runtime/panic.go:596 +0x95
runtime.sigpanic()
	/usr/local/Cellar/go/1.8.1/libexec/src/runtime/signal_unix.go:274 +0x2db

exit status 21: syscall [locked] [Created by mainthread.Run @ mainthread.go:41]
    runtime    cgocall.go:131        cgocall(0x40f42d0, 0xc42003be48, 0x411c2e0)
    gl         _cgo_gotypes.go:10291 _Cfunc_glowTexSubImage2D(#5, 0xde1, 0, #3, #4, #2)
    gl         package.go:7443       TexSubImage2D.func1(140735297242449, 3553, 0, 1099511628032, 21994527529224, 842351583232)
    gl         package.go:7443       TexSubImage2D(3553, 0, 1099511628032, 21994527529224, 842351583232)
    glhf       texture.go:93         (*Texture).SetPixels(*Texture(0xc42001a320), 0, 0, 256, 256, []uint8(#2 len=262144 cap=262144))
    main       setpixels-crash.go:39 run()
    mainthread mainthread.go:39      Run.func1(func(#1))
    runtime    asm_amd64.s:2197      goexit()
1: select [locked]
    mainthread mainthread.go:44      Run(#1)
    pixelgl    run.go:32             Run(func(#1))
    main       setpixels-crash.go:59 main()
1: syscall [locked]
    runtime    asm_amd64.s:2197      goexit()

from pixel.

faiface avatar faiface commented on May 18, 2024

Thanks for the issue! The trouble is, that Canvas.Texture() returns a *Texture value from the "github.com/faiface/glhf" package.

If we look at the docs of the GLHF package (https://godoc.org/github.com/faiface/glhf), we find this:

All calls should be done from the main thread using "github.com/faiface/mainthread" package.

The thing is, that OpenGL is thread sensitive and all of the OpenGL calls should be done from the main thread of your program. If you violate that, the program might crash. In your case, you certainly violated that.

Since no user of Pixel should really need to bother with the GLHF package and this functionality of "replacing the Canvas pixels" is really useful, I've added two methods to Canvas: SetPixels and Pixels.

Use those instead. If you're interested in how this whole thing should have been done, I recommend you taking a look at the source of those methods. It's quite easy.

from pixel.

peterhellberg avatar peterhellberg commented on May 18, 2024

I suspected that it had to do with being on the wrong thread, thank you for clarifying this and that you added the new methods.

from pixel.

faiface avatar faiface commented on May 18, 2024

Cool. If there's no trouble with the new methods, I'll close this issue.

from pixel.

peterhellberg avatar peterhellberg commented on May 18, 2024

I have now verified that SetPixels work the way it is supposed to. Thank you.

from pixel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.