Skip to content

Commit

Permalink
[Render/RenderSystem] The scene's dimensions can be recovered
Browse files Browse the repository at this point in the history
- They may not necessarily be the same as the window's, and should be those used for the camera's viewport & buffers
  • Loading branch information
Razakhel committed Nov 13, 2024
1 parent 53750df commit 59ced50
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 55 deletions.
7 changes: 4 additions & 3 deletions examples/bloomDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int main() {
window.setCloseCallback([&app] () noexcept { app.quit(); });

Raz::Entity& camera = world.addEntityWithComponent<Raz::Transform>(Raz::Vec3f(0.f, 0.f, 5.f));
camera.addComponent<Raz::Camera>(window.getWidth(), window.getHeight());
camera.addComponent<Raz::Camera>(render.getSceneWidth(), render.getSceneHeight());

DemoUtils::setupCameraControls(camera, window);

Expand All @@ -53,8 +53,9 @@ int main() {
Raz::RenderGraph& renderGraph = render.getRenderGraph();
Raz::RenderPass& geometryPass = renderGraph.getGeometryPass();

const auto depthBuffer = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::DEPTH);
const auto colorBuffer = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::RGB, Raz::TextureDataType::FLOAT16);
const auto depthBuffer = Raz::Texture2D::create(render.getSceneWidth(), render.getSceneHeight(), Raz::TextureColorspace::DEPTH);
const auto colorBuffer = Raz::Texture2D::create(render.getSceneWidth(), render.getSceneHeight(),
Raz::TextureColorspace::RGB, Raz::TextureDataType::FLOAT16);

#if !defined(USE_OPENGL_ES)
if (Raz::Renderer::checkVersion(4, 3)) {
Expand Down
22 changes: 11 additions & 11 deletions examples/fullDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ int main() {
// For demonstration purposes, a buffer is created here for each process' output. However, this is far from ideal;
// always reuse buffers across passes whenever you can, as it may save a lot of memory
// Note though that a buffer cannot be set as both read & write in the same pass or process
const auto depthBuffer = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::DEPTH);
const auto colorBuffer = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::RGB);
const auto gradientBuffer = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::RGB);
const auto gradDirBuffer = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::RGB);
const auto edgeBuffer = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::GRAY);
const auto edgeBlendBuffer = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::RGB);
const auto chromAberrBuffer = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::RGB);
const auto blurredBuffer = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::RGB);
const auto vignetteBuffer = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::RGB);
const auto filmGrainBuffer = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::RGB);
const auto depthBuffer = Raz::Texture2D::create(renderSystem.getSceneWidth(), renderSystem.getSceneHeight(), Raz::TextureColorspace::DEPTH);
const auto colorBuffer = Raz::Texture2D::create(renderSystem.getSceneWidth(), renderSystem.getSceneHeight(), Raz::TextureColorspace::RGB);
const auto gradientBuffer = Raz::Texture2D::create(renderSystem.getSceneWidth(), renderSystem.getSceneHeight(), Raz::TextureColorspace::RGB);
const auto gradDirBuffer = Raz::Texture2D::create(renderSystem.getSceneWidth(), renderSystem.getSceneHeight(), Raz::TextureColorspace::RGB);
const auto edgeBuffer = Raz::Texture2D::create(renderSystem.getSceneWidth(), renderSystem.getSceneHeight(), Raz::TextureColorspace::GRAY);
const auto edgeBlendBuffer = Raz::Texture2D::create(renderSystem.getSceneWidth(), renderSystem.getSceneHeight(), Raz::TextureColorspace::RGB);
const auto chromAberrBuffer = Raz::Texture2D::create(renderSystem.getSceneWidth(), renderSystem.getSceneHeight(), Raz::TextureColorspace::RGB);
const auto blurredBuffer = Raz::Texture2D::create(renderSystem.getSceneWidth(), renderSystem.getSceneHeight(), Raz::TextureColorspace::RGB);
const auto vignetteBuffer = Raz::Texture2D::create(renderSystem.getSceneWidth(), renderSystem.getSceneHeight(), Raz::TextureColorspace::RGB);
const auto filmGrainBuffer = Raz::Texture2D::create(renderSystem.getSceneWidth(), renderSystem.getSceneHeight(), Raz::TextureColorspace::RGB);

#if !defined(USE_OPENGL_ES)
if (Raz::Renderer::checkVersion(4, 3)) {
Expand Down Expand Up @@ -174,7 +174,7 @@ int main() {
////////////

Raz::Entity& camera = world.addEntity();
auto& cameraComp = camera.addComponent<Raz::Camera>(window.getWidth(), window.getHeight());
auto& cameraComp = camera.addComponent<Raz::Camera>(renderSystem.getSceneWidth(), renderSystem.getSceneHeight());
auto& cameraTrans = camera.addComponent<Raz::Transform>(Raz::Vec3f(0.f, 0.f, 5.f));

DemoUtils::setupCameraControls(camera, window);
Expand Down
2 changes: 1 addition & 1 deletion examples/minDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int main() {
window.setCloseCallback([&app] () noexcept { app.quit(); });

Raz::Entity& camera = world.addEntityWithComponent<Raz::Transform>(Raz::Vec3f(0.f, 0.f, 5.f));
camera.addComponent<Raz::Camera>(window.getWidth(), window.getHeight());
camera.addComponent<Raz::Camera>(render.getSceneWidth(), render.getSceneHeight());

Raz::Entity& mesh = world.addEntityWithComponent<Raz::Transform>();
mesh.addComponent<Raz::MeshRenderer>(Raz::MeshFormat::load(RAZ_ROOT "assets/meshes/ball.obj").second);
Expand Down
2 changes: 1 addition & 1 deletion examples/physicsDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int main() {
window.setCloseCallback([&app] () noexcept { app.quit(); });

Raz::Entity& camera = world.addEntity();
auto& cameraComp = camera.addComponent<Raz::Camera>(window.getWidth(), window.getHeight());
auto& cameraComp = camera.addComponent<Raz::Camera>(render.getSceneWidth(), render.getSceneHeight());
auto& cameraTrans = camera.addComponent<Raz::Transform>(Raz::Vec3f(0.f, 10.f, 25.f));
cameraComp.setCameraType(Raz::CameraType::LOOK_AT);

Expand Down
2 changes: 1 addition & 1 deletion examples/showcaseDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ int main() {

Raz::Entity& camera = world.addEntity();
auto& cameraTrans = camera.addComponent<Raz::Transform>(Raz::Vec3f(0.f, 0.f, 5.f));
camera.addComponent<Raz::Camera>(window.getWidth(), window.getHeight());
camera.addComponent<Raz::Camera>(render.getSceneWidth(), render.getSceneHeight());

//////////////////
// Light entity //
Expand Down
12 changes: 6 additions & 6 deletions examples/ssrDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int main() {
window.setCloseCallback([&app] () noexcept { app.quit(); });

Raz::Entity& camera = world.addEntityWithComponent<Raz::Transform>(Raz::Vec3f(0.f, 0.25f, 10.f));
camera.addComponent<Raz::Camera>(window.getWidth(), window.getHeight());
camera.addComponent<Raz::Camera>(render.getSceneWidth(), render.getSceneHeight());

DemoUtils::setupCameraControls(camera, window);

Expand Down Expand Up @@ -95,11 +95,11 @@ int main() {
Raz::RenderGraph& renderGraph = render.getRenderGraph();
Raz::RenderPass& geometryPass = renderGraph.getGeometryPass();

const auto depthBuffer = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::DEPTH);
const auto colorBuffer = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::RGB);
const auto blurredColorBuffer = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::RGB);
const auto normalBuffer = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::RGB);
const auto specularBuffer = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::RGBA);
const auto depthBuffer = Raz::Texture2D::create(render.getSceneWidth(), render.getSceneHeight(), Raz::TextureColorspace::DEPTH);
const auto colorBuffer = Raz::Texture2D::create(render.getSceneWidth(), render.getSceneHeight(), Raz::TextureColorspace::RGB);
const auto blurredColorBuffer = Raz::Texture2D::create(render.getSceneWidth(), render.getSceneHeight(), Raz::TextureColorspace::RGB);
const auto normalBuffer = Raz::Texture2D::create(render.getSceneWidth(), render.getSceneHeight(), Raz::TextureColorspace::RGB);
const auto specularBuffer = Raz::Texture2D::create(render.getSceneWidth(), render.getSceneHeight(), Raz::TextureColorspace::RGBA);

#if !defined(USE_OPENGL_ES)
if (Raz::Renderer::checkVersion(4, 3)) {
Expand Down
8 changes: 4 additions & 4 deletions examples/xrDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ int main() {
window.addKeyCallback(Raz::Keyboard::ESCAPE, [&app] (float) noexcept { app.quit(); });
window.setCloseCallback([&app] () noexcept { app.quit(); });

// Enabling XR in the render system changes the render viewport's size according to what the detected XR device expects
auto& xr = world.addSystem<Raz::XrSystem>("RaZ - XR demo");
render.enableXr(xr);

// In an XR workflow, the camera is optional; its parameters aren't technically used, but its transform is
auto& camera = world.addEntityWithComponent<Raz::Transform>();
camera.addComponent<Raz::Camera>(window.getWidth(), window.getHeight());
camera.addComponent<Raz::Camera>(render.getSceneWidth(), render.getSceneHeight());

DemoUtils::setupCameraControls(camera, window);

Expand All @@ -33,9 +34,8 @@ int main() {

world.addEntityWithComponent<Raz::Transform>().addComponent<Raz::Light>(Raz::LightType::DIRECTIONAL, -Raz::Axis::Z, 1.f, Raz::ColorPreset::White);

// TODO: the textures' dimensions must be the same as the rendering viewport's
const auto colorBuffer = Raz::Texture2D::create(2468, 2584, Raz::TextureColorspace::RGBA);
const auto depthBuffer = Raz::Texture2D::create(2468, 2584, Raz::TextureColorspace::DEPTH);
const auto colorBuffer = Raz::Texture2D::create(render.getSceneWidth(), render.getSceneHeight(), Raz::TextureColorspace::RGBA);
const auto depthBuffer = Raz::Texture2D::create(render.getSceneWidth(), render.getSceneHeight(), Raz::TextureColorspace::DEPTH);

// The last executed pass *MUST* have a write color buffer, and at least the geometry pass *MUST* have a write depth buffer
Raz::RenderPass& geomPass = render.getGeometryPass();
Expand Down
2 changes: 2 additions & 0 deletions include/RaZ/Render/RenderSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class RenderSystem final : public System {
m_window->getHeight()); }
#endif

unsigned int getSceneWidth() const { return m_sceneWidth; }
unsigned int getSceneHeight() const { return m_sceneHeight; }
#if !defined(RAZ_NO_WINDOW)
bool hasWindow() const { return (m_window != nullptr); }
const Window& getWindow() const { assert("Error: The window must be set before being accessed." && hasWindow()); return *m_window; }
Expand Down
2 changes: 1 addition & 1 deletion scripts/demo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ window:setCloseCallback(function () app:quit() end)

local camera = world:addEntity()
camera:addComponent(Transform.new(Vec3f.new(0, 0, 5)))
camera:addComponent(Camera.new(window:getWidth(), window:getHeight()))
camera:addComponent(Camera.new(render:getSceneWidth(), render:getSceneHeight()))

local shield = world:addEntity()
local shieldTrans = shield:addComponent(Transform.new(Vec3f.new(), Quaternionf.identity(), Vec3f.new(0.1)))
Expand Down
2 changes: 2 additions & 0 deletions src/RaZ/Script/LuaRenderSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ void LuaWrapper::registerRenderSystemTypes() {
#endif
>(),
sol::base_classes, sol::bases<System>());
renderSystem["getSceneWidth"] = &RenderSystem::getSceneWidth;
renderSystem["getSceneHeight"] = &RenderSystem::getSceneHeight;
#if !defined(RAZ_NO_WINDOW)
renderSystem["hasWindow"] = &RenderSystem::hasWindow;
renderSystem["getWindow"] = PickNonConstOverload<>(&RenderSystem::getWindow);
Expand Down
52 changes: 30 additions & 22 deletions tests/src/RaZ/Render/RenderProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ TEST_CASE("CannyFilterRenderProcess execution", "[render]") {
world.addEntityWithComponents<Raz::Camera, Raz::Transform>();

const Raz::Image gradientImg = Raz::ImageFormat::load(RAZ_TESTS_ROOT "assets/renders/cook-torrance_ball_sobel_grad.png", true);
REQUIRE(gradientImg.getWidth() == window.getWidth());
REQUIRE(gradientImg.getHeight() == window.getHeight());
REQUIRE(gradientImg.getWidth() == render.getSceneWidth());
REQUIRE(gradientImg.getHeight() == render.getSceneHeight());
const Raz::Image gradDirImg = Raz::ImageFormat::load(RAZ_TESTS_ROOT "assets/renders/cook-torrance_ball_sobel_grad_dir.png", true);
REQUIRE(gradDirImg.getWidth() == window.getWidth());
REQUIRE(gradDirImg.getHeight() == window.getHeight());
REQUIRE(gradDirImg.getWidth() == render.getSceneWidth());
REQUIRE(gradDirImg.getHeight() == render.getSceneHeight());

Raz::Texture2DPtr gradientInput = Raz::Texture2D::create(gradientImg);
Raz::Texture2DPtr gradDirInput = Raz::Texture2D::create(gradDirImg);
const Raz::Texture2DPtr output = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::GRAY, Raz::TextureDataType::BYTE);
const Raz::Texture2DPtr output = Raz::Texture2D::create(render.getSceneWidth(), render.getSceneHeight(),
Raz::TextureColorspace::GRAY, Raz::TextureDataType::BYTE);

auto& canny = render.getRenderGraph().addRenderProcess<Raz::CannyFilterRenderProcess>();
canny.addParent(render.getGeometryPass());
Expand Down Expand Up @@ -82,11 +83,12 @@ TEST_CASE("ChromaticAberrationRenderProcess execution", "[render]") {
world.addEntityWithComponents<Raz::Camera, Raz::Transform>();

const Raz::Image baseImg = Raz::ImageFormat::load(RAZ_TESTS_ROOT "assets/renders/cook-torrance_ball_base.png", true);
REQUIRE(baseImg.getWidth() == window.getWidth());
REQUIRE(baseImg.getHeight() == window.getHeight());
REQUIRE(baseImg.getWidth() == render.getSceneWidth());
REQUIRE(baseImg.getHeight() == render.getSceneHeight());

Raz::Texture2DPtr input = Raz::Texture2D::create(baseImg);
const Raz::Texture2DPtr output = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::RGB, Raz::TextureDataType::BYTE);
const Raz::Texture2DPtr output = Raz::Texture2D::create(render.getSceneWidth(), render.getSceneHeight(),
Raz::TextureColorspace::RGB, Raz::TextureDataType::BYTE);

auto& chromaticAberration = render.getRenderGraph().addRenderProcess<Raz::ChromaticAberrationRenderProcess>();
chromaticAberration.addParent(render.getGeometryPass());
Expand Down Expand Up @@ -128,11 +130,12 @@ TEST_CASE("ConvolutionRenderProcess execution", "[render]") {
world.addEntityWithComponents<Raz::Camera, Raz::Transform>();

const Raz::Image baseImg = Raz::ImageFormat::load(RAZ_TESTS_ROOT "assets/renders/cook-torrance_ball_base.png", true);
REQUIRE(baseImg.getWidth() == window.getWidth());
REQUIRE(baseImg.getHeight() == window.getHeight());
REQUIRE(baseImg.getWidth() == render.getSceneWidth());
REQUIRE(baseImg.getHeight() == render.getSceneHeight());

Raz::Texture2DPtr input = Raz::Texture2D::create(baseImg);
const Raz::Texture2DPtr output = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::RGB, Raz::TextureDataType::BYTE);
const Raz::Texture2DPtr output = Raz::Texture2D::create(render.getSceneWidth(), render.getSceneHeight(),
Raz::TextureColorspace::RGB, Raz::TextureDataType::BYTE);

// Giving a unit kernel so that the output is the same as the input
auto& convolution = render.getRenderGraph().addRenderProcess<Raz::ConvolutionRenderProcess>(Raz::Mat3f(0.f, 0.f, 0.f,
Expand Down Expand Up @@ -162,8 +165,9 @@ TEST_CASE("FilmGrainRenderProcess execution", "[render][!mayfail]") { // May fai
// RenderSystem::update() needs a Camera with a Transform component
world.addEntityWithComponents<Raz::Camera, Raz::Transform>();

Raz::Texture2DPtr input = Raz::Texture2D::create(Raz::ColorPreset::White, window.getWidth(), window.getHeight());
const Raz::Texture2DPtr output = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::RGB, Raz::TextureDataType::BYTE);
Raz::Texture2DPtr input = Raz::Texture2D::create(Raz::ColorPreset::White, render.getSceneWidth(), render.getSceneHeight());
const Raz::Texture2DPtr output = Raz::Texture2D::create(render.getSceneWidth(), render.getSceneHeight(),
Raz::TextureColorspace::RGB, Raz::TextureDataType::BYTE);

auto& filmGrain = render.getRenderGraph().addRenderProcess<Raz::FilmGrainRenderProcess>();
filmGrain.addParent(render.getGeometryPass());
Expand All @@ -189,11 +193,12 @@ TEST_CASE("PixelizationRenderProcess execution", "[render][!mayfail]") { // May
world.addEntityWithComponents<Raz::Camera, Raz::Transform>();

const Raz::Image baseImg = Raz::ImageFormat::load(RAZ_TESTS_ROOT "assets/renders/cook-torrance_ball_base.png", true);
REQUIRE(baseImg.getWidth() == window.getWidth());
REQUIRE(baseImg.getHeight() == window.getHeight());
REQUIRE(baseImg.getWidth() == render.getSceneWidth());
REQUIRE(baseImg.getHeight() == render.getSceneHeight());

Raz::Texture2DPtr input = Raz::Texture2D::create(baseImg);
const Raz::Texture2DPtr output = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::RGB, Raz::TextureDataType::BYTE);
const Raz::Texture2DPtr output = Raz::Texture2D::create(render.getSceneWidth(), render.getSceneHeight(),
Raz::TextureColorspace::RGB, Raz::TextureDataType::BYTE);

auto& pixelization = render.getRenderGraph().addRenderProcess<Raz::PixelizationRenderProcess>();
pixelization.addParent(render.getGeometryPass());
Expand All @@ -219,12 +224,14 @@ TEST_CASE("SobelFilterRenderProcess execution", "[render]") {
world.addEntityWithComponents<Raz::Camera, Raz::Transform>();

const Raz::Image baseImg = Raz::ImageFormat::load(RAZ_TESTS_ROOT "assets/renders/cook-torrance_ball_base.png", true);
REQUIRE(baseImg.getWidth() == window.getWidth());
REQUIRE(baseImg.getHeight() == window.getHeight());
REQUIRE(baseImg.getWidth() == render.getSceneWidth());
REQUIRE(baseImg.getHeight() == render.getSceneHeight());

Raz::Texture2DPtr input = Raz::Texture2D::create(baseImg);
const Raz::Texture2DPtr outputGrad = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::RGB, Raz::TextureDataType::BYTE);
const Raz::Texture2DPtr outputGradDir = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::RGB, Raz::TextureDataType::BYTE);
const Raz::Texture2DPtr outputGrad = Raz::Texture2D::create(render.getSceneWidth(), render.getSceneHeight(),
Raz::TextureColorspace::RGB, Raz::TextureDataType::BYTE);
const Raz::Texture2DPtr outputGradDir = Raz::Texture2D::create(render.getSceneWidth(), render.getSceneHeight(),
Raz::TextureColorspace::RGB, Raz::TextureDataType::BYTE);

auto& sobel = render.getRenderGraph().addRenderProcess<Raz::SobelFilterRenderProcess>();
sobel.addParent(render.getGeometryPass());
Expand All @@ -249,8 +256,9 @@ TEST_CASE("VignetteRenderProcess execution", "[render]") {
// RenderSystem::update() needs a Camera with a Transform component
world.addEntityWithComponents<Raz::Camera, Raz::Transform>();

Raz::Texture2DPtr input = Raz::Texture2D::create(Raz::ColorPreset::White, window.getWidth(), window.getHeight());
const Raz::Texture2DPtr output = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::RGB, Raz::TextureDataType::BYTE);
Raz::Texture2DPtr input = Raz::Texture2D::create(Raz::ColorPreset::White, render.getSceneWidth(), render.getSceneHeight());
const Raz::Texture2DPtr output = Raz::Texture2D::create(render.getSceneWidth(), render.getSceneHeight(),
Raz::TextureColorspace::RGB, Raz::TextureDataType::BYTE);

auto& vignette = render.getRenderGraph().addRenderProcess<Raz::VignetteRenderProcess>();
vignette.addParent(render.getGeometryPass());
Expand Down
Loading

0 comments on commit 59ced50

Please sign in to comment.