mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +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 size = 10000
|
||||||
const uint16Size = 2
|
const uint16Size = 2
|
||||||
|
|
||||||
var err error
|
shaderVertexNative, err := c.NewShader(c.VertexShader, shader(c, shaderVertex))
|
||||||
shaders[shaderVertex].native, err = c.NewShader(c.VertexShader, shaders[shaderVertex].source)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer c.DeleteShader(shaders[shaderColorMatrix].native)
|
defer c.DeleteShader(shaderColorMatrixNative)
|
||||||
|
|
||||||
shaders := []opengl.Shader{
|
shaders := []opengl.Shader{
|
||||||
shaders[shaderVertex].native,
|
shaderVertexNative,
|
||||||
shaders[shaderColorMatrix].native,
|
shaderColorMatrixNative,
|
||||||
}
|
}
|
||||||
programColorMatrix, err = c.NewProgram(shaders)
|
programColorMatrix, err = c.NewProgram(shaders)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -16,13 +16,9 @@ package shader
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hajimehoshi/ebiten/internal/opengl"
|
"github.com/hajimehoshi/ebiten/internal/opengl"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type shader struct {
|
|
||||||
native opengl.Shader
|
|
||||||
source string
|
|
||||||
}
|
|
||||||
|
|
||||||
type shaderId int
|
type shaderId int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -30,30 +26,37 @@ const (
|
|||||||
shaderColorMatrix
|
shaderColorMatrix
|
||||||
)
|
)
|
||||||
|
|
||||||
var shaders = map[shaderId]*shader{
|
func shader(c *opengl.Context, id shaderId) string {
|
||||||
shaderVertex: {
|
str := shaders[id]
|
||||||
source: `
|
if !c.GlslHighpSupported() {
|
||||||
uniform mat4 projection_matrix;
|
str = strings.Replace(str, "highp ", "", -1)
|
||||||
uniform mat4 modelview_matrix;
|
str = strings.Replace(str, "lowp ", "", -1)
|
||||||
attribute vec2 vertex;
|
}
|
||||||
attribute vec2 tex_coord;
|
return str
|
||||||
varying vec2 vertex_out_tex_coord;
|
}
|
||||||
|
|
||||||
|
// 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) {
|
void main(void) {
|
||||||
vertex_out_tex_coord = tex_coord;
|
vertex_out_tex_coord = tex_coord;
|
||||||
gl_Position = projection_matrix * modelview_matrix * vec4(vertex, 0, 1);
|
gl_Position = projection_matrix * modelview_matrix * vec4(vertex, 0, 1);
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
},
|
shaderColorMatrix: `
|
||||||
shaderColorMatrix: {
|
uniform lowp sampler2D texture;
|
||||||
source: `
|
uniform lowp mat4 color_matrix;
|
||||||
uniform sampler2D texture;
|
uniform lowp vec4 color_matrix_translation;
|
||||||
uniform mat4 color_matrix;
|
varying highp vec2 vertex_out_tex_coord;
|
||||||
uniform vec4 color_matrix_translation;
|
|
||||||
varying vec2 vertex_out_tex_coord;
|
|
||||||
|
|
||||||
void main(void) {
|
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)) {
|
if (color_matrix != mat4(1.0) || color_matrix_translation != vec4(0.0)) {
|
||||||
// Un-premultiply alpha
|
// Un-premultiply alpha
|
||||||
@ -68,5 +71,4 @@ void main(void) {
|
|||||||
gl_FragColor = color;
|
gl_FragColor = color;
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
@ -58,7 +58,6 @@ func (c *Context) NewTexture(width, height int, pixels []uint8, filter FilterTyp
|
|||||||
}
|
}
|
||||||
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
|
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
|
||||||
t.Bind(gl.TEXTURE_2D)
|
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_MAG_FILTER, int(filter))
|
||||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_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()
|
gl.Shader(s).Delete()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Context) GlslHighpSupported() bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Context) NewProgram(shaders []Shader) (Program, error) {
|
func (c *Context) NewProgram(shaders []Shader) (Program, error) {
|
||||||
p := gl.CreateProgram()
|
p := gl.CreateProgram()
|
||||||
if p == 0 {
|
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.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
|
||||||
gl.BindTexture(gl.TEXTURE_2D, t)
|
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_MAG_FILTER, int(filter))
|
||||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_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,
|
// void texImage2D(GLenum target, GLint level, GLenum internalformat,
|
||||||
// GLsizei width, GLsizei height, GLint border, GLenum format,
|
// GLsizei width, GLsizei height, GLint border, GLenum format,
|
||||||
// GLenum type, ArrayBufferView? pixels);
|
// 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
|
return Texture(t), nil
|
||||||
}
|
}
|
||||||
@ -99,6 +102,11 @@ func (c *Context) DeleteTexture(t Texture) {
|
|||||||
gl.DeleteTexture(t)
|
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) {
|
func (c *Context) NewFramebuffer(texture Texture) (Framebuffer, error) {
|
||||||
gl := c.gl
|
gl := c.gl
|
||||||
f := gl.CreateFramebuffer()
|
f := gl.CreateFramebuffer()
|
||||||
|
Loading…
Reference in New Issue
Block a user