Skip to content

Commit

Permalink
- Add post processing skybox
Browse files Browse the repository at this point in the history
- Add ability to turn on and off post process plugins
- Improve dithering
- Add drawText background
  • Loading branch information
edunad committed Apr 9, 2024
1 parent c2f6bee commit c25fda2
Show file tree
Hide file tree
Showing 20 changed files with 284 additions and 95 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@

| 001-stencil<br/><a href='/samples/001-stencil'><img src="https://i.rawr.dev/sample1-min-2.gif" width="240" /></a> | 002-generated-models<br/><a href='/samples/002-generated-models'><img src="https://i.rawr.dev/sample2-min-3.gif" width="240" /></a> | 003-light<br/><a href='/samples/003-light'><img src="https://i.rawr.dev/sample3-min-3.gif" width="240" /></a> |
| :-----------------------------------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------: |
| 004-instancing<br/><a href='/samples/004-instancing'><img src="https://i.rawr.dev/sample4-min.gif" width="240" /></a> | 005-post-process<br/><a href='/samples/005-post-process'><img src="https://i.rawr.dev/sample5-min.gif" width="240" /></a> | 006-decals<br/><a href='/samples/006-decals'><img src="https://i.rawr.dev/sample6-min-2.gif" width="240" /></a> |
| 004-instancing<br/><a href='/samples/004-instancing'><img src="https://i.rawr.dev/sample4-min.gif" width="240" /></a> | 005-post-process<br/><a href='/samples/005-post-process'><img src="https://i.rawr.dev/sample5-min-2.gif" width="240" /></a> | 006-decals<br/><a href='/samples/006-decals'><img src="https://i.rawr.dev/sample6-min-2.gif" width="240" /></a> |
| 007-particle-system<br/><a href='/samples/007-particle-system'><img src="https://i.rawr.dev/sample7-min.gif" width="240" /></a> | 008-ui<br/><a href='/samples/008-ui'><img src="https://i.rawr.dev/sample8-min.gif" width="240" /></a> | 009-assimp<br/><a href='/samples/009-assimp'><img src="https://i.rawr.dev/sample9-min.gif" width="240" /></a> |
| 010-bass-audio<br/><a href='/samples/010-bass-audio'><img src="https://i.rawr.dev/bylavGsjpB.png" width="240" /></a> | 011-physics-3D<br/><a href='/samples/011-physics-3D'><img src="https://i.rawr.dev/sample11-min.gif" width="240" /></a> | 012-physics-2D<br/><a href='/samples/012-physics-2D'><img src="https://i.rawr.dev/sample12-min.gif" width="240" /></a> |
| 013-webm<br/><a href='/samples/013-webm'><img src="https://i.rawr.dev/sample13-min.gif" width="240" /></a> | 014-scripting<br/><a href='/samples/014-scripting'><img src="https://i.rawr.dev/sample14-min.gif" width="240" /></a> | 015-gpu-picking<br/><a href='/samples/015-gpu-picking'><img src="https://i.rawr.dev/sample15-min.gif" width="240" /></a> |
30 changes: 11 additions & 19 deletions rawrbox.math/include/rawrbox/math/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,7 @@ namespace rawrbox {
}

[[nodiscard]] uint32_t pack() const {
return pack(this->r, this->g, this->b, this->a);
}

[[nodiscard]] static uint32_t pack(NumberType r, NumberType g, NumberType b, NumberType a) {
if constexpr (std::is_same_v<NumberType, int>) {
return 0 | (static_cast<uint8_t>(r) << 0) | (static_cast<uint8_t>(g) << 8) | (static_cast<uint8_t>(b) << 16) | (static_cast<uint8_t>(a) << 24);
} else {
return 0 | (static_cast<uint8_t>(r * 255.0F) << 0) | (static_cast<uint8_t>(g * 255.0F) << 8) | (static_cast<uint8_t>(b * 255.0F) << 16) | (static_cast<uint8_t>(a * 255.0F) << 24);
}
return rawrbox::PackUtils::packRgba8(static_cast<float>(r), static_cast<float>(g), static_cast<float>(b), static_cast<float>(a));
}

ColorType operator-(const ColorType& other) const {
Expand Down Expand Up @@ -305,79 +297,79 @@ namespace rawrbox {
if constexpr (std::is_same_v<NumberType, int>) {
return ColorType(0, 0, 0, 255);
} else {
return ColorType(0, 0, 0, 1);
return ColorType(0.F, 0.F, 0.F, 1.F);
}
}

[[nodiscard]] static inline constexpr ColorType White() {
if constexpr (std::is_same_v<NumberType, int>) {
return ColorType(255, 255, 255, 255);
} else {
return ColorType(1, 1, 1, 1);
return ColorType(1.F, 1.F, 1.F, 1.F);
}
}

[[nodiscard]] static inline constexpr ColorType Gray() {
if constexpr (std::is_same_v<NumberType, int>) {
return ColorType(206, 204, 191, 255);
} else {
return ColorType(0.81F, 0.8F, 0.75F, 1);
return ColorType(0.81F, 0.8F, 0.75F, 1.F);
}
}

[[nodiscard]] static inline constexpr ColorType Red() {
if constexpr (std::is_same_v<NumberType, int>) {
return ColorType(255, 82, 82, 255);
} else {
return ColorType(1, 0.32F, 0.32F, 1);
return ColorType(1, 0.32F, 0.32F, 1.F);
}
}

[[nodiscard]] static inline constexpr ColorType Green() {
if constexpr (std::is_same_v<NumberType, int>) {
return ColorType(36, 222, 128, 255);
} else {
return ColorType(0.14F, 0.87F, 0.50F, 1);
return ColorType(0.14F, 0.87F, 0.50F, 1.F);
}
}

[[nodiscard]] static inline constexpr ColorType Blue() {
if constexpr (std::is_same_v<NumberType, int>) {
return ColorType(51, 171, 222, 255);
} else {
return ColorType(0.2F, 0.67F, 0.87F, 1);
return ColorType(0.2F, 0.67F, 0.87F, 1.F);
}
}

[[nodiscard]] static inline constexpr ColorType Orange() {
if constexpr (std::is_same_v<NumberType, int>) {
return ColorType(255, 120, 61, 255);
} else {
return ColorType(1, 0.47F, 0.24F, 1);
return ColorType(1, 0.47F, 0.24F, 1.F);
}
}

[[nodiscard]] static inline constexpr ColorType Yellow() {
if constexpr (std::is_same_v<NumberType, int>) {
return ColorType(255, 230, 64, 255);
} else {
return ColorType(1, 0.9F, 0.25F, 1);
return ColorType(1, 0.9F, 0.25F, 1.F);
}
}

[[nodiscard]] static inline constexpr ColorType Purple() {
if constexpr (std::is_same_v<NumberType, int>) {
return ColorType(255, 0, 255, 255);
} else {
return ColorType(1.F, 0.F, 1.F, 1);
return ColorType(1.F, 0.F, 1.F, 1.F);
}
}

[[nodiscard]] static inline constexpr ColorType Brown() {
if constexpr (std::is_same_v<NumberType, int>) {
return ColorType(133, 88, 49, 255);
} else {
return ColorType(0.52F, 0.35F, 0.19F, 1);
return ColorType(0.52F, 0.35F, 0.19F, 1.F);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
#define INCLUDED_PIXEL_POST_PROCESS_UNIFORMS

struct PostProcessConstantsStruct {
#ifdef UINT_DATA
uint4 data[MAX_POST_DATA];
#else
float4 data[MAX_POST_DATA];
#endif

uint textureID;
uint textureDepthID;
Expand Down
3 changes: 3 additions & 0 deletions rawrbox.render/assets/shaders/include/camera.fxh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#define NearFar SCamera.viewport.xy
#define px (float2(1.0, 1.0) / ScreenSize)

#define CAMERA_UP float3(0.0, 1.0, 0.0)
#define CAMERA_RIGHT float3(1.0, 0.0, 0.0)

// UTILS -----------------
uint GetSliceFromDepth(float depth) {
return floor(log(depth) * SCamera.gridParams.x - SCamera.gridParams.y);
Expand Down
88 changes: 44 additions & 44 deletions rawrbox.render/assets/shaders/include/dither.fxh
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
#ifndef INCLUDED_DITHER
#define INCLUDED_DITHER

float3 getDither(float2 pos, float3 c, float intensity) {
int DITHER_THRESHOLDS[16] = {
-4, 0, -3, 1,
2, -2, 3, -1,
-3, 1, -4, 0,
3, -1, 2, -2
};

int DITHER_COLORS = 256;
uint index = (uint(pos.x) & 3) * 4 + (uint(pos.y) & 3);

c.rgb = clamp(c.rgb * (DITHER_COLORS - 1) + DITHER_THRESHOLDS[index] * (intensity * 100), min16float3(0, 0, 0), min16float3(DITHER_COLORS - 1, DITHER_COLORS - 1, DITHER_COLORS - 1));
c /= DITHER_COLORS;

return c;
}

float getDitherFast(float2 uv, float factor) {
float DITHER_THRESHOLDS[16] ={
1.0 / 17.0, 9.0 / 17.0, 3.0 / 17.0, 11.0 / 17.0,
13.0 / 17.0, 5.0 / 17.0, 15.0 / 17.0, 7.0 / 17.0,
4.0 / 17.0, 12.0 / 17.0, 2.0 / 17.0, 10.0 / 17.0,
16.0 / 17.0, 8.0 / 17.0, 14.0 / 17.0, 6.0 / 17.0
};

uint index = (uint(uv.x) % 4) * 4 + uint(uv.y) % 4;
return factor - DITHER_THRESHOLDS[index];
}

float alphaDither(float2 uv, float alpha) {
float DITHER_THRESHOLDS[16] ={
1.0 / 17.0, 9.0 / 17.0, 3.0 / 17.0, 11.0 / 17.0,
13.0 / 17.0, 5.0 / 17.0, 15.0 / 17.0, 7.0 / 17.0,
4.0 / 17.0, 12.0 / 17.0, 2.0 / 17.0, 10.0 / 17.0,
16.0 / 17.0, 8.0 / 17.0, 14.0 / 17.0, 6.0 / 17.0
};

uint index = (uint(uv.x) % 4) * 4 + uint(uv.y) % 4;
clip(alpha - DITHER_THRESHOLDS[index]);

return alpha;
}
#define INCLUDED_DITHER

float3 getDither(float2 pos, float3 c, float intensity) {
int DITHER_THRESHOLDS[16] = {
-4, 0, -3, 1,
2, -2, 3, -1,
-3, 1, -4, 0,
3, -1, 2, -2
};

int DITHER_COLORS = 128;
uint index = (uint(pos.x) & 3) * 4 + (uint(pos.y) & 3);

c.rgb = clamp(c.rgb * (DITHER_COLORS - 1) + DITHER_THRESHOLDS[index] * (intensity * 100), min16float3(0, 0, 0), min16float3(DITHER_COLORS - 1, DITHER_COLORS - 1, DITHER_COLORS - 1));
c /= DITHER_COLORS;

return c;
}

float getDitherFast(float2 uv, float factor) {
float DITHER_THRESHOLDS[16] ={
1.0 / 17.0, 9.0 / 17.0, 3.0 / 17.0, 11.0 / 17.0,
13.0 / 17.0, 5.0 / 17.0, 15.0 / 17.0, 7.0 / 17.0,
4.0 / 17.0, 12.0 / 17.0, 2.0 / 17.0, 10.0 / 17.0,
16.0 / 17.0, 8.0 / 17.0, 14.0 / 17.0, 6.0 / 17.0
};

uint index = (uint(uv.x) % 4) * 4 + uint(uv.y) % 4;
return factor - DITHER_THRESHOLDS[index];
}

float alphaDither(float2 uv, float alpha) {
float DITHER_THRESHOLDS[16] ={
1.0 / 17.0, 9.0 / 17.0, 3.0 / 17.0, 11.0 / 17.0,
13.0 / 17.0, 5.0 / 17.0, 15.0 / 17.0, 7.0 / 17.0,
4.0 / 17.0, 12.0 / 17.0, 2.0 / 17.0, 10.0 / 17.0,
16.0 / 17.0, 8.0 / 17.0, 14.0 / 17.0, 6.0 / 17.0
};

uint index = (uint(uv.x) % 4) * 4 + uint(uv.y) % 4;
clip(alpha - DITHER_THRESHOLDS[index]);

return alpha;
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
// ----------------------

float4 billboardTransform(float4 vertex, uint billboard) {
float3 right = float3(1, 0, 0);
float3 up = float3(0, 1, 0);
float3 right = CAMERA_RIGHT;
float3 up = CAMERA_UP;

if ((billboard & 2) != 0) { // X
right = float3(Camera.view[0][0], Camera.view[1][0], Camera.view[2][0]);
Expand Down
14 changes: 8 additions & 6 deletions rawrbox.render/assets/shaders/post_process/dither.psh
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ void main(in PSInput PSIn, out PSOutput PSOut) {

// Dither -----
if(DitherMode == 0.0) { // SLOW MODE
color.rgb = getDither(UV.xy * ScreenSize, color.rgb, DitherIntensity * depth / 3.0);
} else {
color.rgb += getDitherFast(UV.xy * ScreenSize, 1) * DitherIntensity * depth / 3.0;
}
color.rgb = getDither(UV.xy * ScreenSize, color.rgb, DitherIntensity * depth / 3.0);
} else {
color.rgb += getDitherFast(UV.xy * ScreenSize, 1) * DitherIntensity * depth / 3.0;
}

// Posterize
PSOut.Color = saturate(floor(color * DitherColorDepth) / DitherColorDepth);
// Posterize
color = saturate(floor(color * DitherColorDepth) / DitherColorDepth);

PSOut.Color = color;
}
51 changes: 51 additions & 0 deletions rawrbox.render/assets/shaders/post_process/skybox.psh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "camera.fxh"

#define UINT_DATA
#include "pixel_post_process_uniforms.fxh"

#include "math.fxh"
#include "unpack.fxh"

// ## MAPPING ----------------
#define START_COLOR PostProcessConstants.data[0].x
#define END_COLOR PostProcessConstants.data[0].y
#define PSX_BANDING PostProcessConstants.data[0].z
// ========================

Texture2DArray g_Textures[];
SamplerState g_Sampler;

struct PSInput {
float4 Pos : SV_POSITION;
float2 UV : TEX_COORD;
};

struct PSOutput {
float4 Color : SV_TARGET;
};

void main(in PSInput PSIn, out PSOutput PSOut) {
#if defined(DESKTOP_GL) || defined(GL_ES)
float3 UV = float3(PSIn.UV.x, 1.0 - PSIn.UV.y, 0);
#else
float3 UV = float3(PSIn.UV.xy, 0);
#endif

float Depth = g_Textures[PostProcessConstants.textureDepthID].Sample(g_Sampler, UV).r;
Depth = 1.0 - (2.0 * NearFar.x) / (NearFar.y + NearFar.x - Depth * (NearFar.y - NearFar.x));
if (Depth > 0.01) {
PSOut.Color = g_Textures[PostProcessConstants.textureID].Sample(g_Sampler, UV);
return;
}

// Calculate the view direction vector
float3 viewDir = mul(float4(PSIn.UV * 2.0 - 1.0, 1.0, 1.0), Camera.viewInv).xyz;
viewDir = normalize(viewDir);

float2 texCoord = float2(
floor((atan2(viewDir.z, viewDir.x) / (2.0 * PI) + 0.5) * PSX_BANDING) / PSX_BANDING,
floor((acos(viewDir.y) / PI) * PSX_BANDING) / PSX_BANDING
);

PSOut.Color = lerp(Unpack_RGBA8_UNORM(END_COLOR), Unpack_RGBA8_UNORM(START_COLOR), texCoord.y);
}
17 changes: 12 additions & 5 deletions rawrbox.render/include/rawrbox/render/plugins/post_process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,26 @@ namespace rawrbox {
void bindStatic(Diligent::IPipelineResourceSignature& sig) override;
void postRender(rawrbox::TextureRender& render) override;

// Process utils ----
// PLUGIN UTILS ----
template <class T = rawrbox::PostProcessBase, typename... CallbackArgs>
requires(std::derived_from<T, rawrbox::PostProcessBase>)
void add(CallbackArgs&&... args) {
this->_postProcesses.push_back(std::make_unique<T>(std::forward<CallbackArgs>(args)...));
rawrbox::PostProcessBase* add(CallbackArgs&&... args) {
auto process = std::make_unique<T>(std::forward<CallbackArgs>(args)...);

auto* pro = process.get();
this->_postProcesses.push_back(std::move(process));
return pro;
}

virtual void remove(size_t indx);
[[nodiscard]] virtual rawrbox::PostProcessBase& get(size_t indx) const;
// ----

// UTILS ----=
[[nodiscard]] virtual rawrbox::PostProcessBase* get(size_t indx) const;
[[nodiscard]] virtual Diligent::IBuffer* getBuffer() const;
virtual size_t count();
// ---------

std::string getID() override;
// ---------
};
} // namespace rawrbox
8 changes: 8 additions & 0 deletions rawrbox.render/include/rawrbox/render/post_process/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ namespace rawrbox {
protected:
Diligent::IBuffer* _buffer = nullptr;
Diligent::IPipelineState* _pipeline = nullptr;
bool _enabled = true;

#ifdef UINT_DATA
std::array<rawrbox::Vector4u, rawrbox::MAX_POST_DATA> _data = {};
#else
std::array<rawrbox::Vector4f, rawrbox::MAX_POST_DATA> _data = {};
#endif

// LOGGER ------
std::unique_ptr<rawrbox::Logger> _logger = std::make_unique<rawrbox::Logger>("RawrBox-PostProcess");
Expand All @@ -26,5 +31,8 @@ namespace rawrbox {

virtual void init();
virtual void applyEffect(const rawrbox::TextureBase& texture);

virtual void setEnabled(bool enabled);
[[nodiscard]] virtual bool isEnabled() const;
};
} // namespace rawrbox
22 changes: 22 additions & 0 deletions rawrbox.render/include/rawrbox/render/post_process/skybox.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#define UINT_DATA
#include <rawrbox/render/post_process/base.hpp>

namespace rawrbox {
class PostProcessSkybox : public rawrbox::PostProcessBase {
public:
explicit PostProcessSkybox(const rawrbox::Colorf& startColor, const rawrbox::Colorf& endColor);
PostProcessSkybox(PostProcessSkybox&&) = delete;
PostProcessSkybox& operator=(PostProcessSkybox&&) = delete;
PostProcessSkybox(const PostProcessSkybox&) = delete;
PostProcessSkybox& operator=(const PostProcessSkybox&) = delete;
~PostProcessSkybox() override = default;

virtual void setStartColor(const rawrbox::Colorf& col);
virtual void setEndColor(const rawrbox::Colorf& col);
virtual void setPSXBanding(uint32_t banding);

void init() override;
};
} // namespace rawrbox
2 changes: 1 addition & 1 deletion rawrbox.render/include/rawrbox/render/stencil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ namespace rawrbox {
virtual void drawTexture(const rawrbox::Vector2f& pos, const rawrbox::Vector2f& size, const rawrbox::TextureBase& tex, const rawrbox::Color& col = rawrbox::Colors::White(), const rawrbox::Vector2f& uvStart = {0, 0}, const rawrbox::Vector2f& uvEnd = {1, 1}, uint32_t atlasId = 0);
virtual void drawCircle(const rawrbox::Vector2f& pos, const rawrbox::Vector2f& size, const rawrbox::Color& col = rawrbox::Colors::White(), size_t roundness = 32, float angleStart = 0.F, float angleEnd = 360.F);
virtual void drawLine(const rawrbox::Vector2& from, const rawrbox::Vector2& to, const rawrbox::Color& col = rawrbox::Colors::White());
virtual void drawText(const std::string& text, const rawrbox::Vector2f& pos, const rawrbox::Color& col = rawrbox::Colors::White());
virtual void drawText(const std::string& text, const rawrbox::Vector2f& pos, const rawrbox::Color& col = rawrbox::Colors::White(), const rawrbox::Color& bgCol = rawrbox::Colors::Transparent());
virtual void drawText(const rawrbox::Font& font, const std::string& text, const rawrbox::Vector2f& pos, const rawrbox::Color& col = rawrbox::Colors::White(), rawrbox::Alignment alignX = rawrbox::Alignment::Left, rawrbox::Alignment alignY = rawrbox::Alignment::Left);
// --------------------

Expand Down
Loading

0 comments on commit c25fda2

Please sign in to comment.