| VTK 9.0.1

Renderer.h
Go to the documentation of this file.
1 #pragma once
2
3 #include "../Types.h"
4 #include "Camera.h"
5 #include "Data.h"
6 #include "FrameBuffer.h"
7 #include "Light.h"
8 #include "Model.h"
9 #include "Object.h"
10 #include "Texture.h"
11
12 #include <VisRTX.h>
13 #include <limits>
14
15 namespace RTW
16 {
17 class Renderer : public Object
18 {
19 public:
20 Renderer(const char* /*type*/)
21 {
22 VisRTX::Context* rtx = VisRTX_GetContext();
23 this->renderer = rtx->CreateRenderer();
24
25 this->renderer->SetToneMapping(false);
26 }
27
28 ~Renderer()
29 {
30 this->renderer->Release();
31 }
32
33 void Commit() override
34 {
35 // Model
36 Model* model = this->GetObject<Model>({ "model" });
37 if (model)
38 this->renderer->SetModel(model->model);
39
40 // Camera
41 Camera* camera = this->GetObject<Camera>({ "camera" });
42 if (camera)
43 this->renderer->SetCamera(camera->camera);
44
45 // Lights
46 for (VisRTX::Light* light : this->lastLights)
47 this->renderer->RemoveLight(light);
48
49 this->lastLights.clear();
50
51 Data* lightData = this->GetObject<Data>({ "lights" });
52 if (lightData && lightData->GetDataType() == RTW_OBJECT)
53 {
54 Light** lights = reinterpret_cast<Light**>(lightData->GetData());
55 for (size_t i = 0; i < lightData->GetNumElements(); ++i)
56 {
57 Light* lightHandle = lights[i];
58 if (lightHandle)
59 {
60 VisRTX::Light* light = lightHandle->light;
61 this->renderer->AddLight(light);
62 this->lastLights.push_back(light);
63 }
64 }
65 }
66
67 // Samples per pixel
68 int32_t spp;
69 if (this->Get1i({ "spp" }, &spp))
70 this->renderer->SetSamplesPerPixel(spp);
71
72 // Epsilon
73 float epsilon;
74 if (this->Get1f({ "epsilon" }, &epsilon))
75 this->renderer->SetEpsilon(epsilon);
76
77 // Max ray recursion depth
78 int32_t minBounces = this->Get1i({ "rouletteDepth" }, 5);
79 int32_t maxBounces = this->Get1i({ "maxDepth" }, 10);
80 this->renderer->SetNumBounces(minBounces, maxBounces);
81
82 // Denoiser
83 int denoise = this->Get1i({ "denoise" });
84 this->renderer->SetDenoiser(denoise > 0 ? VisRTX::DenoiserType::AI : VisRTX::DenoiserType::NONE);
85 }
86
87 float RenderFrame(FrameBuffer* frameBuffer, const uint32_t /*frameBufferChannels*/)
88 {
89 if (!frameBuffer)
90 return 0.0f;
91
92 try
93 {
94 this->renderer->Render(frameBuffer->frameBuffer);
95 }
96 catch (VisRTX::Exception& e)
97 {
98 std::cerr << "VisRTX internal error: " << e.what() << std::endl;
99 }
100
101 // VisRTX does not use a variance buffer
102 return std::numeric_limits<float>::infinity();
103 }
104
105 private:
106 VisRTX::Renderer* renderer = nullptr;
107
108 std::vector<VisRTX::Light*> lastLights;
109 };
110 }
RTW::Object::Get1f
float Get1f(const std::vector< std::string > &ids, float defaultValue=0.0f, bool *found=nullptr) const
Definition: Object.h:124
Texture.h
Model.h
RTW::Data::GetData
void * GetData() const
Definition: Data.h:118
RTW::Camera
Definition: Camera.h:13
RTW::Model
Definition: Model.h:12
RTW
Definition: Backend.h:5
RTW::Data::GetDataType
RTWDataType GetDataType() const
Definition: Data.h:108
RTW::Renderer::Commit
void Commit() override
Definition: Renderer.h:33
RTW::Object
Definition: Object.h:17
Light.h
RTW::Light
Definition: Light.h:12
RTW::FrameBuffer
Definition: FrameBuffer.h:10
RTW::Data
Definition: Data.h:9
Object.h
RTW::Object::Get1i
int32_t Get1i(const std::vector< std::string > &ids, int32_t defaultValue=0, bool *found=nullptr) const
Definition: Object.h:107
RTW::Renderer::~Renderer
~Renderer()
Definition: Renderer.h:28
RTW::Renderer::Renderer
Renderer(const char *)
Definition: Renderer.h:20
RTW::Renderer::RenderFrame
float RenderFrame(FrameBuffer *frameBuffer, const uint32_t)
Definition: Renderer.h:87
RTW::Data::GetNumElements
size_t GetNumElements() const
Definition: Data.h:103
Camera.h
RTW::Renderer
Definition: Renderer.h:17
Data.h
FrameBuffer.h
RTW_OBJECT
@ RTW_OBJECT
Definition: Types.h:61