gl.h lists the 62 functions the editor calls, loaded with SDL_GL_GetProcAddress into gl, as in smack-a-brother. The 25k lines of glad and KHR headers are gone.
436 lines
15 KiB
C++
436 lines
15 KiB
C++
#pragma once
|
|
|
|
#include "gl.h"
|
|
#include <glm/glm.hpp>
|
|
|
|
#include <array>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
struct Shader;
|
|
|
|
struct ShaderInfo
|
|
{
|
|
const char* vertex;
|
|
const char* fragment;
|
|
};
|
|
|
|
inline constexpr auto VERTEX = R"(
|
|
#version 330 core
|
|
layout (location = 0) in vec2 i_position;
|
|
layout (location = 1) in vec2 i_uv;
|
|
out vec2 i_uv_out;
|
|
uniform mat4 u_transform;
|
|
void main()
|
|
{
|
|
i_uv_out = i_uv;
|
|
gl_Position = u_transform * vec4(i_position, 0.0, 1.0);
|
|
}
|
|
)";
|
|
|
|
inline constexpr auto TEXTURE_COMPATIBILITY_VERTEX = R"(
|
|
#version 330 core
|
|
layout (location = 0) in vec2 i_position;
|
|
layout (location = 1) in vec2 i_uv;
|
|
out vec2 i_uv_out;
|
|
out vec2 TexCoord0;
|
|
out vec4 Color0;
|
|
uniform mat4 u_transform;
|
|
uniform vec4 u_tint;
|
|
void main()
|
|
{
|
|
i_uv_out = i_uv;
|
|
TexCoord0 = i_uv;
|
|
Color0 = u_tint;
|
|
gl_Position = u_transform * vec4(i_position, 0.0, 1.0);
|
|
}
|
|
)";
|
|
|
|
inline constexpr auto AXIS_VERTEX = R"(
|
|
#version 330 core
|
|
layout (location = 0) in vec2 i_position; // full screen line segment: -1..1
|
|
uniform vec2 u_origin_ndc; // world origin in NDC
|
|
uniform int u_axis; // 0 = X axis, 1 = Y axis
|
|
|
|
void main()
|
|
{
|
|
vec2 pos = (u_axis == 0)
|
|
? vec2(i_position.x, u_origin_ndc.y) // horizontal line across screen
|
|
: vec2(u_origin_ndc.x, i_position.x); // vertical line across screen;
|
|
|
|
gl_Position = vec4(pos, 0.0, 1.0);
|
|
}
|
|
)";
|
|
|
|
inline constexpr auto FRAGMENT = R"(
|
|
#version 330 core
|
|
out vec4 o_fragColor;
|
|
uniform vec4 u_color;
|
|
void main()
|
|
{
|
|
o_fragColor = vec4(u_color.rgb * u_color.a, u_color.a);
|
|
}
|
|
)";
|
|
|
|
// u_blur > 0: Flash's low-quality BlurFilter, a box of 2 * u_blur + 1 texels averaged (premultiplied) over the
|
|
// u_region crop, clear outside it. A wide box steps over texels so a fragment reads at most BLUR_SAMPLES per axis.
|
|
inline constexpr auto TEXTURE_FRAGMENT = R"(
|
|
#version 330 core
|
|
in vec2 i_uv_out;
|
|
uniform sampler2D u_texture;
|
|
uniform vec4 u_tint;
|
|
uniform vec4 u_color_offset;
|
|
uniform float u_blur;
|
|
uniform vec4 u_region;
|
|
uniform vec2 u_texel;
|
|
out vec4 o_fragColor;
|
|
const int BLUR_SAMPLES = 33;
|
|
vec4 texel_premultiplied_get(vec2 uv)
|
|
{
|
|
if (any(lessThan(uv, u_region.xy)) || any(greaterThanEqual(uv, u_region.zw))) return vec4(0.0);
|
|
vec4 color = texture(u_texture, uv);
|
|
return vec4(color.rgb * color.a, color.a);
|
|
}
|
|
void main()
|
|
{
|
|
vec4 texColor = texture(u_texture, i_uv_out);
|
|
if (u_blur > 0.0)
|
|
{
|
|
int radius = int(u_blur);
|
|
int stride = max(1, (radius * 2 + BLUR_SAMPLES) / BLUR_SAMPLES);
|
|
vec4 sum = vec4(0.0);
|
|
float count = 0.0;
|
|
for (int y = -radius; y <= radius; y += stride)
|
|
for (int x = -radius; x <= radius; x += stride)
|
|
{
|
|
sum += texel_premultiplied_get(i_uv_out + vec2(x, y) * u_texel);
|
|
count += 1.0;
|
|
}
|
|
sum /= count;
|
|
texColor = sum.a > 0.0 ? vec4(sum.rgb / sum.a, sum.a) : vec4(0.0);
|
|
}
|
|
texColor *= u_tint;
|
|
texColor.rgb += u_color_offset.rgb;
|
|
texColor.a = clamp(texColor.a + u_color_offset.a, 0.0, 1.0);
|
|
texColor.rgb *= texColor.a;
|
|
o_fragColor = texColor;
|
|
}
|
|
)";
|
|
|
|
inline constexpr auto GRID_VERTEX = R"(
|
|
#version 330 core
|
|
layout (location = 0) in vec2 i_position;
|
|
layout (location = 1) in vec2 i_uv;
|
|
|
|
out vec2 i_uv_out;
|
|
|
|
void main()
|
|
{
|
|
i_uv_out = i_position;
|
|
gl_Position = vec4(i_position, 0.0, 1.0);
|
|
}
|
|
)";
|
|
|
|
inline constexpr auto GRID_FRAGMENT = R"(
|
|
#version 330 core
|
|
in vec2 i_uv_out;
|
|
|
|
uniform mat4 u_transform;
|
|
uniform vec2 u_size;
|
|
uniform vec2 u_offset;
|
|
uniform vec4 u_color;
|
|
|
|
out vec4 o_fragColor;
|
|
|
|
void main()
|
|
{
|
|
vec4 world4 = u_transform * vec4(i_uv_out, 0.0, 1.0);
|
|
vec2 world = world4.xy / world4.w;
|
|
|
|
vec2 cell = max(u_size, vec2(1.0));
|
|
vec2 grid = (world - u_offset) / cell;
|
|
|
|
vec2 frac = fract(grid);
|
|
vec2 distToLine = min(frac, 1.0 - frac);
|
|
float distance = min(distToLine.x, distToLine.y);
|
|
|
|
float fw = min(fwidth(grid.x), fwidth(grid.y));
|
|
float alpha = 1.0 - smoothstep(0.0, fw, distance);
|
|
|
|
if (alpha <= 0.0)
|
|
discard;
|
|
|
|
o_fragColor = vec4(u_color.rgb * u_color.a * alpha, u_color.a * alpha);
|
|
}
|
|
)";
|
|
|
|
inline constexpr auto DASHED_VERTEX = R"(
|
|
#version 330 core
|
|
layout (location = 0) in vec2 i_position;
|
|
|
|
out vec2 v_local;
|
|
|
|
uniform mat4 u_transform;
|
|
|
|
void main()
|
|
{
|
|
v_local = i_position;
|
|
gl_Position = u_transform * vec4(i_position, 0.0, 1.0);
|
|
}
|
|
)";
|
|
|
|
inline constexpr auto DASHED_FRAGMENT = R"(
|
|
#version 330 core
|
|
in vec2 v_local;
|
|
|
|
uniform vec4 u_color;
|
|
uniform vec2 u_axis_x;
|
|
uniform vec2 u_axis_y;
|
|
uniform float u_dash_length;
|
|
uniform float u_dash_gap;
|
|
uniform float u_dash_offset;
|
|
|
|
out vec4 o_fragColor;
|
|
|
|
void main()
|
|
{
|
|
vec2 local = clamp(v_local, 0.0, 1.0);
|
|
|
|
float lengthX = max(length(u_axis_x), 1e-4);
|
|
float lengthY = max(length(u_axis_y), 1e-4);
|
|
|
|
float dash = max(u_dash_length, 1e-4);
|
|
float gap = max(u_dash_gap, 0.0);
|
|
float period = max(dash + gap, 1e-4);
|
|
|
|
vec2 pixel = max(fwidth(v_local), vec2(1e-5));
|
|
|
|
float bottomMask = 1.0 - smoothstep(pixel.y, pixel.y * 2.0, local.y);
|
|
float topMask = 1.0 - smoothstep(pixel.y, pixel.y * 2.0, 1.0 - local.y);
|
|
float leftMask = 1.0 - smoothstep(pixel.x, pixel.x * 2.0, local.x);
|
|
float rightMask = 1.0 - smoothstep(pixel.x, pixel.x * 2.0, 1.0 - local.x);
|
|
|
|
float perimeterOffset = u_dash_offset;
|
|
|
|
float bottomPos = mod(perimeterOffset + local.x * lengthX, period);
|
|
if (bottomPos < 0.0) bottomPos += period;
|
|
float bottomDash = bottomMask * (bottomPos <= dash ? 1.0 : 0.0);
|
|
|
|
float rightPos = mod(perimeterOffset + lengthX + local.y * lengthY, period);
|
|
if (rightPos < 0.0) rightPos += period;
|
|
float rightDash = rightMask * (rightPos <= dash ? 1.0 : 0.0);
|
|
|
|
float topPos = mod(perimeterOffset + lengthX + lengthY + (1.0 - local.x) * lengthX, period);
|
|
if (topPos < 0.0) topPos += period;
|
|
float topDash = topMask * (topPos <= dash ? 1.0 : 0.0);
|
|
|
|
float leftPos = mod(perimeterOffset + 2.0 * lengthX + lengthY + (1.0 - local.y) * lengthY, period);
|
|
if (leftPos < 0.0) leftPos += period;
|
|
float leftDash = leftMask * (leftPos <= dash ? 1.0 : 0.0);
|
|
|
|
float alpha = max(max(bottomDash, topDash), max(leftDash, rightDash));
|
|
|
|
if (alpha <= 0.0)
|
|
discard;
|
|
|
|
o_fragColor = vec4(u_color.rgb * u_color.a * alpha, u_color.a * alpha);
|
|
}
|
|
)";
|
|
|
|
constexpr auto UNIFORM_AXIS = "u_axis";
|
|
constexpr auto UNIFORM_COLOR = "u_color";
|
|
constexpr auto UNIFORM_TRANSFORM = "u_transform";
|
|
constexpr auto UNIFORM_TINT = "u_tint";
|
|
constexpr auto UNIFORM_COLOR_OFFSET = "u_color_offset";
|
|
constexpr auto UNIFORM_OFFSET = "u_offset";
|
|
constexpr auto UNIFORM_ORIGIN_NDC = "u_origin_ndc";
|
|
constexpr auto UNIFORM_SIZE = "u_size";
|
|
constexpr auto UNIFORM_MODEL = "u_model";
|
|
constexpr auto UNIFORM_RECT_SIZE = "u_rect_size";
|
|
constexpr auto UNIFORM_TEXTURE = "u_texture";
|
|
constexpr auto UNIFORM_AXIS_X = "u_axis_x";
|
|
constexpr auto UNIFORM_AXIS_Y = "u_axis_y";
|
|
constexpr auto UNIFORM_DASH_LENGTH = "u_dash_length";
|
|
constexpr auto UNIFORM_DASH_GAP = "u_dash_gap";
|
|
constexpr auto UNIFORM_DASH_OFFSET = "u_dash_offset";
|
|
constexpr auto UNIFORM_BLUR = "u_blur";
|
|
constexpr auto UNIFORM_REGION = "u_region";
|
|
constexpr auto UNIFORM_TEXEL = "u_texel";
|
|
|
|
#define RESOURCE_SHADER_ATTRIBUTE_ALIASES \
|
|
X(0, "i_position") \
|
|
X(0, "a_position") \
|
|
X(0, "Position") \
|
|
X(0, "Position0") \
|
|
X(1, "i_uv") \
|
|
X(1, "a_uv") \
|
|
X(1, "TexCoord") \
|
|
X(1, "TexCoord0") \
|
|
X(1, "TextureCoord") \
|
|
X(1, "UV") \
|
|
X(1, "UV0") \
|
|
X(2, "i_color") \
|
|
X(2, "a_color") \
|
|
X(2, "Color") \
|
|
X(2, "Color0")
|
|
|
|
struct AttributeAlias
|
|
{
|
|
GLuint index = 0;
|
|
const char* name = nullptr;
|
|
};
|
|
|
|
inline constexpr AttributeAlias ATTRIBUTE_ALIASES[] = {
|
|
#define X(index, name) {index, name},
|
|
RESOURCE_SHADER_ATTRIBUTE_ALIASES
|
|
#undef X
|
|
};
|
|
|
|
#define RESOURCE_SHADER_UNIFORM_BINDINGS \
|
|
X(IGNORE, "Ignore", "Ignore") \
|
|
X(MAIN_TEXTURE, "MainTexture", "Main Texture") \
|
|
X(TRANSFORM, "Transform", "Transform") \
|
|
X(FRAME_TINT, "FrameTint", "Frame Tint") \
|
|
X(COLOR_OFFSET, "ColorOffset", "Color Offset") \
|
|
X(TEXTURE_SIZE, "TextureSize", "Texture Size") \
|
|
X(PLAYBACK_TIME, "PlaybackTime", "Playback Time") \
|
|
X(COMPONENTS, "Components", "Per Component") \
|
|
X(MANUAL, "Manual", "Manual")
|
|
|
|
enum UniformBinding
|
|
{
|
|
#define X(symbol, value, label) UNIFORM_BINDING_##symbol,
|
|
RESOURCE_SHADER_UNIFORM_BINDINGS
|
|
#undef X
|
|
UNIFORM_BINDING_COUNT
|
|
};
|
|
|
|
struct UniformBindingInfo
|
|
{
|
|
UniformBinding binding = {};
|
|
const char* value = nullptr;
|
|
const char* label = nullptr;
|
|
};
|
|
|
|
inline constexpr UniformBindingInfo UNIFORM_BINDING_INFOS[] = {
|
|
#define X(symbol, value, label) {UNIFORM_BINDING_##symbol, value, label},
|
|
RESOURCE_SHADER_UNIFORM_BINDINGS
|
|
#undef X
|
|
};
|
|
|
|
#define RESOURCE_SHADER_UNIFORM_VALUE_TYPES \
|
|
X(UNKNOWN, "Unknown", 0) \
|
|
X(FLOAT, "Float", 0) \
|
|
X(INT, "Int", 0) \
|
|
X(VEC2, "Vec2", 2) \
|
|
X(VEC3, "Vec3", 3) \
|
|
X(VEC4, "Vec4", 4) \
|
|
X(MAT4, "Mat4", 0) \
|
|
X(SAMPLER2D, "Sampler2D", 0)
|
|
|
|
enum UniformValueType
|
|
{
|
|
#define X(symbol, label, componentCount) UNIFORM_VALUE_##symbol,
|
|
RESOURCE_SHADER_UNIFORM_VALUE_TYPES
|
|
#undef X
|
|
UNIFORM_VALUE_COUNT
|
|
};
|
|
|
|
struct UniformValueTypeInfo
|
|
{
|
|
UniformValueType type = {};
|
|
const char* label = nullptr;
|
|
int componentCount = 0;
|
|
};
|
|
|
|
inline constexpr UniformValueTypeInfo UNIFORM_VALUE_TYPE_INFOS[] = {
|
|
#define X(symbol, label, componentCount) {UNIFORM_VALUE_##symbol, label, componentCount},
|
|
RESOURCE_SHADER_UNIFORM_VALUE_TYPES
|
|
#undef X
|
|
};
|
|
|
|
struct ShaderUniform
|
|
{
|
|
struct Component
|
|
{
|
|
UniformBinding binding = UNIFORM_BINDING_MANUAL;
|
|
float value = 0;
|
|
};
|
|
|
|
std::string name;
|
|
GLint location = -1;
|
|
GLenum glType = 0;
|
|
GLint size = 0;
|
|
UniformValueType valueType = UNIFORM_VALUE_UNKNOWN;
|
|
UniformBinding binding = UNIFORM_BINDING_IGNORE;
|
|
glm::vec4 value{};
|
|
int intValue = 0;
|
|
std::array<Component, 4> components = {};
|
|
};
|
|
|
|
enum ShaderStage
|
|
{
|
|
SHADER_STAGE_VERTEX,
|
|
SHADER_STAGE_FRAGMENT
|
|
};
|
|
|
|
enum ShaderType
|
|
{
|
|
SHADER_LINE,
|
|
SHADER_DASHED,
|
|
SHADER_TEXTURE,
|
|
SHADER_AXIS,
|
|
SHADER_GRID,
|
|
SHADER_COUNT
|
|
};
|
|
|
|
const ShaderInfo SHADERS[SHADER_COUNT] = {{VERTEX, FRAGMENT},
|
|
{DASHED_VERTEX, DASHED_FRAGMENT},
|
|
{VERTEX, TEXTURE_FRAGMENT},
|
|
{AXIS_VERTEX, FRAGMENT},
|
|
{GRID_VERTEX, GRID_FRAGMENT}};
|
|
|
|
UniformBinding uniform_binding_auto_get(std::string_view, UniformValueType);
|
|
void uniform_configs_apply(const Shader&, std::vector<ShaderUniform>&);
|
|
bool uniform_configs_trim(Shader&, const std::vector<ShaderUniform>&);
|
|
void uniform_config_save(Shader&, const ShaderUniform&);
|
|
UniformBinding uniform_binding_get(std::string_view);
|
|
std::string_view uniform_binding_value_get(UniformBinding);
|
|
std::string_view uniform_binding_label_get(UniformBinding);
|
|
UniformValueType uniform_value_type_get(GLenum);
|
|
std::string_view uniform_value_type_label_get(UniformValueType);
|
|
bool is_uniform_value_editable(UniformValueType);
|
|
bool is_uniform_value_vector(UniformValueType);
|
|
bool is_uniform_binding_valid(UniformBinding, UniformValueType);
|
|
void uniform_value_parse(ShaderUniform&, std::string_view);
|
|
std::string uniform_value_string_get(const ShaderUniform&);
|
|
std::string gles_convert(std::string_view, ShaderStage);
|
|
std::string gles_fragment_convert(std::string_view);
|
|
std::string gles_vertex_convert(std::string_view);
|
|
|
|
struct ShaderCompileResult
|
|
{
|
|
GLuint id = 0;
|
|
std::string output;
|
|
std::vector<ShaderUniform> uniforms;
|
|
bool isCompiled = false;
|
|
bool isLinked = false;
|
|
};
|
|
|
|
ShaderCompileResult shader_compile(const char*, const char*);
|
|
|
|
class ShaderProgram
|
|
{
|
|
public:
|
|
GLuint id = 0;
|
|
std::vector<ShaderUniform> uniforms;
|
|
|
|
ShaderProgram();
|
|
ShaderProgram(const char*, const char*);
|
|
ShaderProgram(ShaderProgram&&) noexcept;
|
|
ShaderProgram& operator=(ShaderProgram&&) noexcept;
|
|
~ShaderProgram();
|
|
bool is_valid() const;
|
|
};
|