2023-10-29 14:16:50 +01:00
|
|
|
// Copyright 2023 The Ebitengine 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.
|
|
|
|
|
|
|
|
//go:build playstation5
|
|
|
|
|
|
|
|
package playstation5
|
|
|
|
|
2023-10-29 17:56:57 +01:00
|
|
|
// #include "graphics_playstation5.h"
|
|
|
|
import "C"
|
|
|
|
|
2023-10-29 14:16:50 +01:00
|
|
|
import (
|
2023-10-29 17:56:57 +01:00
|
|
|
"fmt"
|
2024-08-04 12:09:35 +02:00
|
|
|
"runtime"
|
2023-10-29 17:56:57 +01:00
|
|
|
|
2023-10-29 14:16:50 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
|
|
|
)
|
|
|
|
|
2023-10-29 17:56:57 +01:00
|
|
|
type playstation5Error struct {
|
2024-01-13 16:40:50 +01:00
|
|
|
name string
|
|
|
|
code int
|
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPlaystation5Error(name string, err C.ebitengine_Error) *playstation5Error {
|
|
|
|
return &playstation5Error{
|
|
|
|
name: name,
|
2024-06-16 20:04:18 +02:00
|
|
|
code: int(err.Code),
|
|
|
|
message: C.GoString(err.Message),
|
2024-01-13 16:40:50 +01:00
|
|
|
}
|
2023-10-29 17:56:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *playstation5Error) Error() string {
|
2024-01-13 16:40:50 +01:00
|
|
|
return fmt.Sprintf("playstation5: error at %s, code: %d, message: %s", e.name, e.code, e.message)
|
2023-10-29 17:56:57 +01:00
|
|
|
}
|
|
|
|
|
2023-10-29 14:16:50 +01:00
|
|
|
type Graphics struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGraphics() (*Graphics, error) {
|
|
|
|
return &Graphics{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphics) Initialize() error {
|
2024-01-21 15:54:02 +01:00
|
|
|
if err := C.ebitengine_InitializeGraphics(); !C.ebitengine_IsErrorNil(&err) {
|
2024-01-13 16:40:50 +01:00
|
|
|
return newPlaystation5Error("(*playstation5.Graphics).Initialize", err)
|
2023-10-29 17:56:57 +01:00
|
|
|
}
|
2023-10-29 14:16:50 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphics) Begin() error {
|
2024-09-23 10:59:28 +02:00
|
|
|
C.ebitengine_Begin()
|
2023-10-29 14:16:50 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphics) End(present bool) error {
|
2024-09-23 10:59:28 +02:00
|
|
|
var cPresent C.int
|
|
|
|
if present {
|
|
|
|
cPresent = 1
|
|
|
|
}
|
|
|
|
C.ebitengine_End(cPresent)
|
2023-10-29 14:16:50 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphics) SetTransparent(transparent bool) {
|
|
|
|
}
|
|
|
|
|
2023-11-03 18:45:16 +01:00
|
|
|
func (g *Graphics) SetVertices(vertices []float32, indices []uint32) error {
|
2024-09-01 12:37:08 +02:00
|
|
|
defer runtime.KeepAlive(vertices)
|
|
|
|
defer runtime.KeepAlive(indices)
|
|
|
|
C.ebitengine_SetVertices((*C.float)(&vertices[0]), C.int(len(vertices)), (*C.uint32_t)(&indices[0]), C.int(len(indices)))
|
2023-10-29 14:16:50 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphics) NewImage(width, height int) (graphicsdriver.Image, error) {
|
2023-10-29 17:56:57 +01:00
|
|
|
var id C.int
|
2024-01-21 15:54:02 +01:00
|
|
|
if err := C.ebitengine_NewImage(&id, C.int(width), C.int(height)); !C.ebitengine_IsErrorNil(&err) {
|
2024-01-13 16:40:50 +01:00
|
|
|
return nil, newPlaystation5Error("(*playstation5.Graphics).NewImage", err)
|
2023-10-29 17:56:57 +01:00
|
|
|
}
|
|
|
|
return &Image{
|
|
|
|
id: graphicsdriver.ImageID(id),
|
|
|
|
}, nil
|
2023-10-29 14:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphics) NewScreenFramebufferImage(width, height int) (graphicsdriver.Image, error) {
|
2023-10-29 17:56:57 +01:00
|
|
|
var id C.int
|
2024-01-21 15:54:02 +01:00
|
|
|
if err := C.ebitengine_NewScreenFramebufferImage(&id, C.int(width), C.int(height)); !C.ebitengine_IsErrorNil(&err) {
|
2024-01-13 16:40:50 +01:00
|
|
|
return nil, newPlaystation5Error("(*playstation5.Graphics).NewScreenFramebufferImage", err)
|
2023-10-29 17:56:57 +01:00
|
|
|
}
|
|
|
|
return &Image{
|
|
|
|
id: graphicsdriver.ImageID(id),
|
|
|
|
}, nil
|
2023-10-29 14:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphics) SetVsyncEnabled(enabled bool) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphics) NeedsClearingScreen() bool {
|
2024-08-17 16:05:22 +02:00
|
|
|
return true
|
2023-10-29 14:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphics) MaxImageSize() int {
|
2023-10-29 17:56:57 +01:00
|
|
|
return 4096 // TODO: Get the value from the SDK.
|
2023-10-29 14:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphics) NewShader(program *shaderir.Program) (graphicsdriver.Shader, error) {
|
2023-10-29 17:56:57 +01:00
|
|
|
var id C.int
|
|
|
|
// TODO: Give a source code.
|
2024-01-21 15:54:02 +01:00
|
|
|
if err := C.ebitengine_NewShader(&id, nil); !C.ebitengine_IsErrorNil(&err) {
|
2024-01-13 16:40:50 +01:00
|
|
|
return nil, newPlaystation5Error("(*playstation5.Graphics).NewShader", err)
|
2023-10-29 17:56:57 +01:00
|
|
|
}
|
|
|
|
return &Shader{
|
|
|
|
id: graphicsdriver.ShaderID(id),
|
|
|
|
}, nil
|
2023-10-29 14:16:50 +01:00
|
|
|
}
|
|
|
|
|
2024-07-16 15:43:49 +02:00
|
|
|
func (g *Graphics) DrawTriangles(dst graphicsdriver.ImageID, srcs [graphics.ShaderSrcImageCount]graphicsdriver.ImageID, shader graphicsdriver.ShaderID, dstRegions []graphicsdriver.DstRegion, indexOffset int, blend graphicsdriver.Blend, uniforms []uint32, fillRule graphicsdriver.FillRule) error {
|
2024-08-10 11:18:09 +02:00
|
|
|
cSrcs := make([]C.int, len(srcs))
|
2024-08-04 12:09:35 +02:00
|
|
|
for i, src := range srcs {
|
2024-08-10 11:18:09 +02:00
|
|
|
cSrcs[i] = C.int(src)
|
2024-08-04 12:09:35 +02:00
|
|
|
}
|
2024-08-10 11:18:09 +02:00
|
|
|
defer runtime.KeepAlive(cSrcs)
|
|
|
|
|
2024-08-04 12:09:35 +02:00
|
|
|
cDstRegions := make([]C.ebitengine_DstRegion, len(dstRegions))
|
2024-08-10 11:18:09 +02:00
|
|
|
defer runtime.KeepAlive(cDstRegions)
|
2024-08-04 12:09:35 +02:00
|
|
|
for i, r := range dstRegions {
|
|
|
|
cDstRegions[i] = C.ebitengine_DstRegion{
|
2024-08-10 11:18:09 +02:00
|
|
|
MinX: C.int(r.Region.Min.X),
|
|
|
|
MinY: C.int(r.Region.Min.Y),
|
|
|
|
MaxX: C.int(r.Region.Max.X),
|
|
|
|
MaxY: C.int(r.Region.Max.Y),
|
2024-08-04 12:09:35 +02:00
|
|
|
IndexCount: C.int(r.IndexCount),
|
|
|
|
}
|
|
|
|
}
|
2024-08-10 11:18:09 +02:00
|
|
|
|
2024-08-04 12:09:35 +02:00
|
|
|
cBlend := C.ebitengine_Blend{
|
|
|
|
BlendFactorSourceRGB: C.uint8_t(blend.BlendFactorSourceRGB),
|
|
|
|
BlendFactorSourceAlpha: C.uint8_t(blend.BlendFactorSourceAlpha),
|
|
|
|
BlendFactorDestinationRGB: C.uint8_t(blend.BlendFactorDestinationRGB),
|
|
|
|
BlendFactorDestinationAlpha: C.uint8_t(blend.BlendFactorDestinationAlpha),
|
|
|
|
BlendOperationRGB: C.uint8_t(blend.BlendOperationRGB),
|
|
|
|
BlendOperationAlpha: C.uint8_t(blend.BlendOperationAlpha),
|
|
|
|
}
|
|
|
|
|
2024-08-10 11:18:09 +02:00
|
|
|
cUniforms := make([]C.uint32_t, len(uniforms))
|
|
|
|
defer runtime.KeepAlive(cUniforms)
|
|
|
|
for i, u := range uniforms {
|
|
|
|
cUniforms[i] = C.uint32_t(u)
|
2024-08-04 12:09:35 +02:00
|
|
|
}
|
2024-08-10 11:18:09 +02:00
|
|
|
|
|
|
|
if err := C.ebitengine_DrawTriangles(C.int(dst), &cSrcs[0], C.int(len(cSrcs)), C.int(shader), &cDstRegions[0], C.int(len(cDstRegions)), C.int(indexOffset), cBlend, &cUniforms[0], C.int(len(cUniforms)), C.int(fillRule)); !C.ebitengine_IsErrorNil(&err) {
|
2024-08-04 12:09:35 +02:00
|
|
|
return newPlaystation5Error("(*playstation5.Graphics).DrawTriangles", err)
|
|
|
|
}
|
2023-10-29 14:16:50 +01:00
|
|
|
return nil
|
|
|
|
}
|
2023-10-29 17:56:57 +01:00
|
|
|
|
|
|
|
type Image struct {
|
|
|
|
id graphicsdriver.ImageID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) ID() graphicsdriver.ImageID {
|
|
|
|
return i.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) Dispose() {
|
|
|
|
C.ebitengine_DisposeImage(C.int(i.id))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) ReadPixels(args []graphicsdriver.PixelsArgs) error {
|
|
|
|
// TODO: Implement this
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) WritePixels(args []graphicsdriver.PixelsArgs) error {
|
|
|
|
// TODO: Implement this
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Shader struct {
|
|
|
|
id graphicsdriver.ShaderID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Shader) ID() graphicsdriver.ShaderID {
|
|
|
|
return s.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Shader) Dispose() {
|
|
|
|
C.ebitengine_DisposeShader(C.int(s.id))
|
|
|
|
}
|