2022-04-03 19:15:33 +02:00
|
|
|
// Copyright 2022 The Ebiten 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.
|
|
|
|
|
|
|
|
package graphics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shader"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
|
|
|
)
|
|
|
|
|
2023-04-16 11:56:14 +02:00
|
|
|
func shaderSuffix(unit shaderir.Unit) (string, error) {
|
|
|
|
shaderSuffix := `
|
2022-04-03 19:15:33 +02:00
|
|
|
var __imageDstTextureSize vec2
|
|
|
|
|
|
|
|
// imageSrcTextureSize returns the destination image's texture size in pixels.
|
|
|
|
func imageDstTextureSize() vec2 {
|
|
|
|
return __imageDstTextureSize
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
shaderSuffix += fmt.Sprintf(`
|
2023-08-25 01:01:32 +02:00
|
|
|
var __imageSrcTextureSizes [%[1]d]vec2
|
2022-04-03 19:15:33 +02:00
|
|
|
|
|
|
|
// imageSrcTextureSize returns the source image's texture size in pixels.
|
|
|
|
// As an image is a part of internal texture, the texture is usually bigger than the image.
|
2023-04-16 11:56:14 +02:00
|
|
|
// The texture's size is useful when you want to calculate pixels from texels in the texel mode.
|
2022-04-03 19:15:33 +02:00
|
|
|
func imageSrcTextureSize() vec2 {
|
2023-08-25 01:01:32 +02:00
|
|
|
return __imageSrcTextureSizes[0]
|
2022-04-03 19:15:33 +02:00
|
|
|
}
|
|
|
|
|
2023-04-16 11:56:14 +02:00
|
|
|
// The unit is the source texture's pixel or texel.
|
2023-08-23 18:46:11 +02:00
|
|
|
var __imageDstRegionOrigin vec2
|
2022-04-03 19:15:33 +02:00
|
|
|
|
2023-04-16 11:56:14 +02:00
|
|
|
// The unit is the source texture's pixel or texel.
|
2023-08-23 18:46:11 +02:00
|
|
|
var __imageDstRegionSize vec2
|
2022-04-03 19:15:33 +02:00
|
|
|
|
|
|
|
// imageDstRegionOnTexture returns the destination image's region (the origin and the size) on its texture.
|
2023-04-16 11:56:14 +02:00
|
|
|
// The unit is the source texture's pixel or texel.
|
2022-04-03 19:15:33 +02:00
|
|
|
//
|
|
|
|
// As an image is a part of internal texture, the image can be located at an arbitrary position on the texture.
|
|
|
|
func imageDstRegionOnTexture() (vec2, vec2) {
|
2023-08-23 18:46:11 +02:00
|
|
|
return __imageDstRegionOrigin, __imageDstRegionSize
|
2022-04-03 19:15:33 +02:00
|
|
|
}
|
|
|
|
|
2023-04-16 11:56:14 +02:00
|
|
|
// The unit is the source texture's pixel or texel.
|
2023-08-25 01:01:32 +02:00
|
|
|
var __imageSrcRegionOrigins [%[1]d]vec2
|
2022-04-03 19:15:33 +02:00
|
|
|
|
2023-04-16 11:56:14 +02:00
|
|
|
// The unit is the source texture's pixel or texel.
|
2023-08-25 00:30:17 +02:00
|
|
|
var __imageSrcRegionSizes [%[1]d]vec2
|
2022-04-03 19:15:33 +02:00
|
|
|
|
|
|
|
// imageSrcRegionOnTexture returns the source image's region (the origin and the size) on its texture.
|
2023-04-16 11:56:14 +02:00
|
|
|
// The unit is the source texture's pixel or texel.
|
2022-04-03 19:15:33 +02:00
|
|
|
//
|
|
|
|
// As an image is a part of internal texture, the image can be located at an arbitrary position on the texture.
|
|
|
|
func imageSrcRegionOnTexture() (vec2, vec2) {
|
2023-08-25 01:01:32 +02:00
|
|
|
return __imageSrcRegionOrigins[0], __imageSrcRegionSizes[0]
|
2022-04-03 19:15:33 +02:00
|
|
|
}
|
2023-08-25 01:01:32 +02:00
|
|
|
`, ShaderImageCount)
|
2022-04-03 19:15:33 +02:00
|
|
|
|
2022-07-12 18:46:02 +02:00
|
|
|
for i := 0; i < ShaderImageCount; i++ {
|
2022-04-03 19:15:33 +02:00
|
|
|
pos := "pos"
|
|
|
|
if i >= 1 {
|
2023-04-16 11:56:14 +02:00
|
|
|
// Convert the position in texture0's positions to the target texture positions.
|
|
|
|
switch unit {
|
2023-08-01 04:41:27 +02:00
|
|
|
case shaderir.Pixels:
|
2023-08-25 01:01:32 +02:00
|
|
|
pos = fmt.Sprintf("pos - __imageSrcRegionOrigins[0] + __imageSrcRegionOrigins[%d]", i)
|
2023-08-01 04:41:27 +02:00
|
|
|
case shaderir.Texels:
|
2023-08-25 01:01:32 +02:00
|
|
|
pos = fmt.Sprintf("((pos - __imageSrcRegionOrigins[0]) * __imageSrcTextureSizes[0]) / __imageSrcTextureSizes[%[1]d] + __imageSrcRegionOrigins[%[1]d]", i)
|
2023-04-16 11:56:14 +02:00
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("graphics: unexpected unit: %d", unit)
|
|
|
|
}
|
2022-04-03 19:15:33 +02:00
|
|
|
}
|
|
|
|
// __t%d is a special variable for a texture variable.
|
|
|
|
shaderSuffix += fmt.Sprintf(`
|
|
|
|
func imageSrc%[1]dUnsafeAt(pos vec2) vec4 {
|
2023-04-16 11:56:14 +02:00
|
|
|
// pos is the position in positions of the source texture (= 0th image's texture).
|
2023-04-23 15:57:30 +02:00
|
|
|
return __texelAt(__t%[1]d, %[2]s)
|
2022-04-03 19:15:33 +02:00
|
|
|
}
|
2023-08-25 01:01:32 +02:00
|
|
|
`, i, pos)
|
|
|
|
switch unit {
|
|
|
|
case shaderir.Pixels:
|
|
|
|
shaderSuffix += fmt.Sprintf(`
|
|
|
|
func imageSrc%[1]dAt(pos vec2) vec4 {
|
|
|
|
// pos is the position of the source texture (= 0th image's texture).
|
|
|
|
// If pos is in the region, the result is (1, 1). Otherwise, either element is 0.
|
|
|
|
in := step(__imageSrcRegionOrigins[0], pos) - step(__imageSrcRegionOrigins[0] + __imageSrcRegionSizes[%[1]d], pos)
|
|
|
|
return __texelAt(__t%[1]d, %[2]s) * in.x * in.y
|
|
|
|
}
|
|
|
|
`, i, pos)
|
|
|
|
case shaderir.Texels:
|
|
|
|
shaderSuffix += fmt.Sprintf(`
|
2022-04-03 19:15:33 +02:00
|
|
|
func imageSrc%[1]dAt(pos vec2) vec4 {
|
2023-04-16 11:56:14 +02:00
|
|
|
// pos is the position of the source texture (= 0th image's texture).
|
2022-07-10 18:06:07 +02:00
|
|
|
// If pos is in the region, the result is (1, 1). Otherwise, either element is 0.
|
2023-08-25 01:01:32 +02:00
|
|
|
// With the texel mode, all the source region sizes are the same (#1870).
|
|
|
|
// As pos is in texels of the 0th texture, always use the 0th image region size.
|
|
|
|
in := step(__imageSrcRegionOrigins[0], pos) - step(__imageSrcRegionOrigins[0] + __imageSrcRegionSizes[0], pos)
|
2023-04-23 15:57:30 +02:00
|
|
|
return __texelAt(__t%[1]d, %[2]s) * in.x * in.y
|
2022-04-03 19:15:33 +02:00
|
|
|
}
|
|
|
|
`, i, pos)
|
2023-08-25 01:01:32 +02:00
|
|
|
}
|
2022-04-03 19:15:33 +02:00
|
|
|
}
|
2022-04-05 04:12:25 +02:00
|
|
|
|
|
|
|
shaderSuffix += `
|
|
|
|
var __projectionMatrix mat4
|
|
|
|
|
|
|
|
func __vertex(position vec2, texCoord vec2, color vec4) (vec4, vec2, vec4) {
|
|
|
|
return __projectionMatrix * vec4(position, 0, 1), texCoord, color
|
|
|
|
}
|
|
|
|
`
|
2023-04-16 11:56:14 +02:00
|
|
|
return shaderSuffix, nil
|
2022-04-03 19:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func CompileShader(src []byte) (*shaderir.Program, error) {
|
2023-04-16 11:56:14 +02:00
|
|
|
unit, err := shader.ParseCompilerDirectives(src)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
suffix, err := shaderSuffix(unit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-03 19:15:33 +02:00
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.Write(src)
|
2023-04-16 11:56:14 +02:00
|
|
|
buf.WriteString(suffix)
|
2022-04-03 19:15:33 +02:00
|
|
|
|
|
|
|
const (
|
|
|
|
vert = "__vertex"
|
|
|
|
frag = "Fragment"
|
|
|
|
)
|
2023-04-16 11:35:12 +02:00
|
|
|
ir, err := shader.Compile(buf.Bytes(), vert, frag, ShaderImageCount)
|
2022-04-03 19:15:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ir.VertexFunc.Block == nil {
|
2022-12-24 06:34:02 +01:00
|
|
|
return nil, fmt.Errorf("graphics: vertex shader entry point '%s' is missing", vert)
|
2022-04-03 19:15:33 +02:00
|
|
|
}
|
|
|
|
if ir.FragmentFunc.Block == nil {
|
2022-12-24 06:34:02 +01:00
|
|
|
return nil, fmt.Errorf("graphics: fragment shader entry point '%s' is missing", frag)
|
2022-04-03 19:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ir, nil
|
|
|
|
}
|