2018-11-04 10:34:36 +01:00
|
|
|
// Copyright 2018 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2023-12-19 17:52:03 +01:00
|
|
|
//go:build !playstation5
|
|
|
|
|
2018-11-04 10:34:36 +01:00
|
|
|
package opengl
|
|
|
|
|
2018-11-04 11:06:13 +01:00
|
|
|
import (
|
2022-03-21 14:23:07 +01:00
|
|
|
"errors"
|
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
2022-02-06 12:41:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2022-11-17 05:33:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl"
|
2018-11-04 11:06:13 +01:00
|
|
|
)
|
|
|
|
|
2018-11-04 10:34:36 +01:00
|
|
|
type Image struct {
|
2022-02-06 12:41:32 +01:00
|
|
|
id graphicsdriver.ImageID
|
2021-07-02 19:41:37 +02:00
|
|
|
graphics *Graphics
|
|
|
|
texture textureNative
|
2021-07-02 12:26:09 +02:00
|
|
|
stencil renderbufferNative
|
2021-07-02 19:41:37 +02:00
|
|
|
framebuffer *framebuffer
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
screen bool
|
2018-11-04 10:34:36 +01:00
|
|
|
}
|
|
|
|
|
2022-11-18 05:15:04 +01:00
|
|
|
// framebuffer is a wrapper of OpenGL's framebuffer.
|
|
|
|
type framebuffer struct {
|
|
|
|
graphics *Graphics
|
|
|
|
native framebufferNative
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
}
|
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
func (i *Image) ID() graphicsdriver.ImageID {
|
2020-05-19 16:48:43 +02:00
|
|
|
return i.id
|
|
|
|
}
|
|
|
|
|
2018-11-04 10:34:36 +01:00
|
|
|
func (i *Image) IsInvalidated() bool {
|
2022-11-17 05:33:32 +01:00
|
|
|
return !i.graphics.context.ctx.IsTexture(uint32(i.texture))
|
2018-11-04 10:34:36 +01:00
|
|
|
}
|
2018-11-04 10:44:41 +01:00
|
|
|
|
2018-11-12 15:44:13 +01:00
|
|
|
func (i *Image) Dispose() {
|
2018-11-04 12:00:13 +01:00
|
|
|
if i.framebuffer != nil {
|
2022-11-18 05:15:04 +01:00
|
|
|
i.graphics.context.deleteFramebuffer(i.framebuffer.native)
|
2018-11-04 10:44:41 +01:00
|
|
|
}
|
2022-11-15 17:07:16 +01:00
|
|
|
if i.texture != 0 {
|
2021-07-02 19:41:37 +02:00
|
|
|
i.graphics.context.deleteTexture(i.texture)
|
2018-11-04 10:44:41 +01:00
|
|
|
}
|
2022-11-15 17:07:16 +01:00
|
|
|
if i.stencil != 0 {
|
2021-07-02 12:26:09 +02:00
|
|
|
i.graphics.context.deleteRenderbuffer(i.stencil)
|
|
|
|
}
|
2018-11-04 11:06:13 +01:00
|
|
|
|
2020-05-19 16:48:43 +02:00
|
|
|
i.graphics.removeImage(i)
|
2018-11-05 19:44:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) setViewport() error {
|
2018-11-04 11:06:13 +01:00
|
|
|
if err := i.ensureFramebuffer(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-04 10:20:08 +02:00
|
|
|
i.graphics.context.setViewport(i.framebuffer)
|
2018-11-04 11:06:13 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-16 16:18:37 +02:00
|
|
|
func (i *Image) ReadPixels(args []graphicsdriver.PixelsArgs) error {
|
2018-11-04 11:06:13 +01:00
|
|
|
if err := i.ensureFramebuffer(); err != nil {
|
2022-02-27 09:41:19 +01:00
|
|
|
return err
|
2018-11-04 11:06:13 +01:00
|
|
|
}
|
2023-08-16 16:18:37 +02:00
|
|
|
for _, arg := range args {
|
|
|
|
if err := i.graphics.context.framebufferPixels(arg.Pixels, i.framebuffer, arg.Region); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-17 04:47:30 +01:00
|
|
|
}
|
2022-02-27 09:41:19 +01:00
|
|
|
return nil
|
2018-11-04 11:06:13 +01:00
|
|
|
}
|
|
|
|
|
2020-07-26 04:25:21 +02:00
|
|
|
func (i *Image) framebufferSize() (int, int) {
|
|
|
|
if i.screen {
|
|
|
|
// The (default) framebuffer size can't be converted to a power of 2.
|
|
|
|
// On browsers, i.width and i.height are used as viewport size and
|
|
|
|
// Edge can't treat a bigger viewport than the drawing area (#71).
|
|
|
|
return i.width, i.height
|
|
|
|
}
|
|
|
|
return graphics.InternalImageSize(i.width), graphics.InternalImageSize(i.height)
|
|
|
|
}
|
|
|
|
|
2018-11-04 11:06:13 +01:00
|
|
|
func (i *Image) ensureFramebuffer() error {
|
2018-11-04 12:00:13 +01:00
|
|
|
if i.framebuffer != nil {
|
2018-11-04 11:06:13 +01:00
|
|
|
return nil
|
|
|
|
}
|
2018-11-17 11:49:30 +01:00
|
|
|
|
2020-07-26 04:25:21 +02:00
|
|
|
w, h := i.framebufferSize()
|
2018-11-17 11:49:30 +01:00
|
|
|
if i.screen {
|
2022-11-18 05:15:04 +01:00
|
|
|
i.framebuffer = i.graphics.context.newScreenFramebuffer(w, h)
|
2018-11-17 11:49:30 +01:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-18 05:15:04 +01:00
|
|
|
f, err := i.graphics.context.newFramebuffer(i.texture, w, h)
|
2018-11-04 11:06:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-04 12:00:13 +01:00
|
|
|
i.framebuffer = f
|
2018-11-04 11:06:13 +01:00
|
|
|
return nil
|
|
|
|
}
|
2018-11-04 11:42:21 +01:00
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
func (i *Image) ensureStencilBuffer() error {
|
2022-11-15 17:07:16 +01:00
|
|
|
if i.stencil != 0 {
|
2021-07-02 12:26:09 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := i.ensureFramebuffer(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := i.graphics.context.newRenderbuffer(i.framebufferSize())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
i.stencil = r
|
|
|
|
|
|
|
|
if err := i.graphics.context.bindStencilBuffer(i.framebuffer.native, i.stencil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-16 16:18:37 +02:00
|
|
|
func (i *Image) WritePixels(args []graphicsdriver.PixelsArgs) error {
|
2018-11-17 18:09:44 +01:00
|
|
|
if i.screen {
|
2022-08-07 20:02:12 +02:00
|
|
|
return errors.New("opengl: WritePixels cannot be called on the screen")
|
2018-11-17 18:09:44 +01:00
|
|
|
}
|
2019-11-21 15:42:46 +01:00
|
|
|
if len(args) == 0 {
|
2022-03-21 14:23:07 +01:00
|
|
|
return nil
|
2019-11-19 18:41:31 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:35:10 +01:00
|
|
|
// glFlush is necessary on Android.
|
|
|
|
// glTexSubImage2D didn't work without this hack at least on Nexus 5x and NuAns NEO [Reloaded] (#211).
|
2020-04-04 10:20:08 +02:00
|
|
|
if i.graphics.drawCalled {
|
2022-11-17 05:33:32 +01:00
|
|
|
i.graphics.context.ctx.Flush()
|
2019-11-13 17:19:57 +01:00
|
|
|
}
|
2020-04-04 10:20:08 +02:00
|
|
|
i.graphics.drawCalled = false
|
2022-11-17 05:33:32 +01:00
|
|
|
|
|
|
|
i.graphics.context.bindTexture(i.texture)
|
|
|
|
for _, a := range args {
|
2023-04-27 17:00:51 +02:00
|
|
|
x := int32(a.Region.Min.X)
|
|
|
|
y := int32(a.Region.Min.Y)
|
|
|
|
width := int32(a.Region.Dx())
|
|
|
|
height := int32(a.Region.Dy())
|
|
|
|
i.graphics.context.ctx.TexSubImage2D(gl.TEXTURE_2D, 0, x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, a.Pixels)
|
2022-11-17 05:33:32 +01:00
|
|
|
}
|
|
|
|
|
2022-03-21 14:23:07 +01:00
|
|
|
return nil
|
2018-11-05 19:44:43 +01:00
|
|
|
}
|