2014-12-09 15:16:04 +01:00
|
|
|
/*
|
|
|
|
Copyright 2014 Hajime Hoshi
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2013-10-27 14:58:56 +01:00
|
|
|
package opengl
|
|
|
|
|
|
|
|
import (
|
2014-12-06 07:47:48 +01:00
|
|
|
"github.com/go-gl/gl"
|
2014-12-09 14:09:22 +01:00
|
|
|
"github.com/hajimehoshi/ebiten"
|
2014-12-09 16:25:54 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/opengl/internal/shader"
|
2013-10-27 14:58:56 +01:00
|
|
|
"image"
|
2014-05-03 19:13:43 +02:00
|
|
|
"math"
|
2013-12-10 13:21:11 +01:00
|
|
|
"sync"
|
2013-10-27 14:58:56 +01:00
|
|
|
)
|
|
|
|
|
2014-10-12 07:07:44 +02:00
|
|
|
type ids struct {
|
2014-12-09 14:09:22 +01:00
|
|
|
textures map[ebiten.TextureID]*texture
|
|
|
|
renderTargets map[ebiten.RenderTargetID]*renderTarget
|
|
|
|
renderTargetToTexture map[ebiten.RenderTargetID]ebiten.TextureID
|
2014-10-12 07:07:44 +02:00
|
|
|
lastId int
|
2014-12-09 14:09:22 +01:00
|
|
|
currentRenderTargetId ebiten.RenderTargetID
|
2014-10-12 07:07:44 +02:00
|
|
|
sync.RWMutex
|
|
|
|
}
|
2014-05-02 17:06:20 +02:00
|
|
|
|
2014-10-12 07:07:44 +02:00
|
|
|
var idsInstance = &ids{
|
2014-12-09 14:09:22 +01:00
|
|
|
textures: map[ebiten.TextureID]*texture{},
|
|
|
|
renderTargets: map[ebiten.RenderTargetID]*renderTarget{},
|
|
|
|
renderTargetToTexture: map[ebiten.RenderTargetID]ebiten.TextureID{},
|
2014-10-12 07:07:44 +02:00
|
|
|
currentRenderTargetId: -1,
|
2014-05-02 17:06:20 +02:00
|
|
|
}
|
|
|
|
|
2014-12-09 14:09:22 +01:00
|
|
|
func NewRenderTargetID(width, height int, filter ebiten.Filter) (ebiten.RenderTargetID, error) {
|
2014-12-07 20:22:50 +01:00
|
|
|
return idsInstance.createRenderTarget(width, height, filter)
|
2014-05-02 17:06:20 +02:00
|
|
|
}
|
|
|
|
|
2014-12-09 14:09:22 +01:00
|
|
|
func NewTextureID(img image.Image, filter ebiten.Filter) (ebiten.TextureID, error) {
|
2014-12-07 20:22:50 +01:00
|
|
|
return idsInstance.createTexture(img, filter)
|
2014-05-02 17:06:20 +02:00
|
|
|
}
|
|
|
|
|
2014-12-09 14:09:22 +01:00
|
|
|
func (i *ids) textureAt(id ebiten.TextureID) *texture {
|
2014-05-02 17:06:20 +02:00
|
|
|
i.RLock()
|
|
|
|
defer i.RUnlock()
|
2013-10-27 14:58:56 +01:00
|
|
|
return i.textures[id]
|
|
|
|
}
|
|
|
|
|
2014-12-09 14:09:22 +01:00
|
|
|
func (i *ids) renderTargetAt(id ebiten.RenderTargetID) *renderTarget {
|
2014-05-02 17:06:20 +02:00
|
|
|
i.RLock()
|
|
|
|
defer i.RUnlock()
|
2013-10-27 14:58:56 +01:00
|
|
|
return i.renderTargets[id]
|
|
|
|
}
|
|
|
|
|
2014-12-09 14:09:22 +01:00
|
|
|
func (i *ids) toTexture(id ebiten.RenderTargetID) ebiten.TextureID {
|
2014-05-02 17:06:20 +02:00
|
|
|
i.RLock()
|
|
|
|
defer i.RUnlock()
|
2013-10-27 14:58:56 +01:00
|
|
|
return i.renderTargetToTexture[id]
|
|
|
|
}
|
|
|
|
|
2014-12-09 14:09:22 +01:00
|
|
|
func (i *ids) createTexture(img image.Image, filter ebiten.Filter) (ebiten.TextureID, error) {
|
2014-12-07 20:22:50 +01:00
|
|
|
texture, err := createTextureFromImage(img, filter)
|
2013-10-27 14:58:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2013-12-10 13:21:11 +01:00
|
|
|
|
2014-05-02 17:06:20 +02:00
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
2014-05-11 12:44:36 +02:00
|
|
|
i.lastId++
|
2014-12-09 14:09:22 +01:00
|
|
|
textureId := ebiten.TextureID(i.lastId)
|
2013-10-27 14:58:56 +01:00
|
|
|
i.textures[textureId] = texture
|
|
|
|
return textureId, nil
|
|
|
|
}
|
|
|
|
|
2014-12-09 14:09:22 +01:00
|
|
|
func (i *ids) createRenderTarget(width, height int, filter ebiten.Filter) (ebiten.RenderTargetID, error) {
|
2014-12-07 20:22:50 +01:00
|
|
|
texture, err := createTexture(width, height, filter)
|
2013-10-27 14:58:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2014-12-07 20:22:50 +01:00
|
|
|
framebuffer := createFramebuffer(gl.Texture(texture.native))
|
2014-05-03 19:13:43 +02:00
|
|
|
// The current binded framebuffer can be changed.
|
|
|
|
i.currentRenderTargetId = -1
|
2014-12-07 16:07:36 +01:00
|
|
|
r := &renderTarget{
|
2014-10-12 07:07:44 +02:00
|
|
|
framebuffer: framebuffer,
|
|
|
|
width: texture.width,
|
|
|
|
height: texture.height,
|
|
|
|
}
|
2014-01-08 10:47:38 +01:00
|
|
|
|
2014-05-02 17:06:20 +02:00
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
2014-05-11 12:44:36 +02:00
|
|
|
i.lastId++
|
2014-12-09 14:09:22 +01:00
|
|
|
textureId := ebiten.TextureID(i.lastId)
|
2014-05-11 12:44:36 +02:00
|
|
|
i.lastId++
|
2014-12-09 14:09:22 +01:00
|
|
|
renderTargetId := ebiten.RenderTargetID(i.lastId)
|
2014-05-11 12:44:36 +02:00
|
|
|
|
2013-10-27 14:58:56 +01:00
|
|
|
i.textures[textureId] = texture
|
2014-12-07 16:07:36 +01:00
|
|
|
i.renderTargets[renderTargetId] = r
|
2013-10-27 14:58:56 +01:00
|
|
|
i.renderTargetToTexture[renderTargetId] = textureId
|
|
|
|
|
|
|
|
return renderTargetId, nil
|
|
|
|
}
|
2014-01-07 13:58:46 +01:00
|
|
|
|
2014-01-11 00:59:38 +01:00
|
|
|
// NOTE: renderTarget can't be used as a texture.
|
2014-12-09 14:09:22 +01:00
|
|
|
func (i *ids) addRenderTarget(renderTarget *renderTarget) ebiten.RenderTargetID {
|
2014-05-02 17:06:20 +02:00
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
2014-05-11 12:44:36 +02:00
|
|
|
i.lastId++
|
2014-12-09 14:09:22 +01:00
|
|
|
id := ebiten.RenderTargetID(i.lastId)
|
2014-01-11 00:59:38 +01:00
|
|
|
i.renderTargets[id] = renderTarget
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2014-12-09 14:09:22 +01:00
|
|
|
func (i *ids) deleteRenderTarget(id ebiten.RenderTargetID) {
|
2014-05-02 17:06:20 +02:00
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
2014-01-08 10:58:15 +01:00
|
|
|
|
2014-01-07 13:58:46 +01:00
|
|
|
renderTarget := i.renderTargets[id]
|
2014-01-08 10:58:15 +01:00
|
|
|
textureId := i.renderTargetToTexture[id]
|
|
|
|
texture := i.textures[textureId]
|
|
|
|
|
2014-05-02 17:06:20 +02:00
|
|
|
renderTarget.dispose()
|
|
|
|
texture.dispose()
|
2014-01-08 10:47:38 +01:00
|
|
|
|
2014-01-07 13:58:46 +01:00
|
|
|
delete(i.renderTargets, id)
|
2014-01-08 10:58:15 +01:00
|
|
|
delete(i.renderTargetToTexture, id)
|
|
|
|
delete(i.textures, textureId)
|
2014-01-07 13:58:46 +01:00
|
|
|
}
|
2014-01-09 16:42:42 +01:00
|
|
|
|
2014-12-09 14:09:22 +01:00
|
|
|
func (i *ids) fillRenderTarget(id ebiten.RenderTargetID, r, g, b uint8) {
|
2014-05-03 19:13:43 +02:00
|
|
|
i.setViewportIfNeeded(id)
|
|
|
|
const max = float64(math.MaxUint8)
|
2014-12-06 07:47:48 +01:00
|
|
|
gl.ClearColor(gl.GLclampf(float64(r)/max), gl.GLclampf(float64(g)/max), gl.GLclampf(float64(b)/max), 1)
|
|
|
|
gl.Clear(gl.COLOR_BUFFER_BIT)
|
2014-01-11 00:59:38 +01:00
|
|
|
}
|
|
|
|
|
2014-12-09 14:09:22 +01:00
|
|
|
func (i *ids) drawTexture(target ebiten.RenderTargetID, id ebiten.TextureID, parts []ebiten.TexturePart, geo ebiten.GeometryMatrix, color ebiten.ColorMatrix) {
|
2014-01-09 16:42:42 +01:00
|
|
|
texture := i.textureAt(id)
|
2014-05-03 19:13:43 +02:00
|
|
|
i.setViewportIfNeeded(target)
|
|
|
|
r := i.renderTargetAt(target)
|
|
|
|
projectionMatrix := r.projectionMatrix()
|
2014-12-07 20:22:50 +01:00
|
|
|
shader.DrawTexture(texture.native, texture.width, texture.height, projectionMatrix, parts, geo, color)
|
2014-05-03 19:13:43 +02:00
|
|
|
}
|
|
|
|
|
2014-12-09 14:09:22 +01:00
|
|
|
func (i *ids) setViewportIfNeeded(id ebiten.RenderTargetID) {
|
2014-05-03 19:13:43 +02:00
|
|
|
r := i.renderTargetAt(id)
|
|
|
|
if i.currentRenderTargetId != id {
|
|
|
|
r.setAsViewport()
|
|
|
|
i.currentRenderTargetId = id
|
|
|
|
}
|
2014-01-09 16:42:42 +01:00
|
|
|
}
|