16constexpr const char* kBrightShader = R
"(#version 330
18uniform sampler2D texture0;
22 vec3 color = texture(texture0, fragTexCoord).rgb;
23 float brightest = max(color.r, max(color.g, color.b));
24 finalColor = vec4(color * clamp(brightest - 1.0, 0.0, 1.0), 1.0);
31constexpr const char* kSpreadShader = R
"(#version 330
33uniform sampler2D texture0;
39 for (int x = -1; x <= 1; x++) {
40 for (int y = -1; y <= 1; y++) {
41 float weight = (2.0 - abs(float(x))) * (2.0 - abs(float(y)));
42 sum += texture(texture0, fragTexCoord + vec2(x, y) * pixel).rgb * weight;
45 finalColor = vec4(sum / 16.0, 1.0);
50constexpr const char* kCombineShader = R
"(#version 330
52uniform sampler2D texture0;
53uniform sampler2D bloom;
54uniform float strength;
58 vec3 color = texture(texture0, fragTexCoord).rgb;
59 color += texture(bloom, fragTexCoord).rgb * strength;
60 // The screen cuts each channel at white on its own, which turns a bright
61 // orange yellow, so a colour past white is scaled down to it as a whole.
62 float brightest = max(color.r, max(color.g, color.b));
63 finalColor = vec4(color / max(brightest, 1.0), 1.0);
70constexpr const char* kFxaaShader = R
"(#version 330
72uniform sampler2D texture0;
76// How bright each channel looks to us.
77const vec3 luma = vec3(0.299, 0.587, 0.114);
79float Brightness(vec2 at) {
80 return dot(texture(texture0, fragTexCoord + at * pixel).rgb, luma);
84 float nw = Brightness(vec2(-1.0, -1.0));
85 float ne = Brightness(vec2(1.0, -1.0));
86 float sw = Brightness(vec2(-1.0, 1.0));
87 float se = Brightness(vec2(1.0, 1.0));
88 float middle = Brightness(vec2(0.0));
89 float darkest = min(middle, min(min(nw, ne), min(sw, se)));
90 float brightest = max(middle, max(max(nw, ne), max(sw, se)));
91 // Across an edge, the brightness changes, so the edge runs at right angles
93 vec2 along = vec2((sw + se) - (nw + ne), (nw + sw) - (ne + se));
94 float reduce = max((nw + ne + sw + se) / 32.0, 1.0 / 128.0);
95 float scale = 1.0 / (min(abs(along.x), abs(along.y)) + reduce);
96 along = clamp(along * scale, -8.0, 8.0) * pixel;
97 vec3 near = 0.5 * (texture(texture0, fragTexCoord - along / 6.0).rgb +
98 texture(texture0, fragTexCoord + along / 6.0).rgb);
99 vec3 ends = texture(texture0, fragTexCoord - along / 2.0).rgb +
100 texture(texture0, fragTexCoord + along / 2.0).rgb;
101 vec3 far = 0.5 * near + 0.25 * ends;
102 // Reaching further can cross into another edge, which shows as a
103 // brightness that isn't around the pixel.
104 float reached = dot(far, luma);
105 bool crossed = reached < darkest || reached > brightest;
106 finalColor = vec4(crossed ? near : far, 1.0);
112constexpr float kStrength = 0.25f;
116RenderTexture2D LoadFrame(
int width,
int height) {
117 constexpr int kFormat = PIXELFORMAT_UNCOMPRESSED_R16G16B16A16;
118 RenderTexture2D frame{};
119 frame.id = rlLoadFramebuffer();
120 frame.texture = {.id = rlLoadTexture(
nullptr, width, height, kFormat, 1),
125 frame.depth = {.id = rlLoadTextureDepth(width, height,
true),
128 rlFramebufferAttach(frame.id, frame.texture.id, RL_ATTACHMENT_COLOR_CHANNEL0,
129 RL_ATTACHMENT_TEXTURE2D, 0);
130 rlFramebufferAttach(frame.id, frame.depth.id, RL_ATTACHMENT_DEPTH,
131 RL_ATTACHMENT_RENDERBUFFER, 0);
138void DrawOver(
const Texture2D& texture,
float width,
float height) {
139 DrawTexturePro(texture,
140 {0.0f, 0.0f,
static_cast<float>(texture.width),
141 -
static_cast<float>(texture.height)},
142 {0.0f, 0.0f, width, height}, {0.0f, 0.0f}, 0.0f, WHITE);
145void DrawInto(
const RenderTexture2D& target,
const Texture2D& texture) {
146 BeginTextureMode(target);
147 DrawOver(texture,
static_cast<float>(target.texture.width),
148 static_cast<float>(target.texture.height));
156 : bright_(
LoadGlsl(nullptr, kBrightShader)),
157 spread_(
LoadGlsl(nullptr, kSpreadShader)),
158 combine_(
LoadGlsl(nullptr, kCombineShader)),
159 fxaa_(
LoadGlsl(nullptr, kFxaaShader)) {}
162 UnloadRenderTexture(frame_);
163 for (
const RenderTexture2D& half : halves_) {
164 UnloadRenderTexture(half);
166 UnloadShader(bright_);
167 UnloadShader(spread_);
168 UnloadRenderTexture(picture_);
169 UnloadShader(combine_);
176 Begin(graphics, GetRenderWidth(), GetRenderHeight());
181 graphics_ = graphics;
182 if (frame_.texture.width != width || frame_.texture.height != height) {
183 Resize(width, height);
185 BeginTextureMode(frame_);
188void PostProcess::Resize(
int width,
int height) {
189 UnloadRenderTexture(frame_);
190 frame_ = LoadFrame(width, height);
193 SetTextureFilter(frame_.texture, TEXTURE_FILTER_BILINEAR);
196 UnloadRenderTexture(picture_);
197 picture_ = LoadRenderTexture(width, height);
198 SetTextureFilter(picture_.texture, TEXTURE_FILTER_BILINEAR);
199 for (RenderTexture2D& half : halves_) {
200 width = std::max(width / 2, 1);
201 height = std::max(height / 2, 1);
202 UnloadRenderTexture(half);
203 half = LoadFrame(width, height);
204 SetTextureFilter(half.texture, TEXTURE_FILTER_BILINEAR);
210void PostProcess::Bloom() {
212 BeginShaderMode(bright_);
213 DrawInto(halves_[0], frame_.texture);
217 for (
int i = 1; i < kHalves; ++i) {
218 DrawInto(halves_[i], halves_[i - 1].texture);
221 BeginBlendMode(BLEND_ADDITIVE);
222 BeginShaderMode(spread_);
223 for (
int i = kHalves - 1; i > 0; --i) {
224 const Texture2D& texture = halves_[i].texture;
225 const Vector2 pixel = {1.0f /
static_cast<float>(texture.width),
226 1.0f /
static_cast<float>(texture.height)};
227 SetShaderValue(spread_, GetShaderLocation(spread_,
"pixel"), &pixel,
228 SHADER_UNIFORM_VEC2);
229 DrawInto(halves_[i - 1], texture);
239void PostProcess::Show(
const RenderTexture2D* into) {
240 if (graphics_.
bloom) {
246 BeginTextureMode(picture_);
247 Combine(
static_cast<float>(picture_.texture.width),
248 static_cast<float>(picture_.texture.height));
251 const float width = into !=
nullptr ?
static_cast<float>(into->texture.width)
252 : static_cast<float>(GetScreenWidth());
253 const float height = into !=
nullptr
254 ?
static_cast<float>(into->texture.height)
255 :
static_cast<float>(GetScreenHeight());
256 if (into !=
nullptr) {
257 BeginTextureMode(*into);
263 if (graphics_.antialias) {
264 BeginShaderMode(fxaa_);
265 const Vector2 pixel = {1.0f / static_cast<float>(picture_.texture.width),
266 1.0f / static_cast<float>(picture_.texture.height)};
267 SetShaderValue(fxaa_, GetShaderLocation(fxaa_,
"pixel"), &pixel,
268 SHADER_UNIFORM_VEC2);
269 DrawOver(picture_.texture, width, height);
272 Combine(width, height);
274 if (into !=
nullptr) {
279void PostProcess::Combine(
float width,
float height) {
280 BeginShaderMode(combine_);
281 SetShaderValueTexture(combine_, GetShaderLocation(combine_,
"bloom"),
283 const float strength = graphics_.bloom ? kStrength : 0.0f;
284 SetShaderValue(combine_, GetShaderLocation(combine_,
"strength"), &strength,
285 SHADER_UNIFORM_FLOAT);
286 DrawOver(frame_.texture, width, height);
void DrawOnto(const RenderTexture2D &into)
void Draw()
Draws the frame to the screen, with bloom if it's on.
void Begin(const GraphicsSettings &graphics)
Shader LoadGlsl(const char *vertex, const char *fragment)
bool antialias
Smooths the world's edges, with FXAA.