mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 10:48:53 +01:00
Fix shaders for WebGL
This commit is contained in:
parent
bd4434c62c
commit
24c39bcd18
@ -24,22 +24,21 @@ func initialize(c *opengl.Context) error {
|
||||
const size = 10000
|
||||
const uint16Size = 2
|
||||
|
||||
var err error
|
||||
shaders[shaderVertex].native, err = c.NewShader(c.VertexShader, shaders[shaderVertex].source)
|
||||
shaderVertexNative, err := c.NewShader(c.VertexShader, shader(c, shaderVertex))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer c.DeleteShader(shaders[shaderVertex].native)
|
||||
defer c.DeleteShader(shaderVertexNative)
|
||||
|
||||
shaders[shaderColorMatrix].native, err = c.NewShader(c.FragmentShader, shaders[shaderColorMatrix].source)
|
||||
shaderColorMatrixNative, err := c.NewShader(c.FragmentShader, shader(c, shaderColorMatrix))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer c.DeleteShader(shaders[shaderColorMatrix].native)
|
||||
defer c.DeleteShader(shaderColorMatrixNative)
|
||||
|
||||
shaders := []opengl.Shader{
|
||||
shaders[shaderVertex].native,
|
||||
shaders[shaderColorMatrix].native,
|
||||
shaderVertexNative,
|
||||
shaderColorMatrixNative,
|
||||
}
|
||||
programColorMatrix, err = c.NewProgram(shaders)
|
||||
if err != nil {
|
||||
|
@ -16,13 +16,9 @@ package shader
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten/internal/opengl"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type shader struct {
|
||||
native opengl.Shader
|
||||
source string
|
||||
}
|
||||
|
||||
type shaderId int
|
||||
|
||||
const (
|
||||
@ -30,30 +26,37 @@ const (
|
||||
shaderColorMatrix
|
||||
)
|
||||
|
||||
var shaders = map[shaderId]*shader{
|
||||
shaderVertex: {
|
||||
source: `
|
||||
uniform mat4 projection_matrix;
|
||||
uniform mat4 modelview_matrix;
|
||||
attribute vec2 vertex;
|
||||
attribute vec2 tex_coord;
|
||||
varying vec2 vertex_out_tex_coord;
|
||||
func shader(c *opengl.Context, id shaderId) string {
|
||||
str := shaders[id]
|
||||
if !c.GlslHighpSupported() {
|
||||
str = strings.Replace(str, "highp ", "", -1)
|
||||
str = strings.Replace(str, "lowp ", "", -1)
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
// TODO: Create version 100 for WebGL
|
||||
var shaders = map[shaderId]string{
|
||||
shaderVertex: `
|
||||
uniform highp mat4 projection_matrix;
|
||||
uniform highp mat4 modelview_matrix;
|
||||
attribute highp vec2 vertex;
|
||||
attribute highp vec2 tex_coord;
|
||||
varying highp vec2 vertex_out_tex_coord;
|
||||
|
||||
void main(void) {
|
||||
vertex_out_tex_coord = tex_coord;
|
||||
gl_Position = projection_matrix * modelview_matrix * vec4(vertex, 0, 1);
|
||||
}
|
||||
`,
|
||||
},
|
||||
shaderColorMatrix: {
|
||||
source: `
|
||||
uniform sampler2D texture;
|
||||
uniform mat4 color_matrix;
|
||||
uniform vec4 color_matrix_translation;
|
||||
varying vec2 vertex_out_tex_coord;
|
||||
shaderColorMatrix: `
|
||||
uniform lowp sampler2D texture;
|
||||
uniform lowp mat4 color_matrix;
|
||||
uniform lowp vec4 color_matrix_translation;
|
||||
varying highp vec2 vertex_out_tex_coord;
|
||||
|
||||
void main(void) {
|
||||
vec4 color = texture2D(texture, vertex_out_tex_coord);
|
||||
lowp vec4 color = texture2D(texture, vertex_out_tex_coord);
|
||||
|
||||
if (color_matrix != mat4(1.0) || color_matrix_translation != vec4(0.0)) {
|
||||
// Un-premultiply alpha
|
||||
@ -68,5 +71,4 @@ void main(void) {
|
||||
gl_FragColor = color;
|
||||
}
|
||||
`,
|
||||
},
|
||||
}
|
||||
|
@ -58,7 +58,6 @@ func (c *Context) NewTexture(width, height int, pixels []uint8, filter FilterTyp
|
||||
}
|
||||
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
|
||||
t.Bind(gl.TEXTURE_2D)
|
||||
defer gl.Texture(0).Bind(gl.TEXTURE_2D)
|
||||
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, int(filter))
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, int(filter))
|
||||
@ -148,6 +147,10 @@ func (c *Context) DeleteShader(s Shader) {
|
||||
gl.Shader(s).Delete()
|
||||
}
|
||||
|
||||
func (c *Context) GlslHighpSupported() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *Context) NewProgram(shaders []Shader) (Program, error) {
|
||||
p := gl.CreateProgram()
|
||||
if p == 0 {
|
||||
|
@ -63,7 +63,6 @@ func (c *Context) NewTexture(width, height int, pixels []uint8, filter FilterTyp
|
||||
}
|
||||
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
|
||||
gl.BindTexture(gl.TEXTURE_2D, t)
|
||||
defer gl.BindTexture(gl.TEXTURE_2D, nil)
|
||||
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, int(filter))
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, int(filter))
|
||||
@ -71,7 +70,11 @@ func (c *Context) NewTexture(width, height int, pixels []uint8, filter FilterTyp
|
||||
// void texImage2D(GLenum target, GLint level, GLenum internalformat,
|
||||
// GLsizei width, GLsizei height, GLint border, GLenum format,
|
||||
// GLenum type, ArrayBufferView? pixels);
|
||||
gl.Call("texImage2D", gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
|
||||
var p interface{}
|
||||
if pixels != nil {
|
||||
p = pixels
|
||||
}
|
||||
gl.Call("texImage2D", gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, p)
|
||||
|
||||
return Texture(t), nil
|
||||
}
|
||||
@ -99,6 +102,11 @@ func (c *Context) DeleteTexture(t Texture) {
|
||||
gl.DeleteTexture(t)
|
||||
}
|
||||
|
||||
func (c *Context) GlslHighpSupported() bool {
|
||||
gl := c.gl
|
||||
return gl.Call("getShaderPrecisionFormat", gl.FRAGMENT_SHADER, gl.HIGH_FLOAT).Get("precision").Int() != 0
|
||||
}
|
||||
|
||||
func (c *Context) NewFramebuffer(texture Texture) (Framebuffer, error) {
|
||||
gl := c.gl
|
||||
f := gl.CreateFramebuffer()
|
||||
|
Loading…
Reference in New Issue
Block a user