Skip to content

Commit

Permalink
Revert "internal/restorable: remove unnecessary functions and variabl…
Browse files Browse the repository at this point in the history
…es around shaders"

This reverts commit 4f3e00e.

Updates #3083
  • Loading branch information
hajimehoshi committed Sep 7, 2024
1 parent 34639d0 commit 6453e55
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion internal/atlas/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderSrcImageCount]*Image, vertice
vertices[i+2] += oxf
vertices[i+3] += oyf
}
if shader.ir.Unit == shaderir.Texels {
if shader.ensureShader().Unit() == shaderir.Texels {
sw, sh := srcs[0].backend.restorable.InternalSize()
swf, shf := float32(sw), float32(sh)
for i := 0; i < n; i += graphics.VertexFloatCount {
Expand Down
10 changes: 2 additions & 8 deletions internal/atlas/shader.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ func (s *Shader) deallocate() {
}

var (
NearestFilterShader = &Shader{
shader: restorable.NearestFilterShader,
ir: restorable.LinearFilterShaderIR,
}
LinearFilterShader = &Shader{
shader: restorable.LinearFilterShader,
ir: restorable.LinearFilterShaderIR,
}
NearestFilterShader = &Shader{shader: restorable.NearestFilterShader}
LinearFilterShader = &Shader{shader: restorable.LinearFilterShader}
)
14 changes: 12 additions & 2 deletions internal/restorable/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import (

// images is a set of Image objects.
type images struct {
images map[*Image]struct{}
images map[*Image]struct{}
shaders map[*Shader]struct{}
}

// theImages represents the images for the current process.
var theImages = &images{
images: map[*Image]struct{}{},
images: map[*Image]struct{}{},
shaders: map[*Shader]struct{}{},
}

func SwapBuffers(graphicsDriver graphicsdriver.Graphics) error {
Expand Down Expand Up @@ -62,11 +64,19 @@ func (i *images) add(img *Image) {
i.images[img] = struct{}{}
}

func (i *images) addShader(shader *Shader) {
i.shaders[shader] = struct{}{}
}

// remove removes img from the images.
func (i *images) remove(img *Image) {
delete(i.images, img)
}

func (i *images) removeShader(shader *Shader) {
delete(i.shaders, shader)
}

var graphicsDriverInitialized bool

// InitializeGraphicsDriverState initializes the graphics driver state.
Expand Down
23 changes: 16 additions & 7 deletions internal/restorable/shader.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,37 @@ import (

type Shader struct {
shader *graphicscommand.Shader
ir *shaderir.Program
}

func NewShader(ir *shaderir.Program) *Shader {
s := &Shader{
shader: graphicscommand.NewShader(ir),
ir: ir,
}
theImages.addShader(s)
return s
}

func (s *Shader) Dispose() {
theImages.removeShader(s)
s.shader.Dispose()
s.shader = nil
s.ir = nil
}

func (s *Shader) restore() {
s.shader = graphicscommand.NewShader(s.ir)
}

func (s *Shader) Unit() shaderir.Unit {
return s.ir.Unit
}

var (
NearestFilterShader *Shader
NearestFilterShaderIR *shaderir.Program
LinearFilterShader *Shader
LinearFilterShaderIR *shaderir.Program
clearShader *Shader
NearestFilterShader *Shader
LinearFilterShader *Shader
clearShader *Shader
)

func init() {
Expand Down Expand Up @@ -79,9 +90,7 @@ func init() {
if err := wg.Wait(); err != nil {
panic(err)
}
NearestFilterShaderIR = nearestIR
NearestFilterShader = NewShader(nearestIR)
LinearFilterShaderIR = linearIR
LinearFilterShader = NewShader(linearIR)
clearShader = NewShader(clearIR)
}

0 comments on commit 6453e55

Please sign in to comment.