2020-05-17 20:45:58 +02:00
|
|
|
// Copyright 2020 The Ebiten Authors
|
2014-12-24 03:04:10 +01:00
|
|
|
//
|
|
|
|
// 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.
|
2014-12-09 15:16:04 +01:00
|
|
|
|
2023-12-19 17:52:03 +01:00
|
|
|
//go:build !playstation5
|
|
|
|
|
2018-10-29 17:27:31 +01:00
|
|
|
package opengl
|
2013-06-15 10:07:14 +02:00
|
|
|
|
2017-12-20 16:24:27 +01:00
|
|
|
import (
|
2020-05-24 16:15:00 +02:00
|
|
|
"fmt"
|
|
|
|
|
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"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir/glsl"
|
2017-12-20 16:24:27 +01:00
|
|
|
)
|
|
|
|
|
2020-05-17 20:45:58 +02:00
|
|
|
type Shader struct {
|
2022-02-06 12:41:32 +01:00
|
|
|
id graphicsdriver.ShaderID
|
2020-05-17 20:45:58 +02:00
|
|
|
graphics *Graphics
|
2019-01-07 16:07:47 +01:00
|
|
|
|
2020-05-17 20:45:58 +02:00
|
|
|
ir *shaderir.Program
|
|
|
|
p program
|
2019-02-12 03:00:18 +01:00
|
|
|
}
|
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
func newShader(id graphicsdriver.ShaderID, graphics *Graphics, program *shaderir.Program) (*Shader, error) {
|
2020-05-17 20:45:58 +02:00
|
|
|
s := &Shader{
|
|
|
|
id: id,
|
|
|
|
graphics: graphics,
|
|
|
|
ir: program,
|
2017-12-20 16:24:27 +01:00
|
|
|
}
|
2020-05-17 20:45:58 +02:00
|
|
|
if err := s.compile(); err != nil {
|
|
|
|
return nil, err
|
2019-02-12 03:00:18 +01:00
|
|
|
}
|
2020-05-17 20:45:58 +02:00
|
|
|
return s, nil
|
2015-01-02 15:30:22 +01:00
|
|
|
}
|
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
func (s *Shader) ID() graphicsdriver.ShaderID {
|
2020-05-17 20:45:58 +02:00
|
|
|
return s.id
|
2014-12-20 10:07:25 +01:00
|
|
|
}
|
2018-02-22 03:46:46 +01:00
|
|
|
|
2020-05-17 20:45:58 +02:00
|
|
|
func (s *Shader) Dispose() {
|
|
|
|
s.graphics.context.deleteProgram(s.p)
|
|
|
|
s.graphics.removeShader(s)
|
2018-12-23 19:00:00 +01:00
|
|
|
}
|
|
|
|
|
2020-05-17 20:45:58 +02:00
|
|
|
func (s *Shader) compile() error {
|
2021-08-01 14:50:21 +02:00
|
|
|
vssrc, fssrc := glsl.Compile(s.ir, s.graphics.context.glslVersion())
|
2019-02-12 03:00:18 +01:00
|
|
|
|
2022-11-17 05:33:32 +01:00
|
|
|
vs, err := s.graphics.context.newShader(gl.VERTEX_SHADER, vssrc)
|
2020-05-17 20:45:58 +02:00
|
|
|
if err != nil {
|
2024-07-04 14:46:45 +02:00
|
|
|
return err
|
2020-05-17 20:45:58 +02:00
|
|
|
}
|
2022-11-17 05:33:32 +01:00
|
|
|
defer s.graphics.context.ctx.DeleteShader(uint32(vs))
|
2019-02-17 07:04:59 +01:00
|
|
|
|
2022-11-17 05:33:32 +01:00
|
|
|
fs, err := s.graphics.context.newShader(gl.FRAGMENT_SHADER, fssrc)
|
2020-05-17 20:45:58 +02:00
|
|
|
if err != nil {
|
2024-07-04 14:46:45 +02:00
|
|
|
return err
|
2020-05-17 20:45:58 +02:00
|
|
|
}
|
2022-11-17 05:33:32 +01:00
|
|
|
defer s.graphics.context.ctx.DeleteShader(uint32(fs))
|
2019-02-17 11:01:43 +01:00
|
|
|
|
2020-05-17 20:45:58 +02:00
|
|
|
p, err := s.graphics.context.newProgram([]shader{vs, fs}, theArrayBufferLayout.names())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-17 11:01:43 +01:00
|
|
|
|
2024-07-04 14:46:45 +02:00
|
|
|
// Check errors only after linking fails.
|
|
|
|
// See https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/WebGL_best_practices#dont_check_shader_compile_status_unless_linking_fails
|
|
|
|
if s.graphics.context.ctx.GetProgrami(uint32(p), gl.LINK_STATUS) == gl.FALSE {
|
|
|
|
programInfo := s.graphics.context.ctx.GetProgramInfoLog(uint32(p))
|
|
|
|
vertexShaderInfo := s.graphics.context.ctx.GetShaderInfoLog(uint32(vs))
|
|
|
|
fragmentShaderInfo := s.graphics.context.ctx.GetShaderInfoLog(uint32(fs))
|
|
|
|
return fmt.Errorf("opengl: program error: %s\nvertex shader error: %s\nvertex shader source: %s\nfragment shader error: %s\nfragment shader source: %s",
|
|
|
|
programInfo, vertexShaderInfo, vssrc, fragmentShaderInfo, fssrc)
|
|
|
|
}
|
|
|
|
|
2020-05-17 20:45:58 +02:00
|
|
|
s.p = p
|
|
|
|
return nil
|
2014-12-20 10:07:25 +01:00
|
|
|
}
|