DIE Engine
Loading...
Searching...
No Matches
renderer.h
Go to the documentation of this file.
1
11
12#ifndef RENDERER_H
13#define RENDERER_H
14
15#include "mapobjects.h"
16#include "workerpool.h"
17
18#include <QVector2D>
19#include <QVector3D>
20#include <QImage>
21#include <QList>
22
23#include <atomic>
24#include <stdint.h>
25
26#include <smmintrin.h>
27
28/*****************************************************************************/
42
43/*****************************************************************************/
47typedef struct {
48 QVector3D pos;
49 QVector3D offset;
50 float pan;
51 float tilt;
52} Viewpoint;
53
54/*****************************************************************************/
55constexpr float D2R = 3.14159265f / 180.0f;
56constexpr float R2D = 180.0f / 3.14159265f;
57
58/*****************************************************************************/
60{
61public:
62 static constexpr int DefaultFrameResoX = 960;
63 static constexpr int DefaultFrameResoY = 536;
64 static constexpr int DefaultGlowmapReso = 256;
65 static constexpr float DefaultGlowmapArea = 1024.0f;
66 static constexpr int DefaultNodesMax = 2048;
67 static constexpr int DefaultWallsMax = 1024;
68 static constexpr int DefaultTexturesMax = 32;
69 static constexpr int DefaultLightsMax = 64;
70
71 static constexpr float GlowSampleDistance = 1.0f;
72 static constexpr float GlowBleedFactor = 0.25f;
73 static constexpr float LightFallOff = 32.0f;
74
75 static constexpr float HeightMax = 16384.0f;
76 static constexpr float HeightMin = -16384.0f;
77
79 static constexpr uint16_t ClickNoWall = 0xFFFF;
80
82 typedef struct {
83 QVector3D pos;
84 uint16_t flags;
85 } Node;
86
88 typedef struct {
89 uint16_t nodeID1;
90 uint16_t nodeID2;
91 float height;
92
93 QVector3D offset;
95 uint16_t textureID;
96 uint16_t flags;
97
98 uint32_t rayFront;
99 uint32_t rayBack;
100 } Wall;
101
103 typedef struct {
104 uint32_t * pixels;
105 uint16_t size;
106 uint16_t count;
107 uint16_t mask;
108 uint32_t block;
109 } Texture;
110
112 typedef struct {
113 uint16_t nodeID;
114 uint32_t color;
115 float strength;
116 float x, z;
117 __m128 argb;
118 bool still;
119 } Light;
120
121 Renderer();
122
124 void init();
125
127 void terminate();
128
130 void render(Viewpoint & vp);
131
133 QImage * getImage() const {return image;}
134
137
139 void checkFlag(RENDERER_FLAGS flag, bool checked);
141
143 void setGlowmapSize(int size);
144 int getGlowmapSize() const {return glowmapSize;}
145
147 void setGlowmapArea(float area);
148 float getGlowmapArea() const {return glowmapArea;}
149
151 void setFramesReso(int width, int height);
152 int getFrameResoX() const {return frameResoX;}
153 int getFrameResoY() const {return frameResoY;}
154
156 void setFOVAngle(float angle);
157 void setFOVDistanceNear(float distance);
158 void setFOVDistanceFar(float distance);
159
161 void clickRegister(int x, int y);
162
164 uint16_t clickGetWallID() const {return clickWallID;}
165
166// Shared render state (written by Map::pass and Env::pass)
167 uint32_t flags;
168
169// Field of view
170 float fovAngle;
173
174// Sun lighting (fed by Env::pass)
175 uint32_t sunAmbient;
176 uint32_t sunRayColor;
177 QVector3D sunRayDirection;
180
181// Distance fog (fed by Env::pass)
184 uint32_t fogFarColor;
185 uint16_t fogFlags;
186
187// Ambient occlusion
190
191// Post-fx settings
195 float gammaKRed;
198
199// Object pools, repopulated by Map::pass before each render
203
207
211
215
216// Floor / ceiling heights under the camera, updated by the last render
219
220private:
221 static constexpr int StripsMax = 512;
222
224 typedef enum : uint16_t {
225 VSTRIP_FLAG_FREE = 0x0000,
226 VSTRIP_FLAG_INVISIBLE = 0x0001,
227 VSTRIP_FLAG_ALPHA = 0x0004,
228 VSTRIP_FLAG_HASCEILING = 0x0100,
229 VSTRIP_FLAG_HASFLOOR = 0x0200,
230 VSTRIP_FLAG_SEETHROUGH = 0x0400,
231 VSTRIP_FLAG_BACKCULLED = 0x0800,
232 VSTRIP_FLAG_HIGHLIGHTED = 0x1000,
233 } VSTRIP_FLAGS;
234
235 typedef struct {
236 float k;
237 float yTop;
238 float yBot;
239 float yNoclipTop;
240 float yNoclipBot;
241 float yDarkenTop;
242 float yDarkenBot;
243 float vTop;
244 float vBot;
245 float u;
246 float dist;
247 uint16_t wallId;
248 bool back;
249 uint16_t flags;
250 } Strip;
251
252 typedef struct {
253 QVector3D casterPos;
254 int casterX;
255 float casterRx;
256 float casterRz;
257 float casterPC;
258 float casterHorizon;
259 float casterVShift;
260
261 Strip vStrips[StripsMax];
262 uint16_t vStripsMap[StripsMax];
263 float vStripsZOrders[StripsMax];
264 int vStripsCount;
265
266 Strip vStripsClipped[StripsMax];
267 int vStripsClippedCount;
268
269 float currentCeiling;
270 float currentFloor;
271 } Context;
272 Context renderStates[WorkerPool::WorkersMax];
273
274 int frameResoX;
275 int frameResoY;
276 int glowmapSize;
277 std::atomic<int> pendingFrameWidth;
278 std::atomic<int> pendingFrameHeight;
279 std::atomic<int> pendingGlowmapSize;
280 Viewpoint viewPoint;
281
282 QImage * image;
283 uint32_t * frame;
284 uint32_t * frameLast;
285 bool framesAllocated;
286
287 typedef struct {
288 float bx, bz;
289 float ex, ez;
290 float dx, dz;
291 float length;
292 float invLen;
293 } Segment;
294 Segment * segments;
295
296 uint32_t floorRay;
297 uint32_t ceilingRay;
298
299 __m128 sunAmbientArgb;
300 __m128 sunRayColorArgb;
301
302 float fogDistanceInv;
303 float fogDistanceOffset;
304
305 int clickX;
306 int clickY;
307 bool clickRegistered;
308 uint16_t clickWallID;
309
310 QVector2D sceneCornerBegin, sceneCornerEnd;
311 float glowmapArea;
312 float glowmapScale;
313 __m128 * glowmap;
314 __m128 * glowmapStill;
315 __m128 glowBleed;
316
317 float * pitchTable;
318 float * panTable;
319
320 void allocateObjects(int noNodes, int noWalls, int noTextures, int noLights);
321 void allocateFrames(int width, int height);
322 void allocateGlowmap(int size);
323 bool allocatePixelBuffer(uint32_t *& pixels, int width, int height);
324 void deallocate();
325
326 void renderChunk(Context & state, int x1, int x2);
327 void renderConcurrent();
328
329 void fogSunMash();
330 void wallsMash();
331 void lightsMash();
332 void glowmapChunk(__m128 * gm, bool still, int z1, int z2);
333 void glowmapConcurrent(__m128 * gm, bool still);
334 void texturesMash();
335
336 void findFloorCeilings(Context & state);
337 void renderVertical(Context & state, int depth, QList<float> stackBelow, QList<float> stackAbove, int scanTop, int scanBot);
338 void drawVStrips(Context & state);
339
340 void vstripAdd(Context & state, const Strip & strip, int scanTop, int scanBot);
341 void vstripDraw(Context & state, const Strip & strip);
342 void surfaceDraw(Context & state, const Wall & w, uint16_t sID, int scanTop, int scanBot, float height);
343
344 void buildTables();
345
346 void sceneBoundsInit();
347 void sceneBoundsRegister(float x, float z);
348 void sceneGetCoords(const float x, const float z, float & xs, float & zs);
349 void worldGetCoords(const float xs, const float zs, float & x, float & z);
350
351 __m128 sampleGlowmap(float gx, float gy);
352
353 void configInit();
354 bool configLoad(const QString & filename);
355 bool configSave(const QString & filename);
356};
357
358extern Renderer renderer;
359
360#endif // RENDERER_H
Definition renderer.h:60
static constexpr int DefaultFrameResoX
Definition renderer.h:62
static constexpr int DefaultGlowmapReso
Definition renderer.h:64
Node * nodes
Definition renderer.h:200
int nodesAllocated
Definition renderer.h:202
static constexpr float GlowBleedFactor
Definition renderer.h:72
float fovDistanceFar
far clip distance, in world units
Definition renderer.h:172
void clickRegister(int x, int y)
Register a frame position to pick a wall during the next render.
Definition renderer.cpp:220
void setGlowmapArea(float area)
Set the world area covered by the glowmap, in world units.
Definition renderer.cpp:96
void setFOVAngle(float angle)
Set the horizontal field of view, in degrees.
Definition renderer.cpp:110
Wall * walls
Definition renderer.h:204
void init()
Allocate the object pools, frames and tables, load the config.
Definition renderer.cpp:49
RENDERER_FLAGS getFlags() const
Definition renderer.h:140
int lightsAllocated
Definition renderer.h:214
float occlusionDarken
darkening strength (0.0 .. 1.0)
Definition renderer.h:189
int lightsCount
Definition renderer.h:213
Light * lights
Definition renderer.h:212
static constexpr float GlowSampleDistance
Definition renderer.h:71
int getFrameResoX() const
Definition renderer.h:152
static constexpr int DefaultWallsMax
Definition renderer.h:67
void setGlowmapSize(int size)
Request a glowmap resize (applied at the start of the next render).
Definition renderer.cpp:90
float fovAngle
horizontal FOV, in degrees
Definition renderer.h:170
void render(Viewpoint &vp)
Render the scene to the internal frame (see getImage).
Definition renderer.cpp:437
void setFOVDistanceNear(float distance)
Definition renderer.cpp:116
uint16_t clickGetWallID() const
Wall picked by the last registered click (see clickRegister).
Definition renderer.h:164
static constexpr uint16_t ClickNoWall
Sentinel returned by clickGetWallID when no wall was picked.
Definition renderer.h:79
void setFOVDistanceFar(float distance)
Definition renderer.cpp:121
void checkFlag(RENDERER_FLAGS flag, bool checked)
Set or clear a single renderer flag.
Definition renderer.cpp:83
float fogDistanceNear
fog start distance, in world units
Definition renderer.h:182
int texturesAllocated
Definition renderer.h:210
float getGlowmapArea() const
Definition renderer.h:148
float gammaKBlue
blue smoothstep tone gain
Definition renderer.h:197
QVector3D sunRayDirection
normalised ray direction
Definition renderer.h:177
float motionBlurFactor
previous frame blend, in percent (0 .. 100)
Definition renderer.h:192
int getFrameResoY() const
Definition renderer.h:153
float sunRayStrength
ray intensity (0.0 .. 1.0)
Definition renderer.h:179
Renderer()
Definition renderer.cpp:33
static constexpr int DefaultLightsMax
Definition renderer.h:69
void setFramesReso(int width, int height)
Request a frame resize (applied at the start of the next render).
Definition renderer.cpp:102
uint32_t fogFarColor
packed RGB fog color
Definition renderer.h:184
float fogDistanceFar
fog full-density distance, in world units
Definition renderer.h:183
static constexpr int DefaultTexturesMax
Definition renderer.h:68
static constexpr float DefaultGlowmapArea
Definition renderer.h:65
float lastFloor
Definition renderer.h:217
uint32_t sunRayColor
packed RGB direct ray color
Definition renderer.h:176
void terminate()
Release all the renderer resources.
Definition renderer.cpp:71
int nodesCount
Definition renderer.h:201
float vignetteOuterRadius
full black, in percent of half the frame height
Definition renderer.h:194
float fovDistanceNear
near clip distance, in world units
Definition renderer.h:171
int wallsAllocated
Definition renderer.h:206
static constexpr int DefaultNodesMax
Definition renderer.h:66
float vignetteInnerRadius
darkening start, in percent of half the frame height
Definition renderer.h:193
static constexpr float HeightMin
Definition renderer.h:76
void setFlags(RENDERER_FLAGS flags)
Replace the whole RENDERER_FLAGS bitmask.
Definition renderer.cpp:78
uint16_t fogFlags
combination of FOG_FLAGS
Definition renderer.h:185
float gammaKGreen
green smoothstep tone gain
Definition renderer.h:196
int wallsCount
Definition renderer.h:205
int texturesCount
Definition renderer.h:209
float sunAmbientStrength
ambient intensity (0.0 .. 1.0)
Definition renderer.h:178
static constexpr float HeightMax
Definition renderer.h:75
Texture * textures
Definition renderer.h:208
int getGlowmapSize() const
Definition renderer.h:144
float occlusionLength
corner darkening reach, in world units
Definition renderer.h:188
float lastCeiling
Definition renderer.h:218
uint32_t sunAmbient
packed RGB ambient color
Definition renderer.h:175
static constexpr int DefaultFrameResoY
Definition renderer.h:63
float gammaKRed
red smoothstep tone gain (1.0 = full curve)
Definition renderer.h:195
static constexpr float LightFallOff
Definition renderer.h:73
uint32_t flags
active RENDERER_FLAGS bitmask
Definition renderer.h:167
QImage * getImage() const
Frame rendered by the last render() call.
Definition renderer.h:133
static constexpr unsigned WorkersMax
Definition workerpool.h:28
@ WALL_SURFACES_COUNT
Definition mapobjects.h:61
Renderer renderer
Definition renderer.cpp:27
RENDERER_FLAGS
Definition renderer.h:29
@ RENDERER_FLAGS_DEFAULT
Definition renderer.h:40
@ RENDERER_FLAG_GAMMA
Definition renderer.h:37
@ RENDERER_FLAG_MOTIONBLUR
Definition renderer.h:35
@ RENDERER_FLAG_ALPHA_FEATURES
Definition renderer.h:39
@ RENDERER_FLAG_VIGNETTE
Definition renderer.h:36
@ RENDERER_FLAG_LIGHTS
Definition renderer.h:32
@ RENDERER_FLAG_AMBIENT_OCCLUSION
Definition renderer.h:33
@ RENDERER_FLAG_SURFACES
Definition renderer.h:31
@ RENDERER_FLAG_GLOWMAP_REBUILD
Definition renderer.h:34
@ RENDERER_FLAG_MULTITHREADING
Definition renderer.h:38
@ RENDERER_FLAG_WALLS
Definition renderer.h:30
constexpr float D2R
Definition renderer.h:55
constexpr float R2D
Definition renderer.h:56
Renderer-side light (compact variant, fed by Map::pass).
Definition renderer.h:112
float strength
Definition renderer.h:115
float z
Definition renderer.h:116
__m128 argb
Definition renderer.h:117
float x
Definition renderer.h:116
uint32_t color
Definition renderer.h:114
bool still
Definition renderer.h:118
uint16_t nodeID
Definition renderer.h:113
Renderer-side node (compact variant, fed by Map::pass).
Definition renderer.h:82
uint16_t flags
Definition renderer.h:84
QVector3D pos
Definition renderer.h:83
Renderer-side texture strip (column of square tiles).
Definition renderer.h:103
uint16_t count
Definition renderer.h:106
uint16_t size
Definition renderer.h:105
uint16_t mask
Definition renderer.h:107
uint32_t block
Definition renderer.h:108
uint32_t * pixels
Definition renderer.h:104
Renderer-side wall (compact variant, fed by Map::pass).
Definition renderer.h:88
uint16_t nodeID2
Definition renderer.h:90
float height
Definition renderer.h:91
uint32_t rayFront
Definition renderer.h:98
uint16_t flags
Definition renderer.h:96
uint32_t rayBack
Definition renderer.h:99
uint16_t textureID
Definition renderer.h:95
uint16_t nodeID1
Definition renderer.h:89
QVector3D offset
Definition renderer.h:93
Surface surfaces[WALL_SURFACES_COUNT]
Definition renderer.h:94
Texture mapping of one face of a map object.
Definition mapobjects.h:46
Camera position and orientation.
Definition renderer.h:47
QVector3D pos
Definition renderer.h:48
float pan
Definition renderer.h:50
float tilt
Definition renderer.h:51
QVector3D offset
Definition renderer.h:49