From 4f3e00ec3af857522828312a27d4a5705d21877a Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 9 Jan 2024 03:01:08 +0900 Subject: [PATCH] internal/restorable: remove unnecessary functions and variables around shaders Updates #805 --- internal/atlas/image.go | 2 +- internal/atlas/shader.go | 10 ++++++++-- internal/restorable/images.go | 14 ++------------ internal/restorable/shader.go | 23 +++++++---------------- 4 files changed, 18 insertions(+), 31 deletions(-) diff --git a/internal/atlas/image.go b/internal/atlas/image.go index cde31835e..d0e87dcb0 100644 --- a/internal/atlas/image.go +++ b/internal/atlas/image.go @@ -376,7 +376,7 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [ vertices[i+2] += oxf vertices[i+3] += oyf } - if shader.ensureShader().Unit() == shaderir.Texels { + if shader.ir.Unit == shaderir.Texels { sw, sh := srcs[0].backend.restorable.InternalSize() swf, shf := float32(sw), float32(sh) for i := 0; i < n; i += graphics.VertexFloatCount { diff --git a/internal/atlas/shader.go b/internal/atlas/shader.go index fe2f86d98..8861d14c7 100644 --- a/internal/atlas/shader.go +++ b/internal/atlas/shader.go @@ -73,6 +73,12 @@ func (s *Shader) deallocate() { } var ( - NearestFilterShader = &Shader{shader: restorable.NearestFilterShader} - LinearFilterShader = &Shader{shader: restorable.LinearFilterShader} + NearestFilterShader = &Shader{ + shader: restorable.NearestFilterShader, + ir: restorable.LinearFilterShaderIR, + } + LinearFilterShader = &Shader{ + shader: restorable.LinearFilterShader, + ir: restorable.LinearFilterShaderIR, + } ) diff --git a/internal/restorable/images.go b/internal/restorable/images.go index adcc9469b..48be44353 100644 --- a/internal/restorable/images.go +++ b/internal/restorable/images.go @@ -22,14 +22,12 @@ import ( // images is a set of Image objects. type images struct { - images map[*Image]struct{} - shaders map[*Shader]struct{} + images map[*Image]struct{} } // theImages represents the images for the current process. var theImages = &images{ - images: map[*Image]struct{}{}, - shaders: map[*Shader]struct{}{}, + images: map[*Image]struct{}{}, } func SwapBuffers(graphicsDriver graphicsdriver.Graphics) error { @@ -64,19 +62,11 @@ 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. diff --git a/internal/restorable/shader.go b/internal/restorable/shader.go index f356fe629..e46504f1c 100644 --- a/internal/restorable/shader.go +++ b/internal/restorable/shader.go @@ -27,37 +27,26 @@ 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 - LinearFilterShader *Shader - clearShader *Shader + NearestFilterShader *Shader + NearestFilterShaderIR *shaderir.Program + LinearFilterShader *Shader + LinearFilterShaderIR *shaderir.Program + clearShader *Shader ) func init() { @@ -96,7 +85,9 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { if err := wg.Wait(); err != nil { panic(err) } + NearestFilterShaderIR = nearestIR NearestFilterShader = NewShader(nearestIR) + LinearFilterShaderIR = linearIR LinearFilterShader = NewShader(linearIR) clearShader = NewShader(clearIR) }