Remove render targets from ids

This commit is contained in:
Hajime Hoshi 2014-12-17 22:24:30 +09:00
parent f8c6958a4a
commit 7c56ac268e
2 changed files with 12 additions and 47 deletions

View File

@ -79,7 +79,8 @@ type Texture struct {
// A render target is essentially same as a texture, but it is assumed that the // A render target is essentially same as a texture, but it is assumed that the
// all alpha values of a render target is maximum. // all alpha values of a render target is maximum.
type RenderTarget struct { type RenderTarget struct {
id int glRenderTarget *opengl.RenderTarget
texture *Texture
} }
func u(x int, width int) float32 { func u(x int, width int) float32 {

54
ids.go
View File

@ -21,32 +21,16 @@ import (
"github.com/hajimehoshi/ebiten/internal/opengl" "github.com/hajimehoshi/ebiten/internal/opengl"
"github.com/hajimehoshi/ebiten/internal/opengl/internal/shader" "github.com/hajimehoshi/ebiten/internal/opengl/internal/shader"
"math" "math"
"sync"
) )
type ids struct { type ids struct {
renderTargets map[*RenderTarget]*opengl.RenderTarget
renderTargetToTexture map[*RenderTarget]*Texture
lastID int
currentRenderTarget *RenderTarget currentRenderTarget *RenderTarget
sync.RWMutex
} }
var idsInstance = &ids{ var idsInstance = &ids{}
renderTargets: map[*RenderTarget]*opengl.RenderTarget{},
renderTargetToTexture: map[*RenderTarget]*Texture{},
}
func (i *ids) renderTargetAt(renderTarget *RenderTarget) *opengl.RenderTarget {
i.RLock()
defer i.RUnlock()
return i.renderTargets[renderTarget]
}
func (i *ids) toTexture(renderTarget *RenderTarget) *Texture { func (i *ids) toTexture(renderTarget *RenderTarget) *Texture {
i.RLock() return renderTarget.texture
defer i.RUnlock()
return i.renderTargetToTexture[renderTarget]
} }
func (i *ids) createRenderTarget(width, height int, filter int) (*RenderTarget, error) { func (i *ids) createRenderTarget(width, height int, filter int) (*RenderTarget, error) {
@ -57,48 +41,30 @@ func (i *ids) createRenderTarget(width, height int, filter int) (*RenderTarget,
// The current binded framebuffer can be changed. // The current binded framebuffer can be changed.
i.currentRenderTarget = nil i.currentRenderTarget = nil
r, err := opengl.NewRenderTargetFromTexture(glTexture) glRenderTarget, err := opengl.NewRenderTargetFromTexture(glTexture)
if err != nil { if err != nil {
return nil, err return nil, err
} }
i.Lock()
defer i.Unlock()
texture := &Texture{glTexture} texture := &Texture{glTexture}
i.lastID++ renderTarget := &RenderTarget{glRenderTarget, texture}
renderTarget := &RenderTarget{i.lastID}
//i.textures[texture] = glTexture
i.renderTargets[renderTarget] = r
i.renderTargetToTexture[renderTarget] = texture
return renderTarget, nil return renderTarget, nil
} }
// NOTE: renderTarget can't be used as a texture. // NOTE: renderTarget can't be used as a texture.
func (i *ids) addRenderTarget(glRenderTarget *opengl.RenderTarget) *RenderTarget { func (i *ids) addRenderTarget(glRenderTarget *opengl.RenderTarget) *RenderTarget {
i.Lock() return &RenderTarget{glRenderTarget, nil}
defer i.Unlock()
i.lastID++
renderTarget := &RenderTarget{i.lastID}
i.renderTargets[renderTarget] = glRenderTarget
return renderTarget
} }
func (i *ids) deleteRenderTarget(renderTarget *RenderTarget) { func (i *ids) deleteRenderTarget(renderTarget *RenderTarget) {
i.Lock()
defer i.Unlock()
glRenderTarget := i.renderTargets[renderTarget] glRenderTarget := renderTarget.glRenderTarget
texture := i.renderTargetToTexture[renderTarget] texture := renderTarget.texture
glTexture := texture.glTexture glTexture := texture.glTexture
glRenderTarget.Dispose() glRenderTarget.Dispose()
glTexture.Dispose() glTexture.Dispose()
delete(i.renderTargets, renderTarget)
delete(i.renderTargetToTexture, renderTarget)
} }
func (i *ids) fillRenderTarget(renderTarget *RenderTarget, r, g, b uint8) error { func (i *ids) fillRenderTarget(renderTarget *RenderTarget, r, g, b uint8) error {
@ -116,17 +82,15 @@ func (i *ids) drawTexture(target *RenderTarget, texture *Texture, parts []Textur
if err := i.setViewportIfNeeded(target); err != nil { if err := i.setViewportIfNeeded(target); err != nil {
return err return err
} }
r := i.renderTargetAt(target) projectionMatrix := target.glRenderTarget.ProjectionMatrix()
projectionMatrix := r.ProjectionMatrix()
quads := textureQuads(parts, glTexture.Width(), glTexture.Height()) quads := textureQuads(parts, glTexture.Width(), glTexture.Height())
shader.DrawTexture(glTexture.Native(), projectionMatrix, quads, &geo, &color) shader.DrawTexture(glTexture.Native(), projectionMatrix, quads, &geo, &color)
return nil return nil
} }
func (i *ids) setViewportIfNeeded(renderTarget *RenderTarget) error { func (i *ids) setViewportIfNeeded(renderTarget *RenderTarget) error {
r := i.renderTargetAt(renderTarget)
if i.currentRenderTarget != renderTarget { if i.currentRenderTarget != renderTarget {
if err := r.SetAsViewport(); err != nil { if err := renderTarget.glRenderTarget.SetAsViewport(); err != nil {
return err return err
} }
i.currentRenderTarget = renderTarget i.currentRenderTarget = renderTarget