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
|
|
|
|
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"
|
|
|
|
|
2019-06-25 17:43:09 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/driver"
|
2020-05-17 20:45:58 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/shaderir"
|
2020-08-03 16:21:09 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/shaderir/glsl"
|
2017-12-20 16:24:27 +01:00
|
|
|
)
|
|
|
|
|
2020-05-17 20:45:58 +02:00
|
|
|
type Shader struct {
|
|
|
|
id driver.ShaderID
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-17 20:45:58 +02:00
|
|
|
func NewShader(id driver.ShaderID, graphics *Graphics, program *shaderir.Program) (*Shader, error) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-17 20:45:58 +02:00
|
|
|
func (s *Shader) ID() driver.ShaderID {
|
|
|
|
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 {
|
2020-08-03 16:21:09 +02:00
|
|
|
vssrc, fssrc := glsl.Compile(s.ir)
|
2019-02-12 03:00:18 +01:00
|
|
|
|
2020-05-17 20:45:58 +02:00
|
|
|
vs, err := s.graphics.context.newShader(vertexShader, vssrc)
|
|
|
|
if err != nil {
|
2020-05-24 19:36:45 +02:00
|
|
|
return fmt.Errorf("opengl: vertex shader compile error: %v, source:\n%s", err, vssrc)
|
2020-05-17 20:45:58 +02:00
|
|
|
}
|
|
|
|
defer s.graphics.context.deleteShader(vs)
|
2019-02-17 07:04:59 +01:00
|
|
|
|
2020-05-17 20:45:58 +02:00
|
|
|
fs, err := s.graphics.context.newShader(fragmentShader, fssrc)
|
|
|
|
if err != nil {
|
2020-05-24 19:36:45 +02:00
|
|
|
return fmt.Errorf("opengl: fragment shader compile error: %v, source:\n%s", err, fssrc)
|
2020-05-17 20:45:58 +02:00
|
|
|
}
|
2020-05-23 15:36:50 +02:00
|
|
|
defer s.graphics.context.deleteShader(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
|
|
|
|
2020-05-17 20:45:58 +02:00
|
|
|
s.p = p
|
|
|
|
return nil
|
2014-12-20 10:07:25 +01:00
|
|
|
}
|