Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
   Copyright (c) 2016 DIVIDE-Studio
   Copyright (c) 2009 Ionut Cava

   This file is part of DIVIDE Framework.

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software
   and associated documentation files (the "Software"), to deal in the Software
   without restriction,
   including without limitation the rights to use, copy, modify, merge, publish,
   distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so,
   subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED,
   INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
   PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
   DAMAGES OR OTHER LIABILITY,
   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
   IN CONNECTION WITH THE SOFTWARE
   OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 */

#ifndef _RENDER_API_H_
#define _RENDER_API_H_

#include "config.h"

#include "Core/Math/Headers/MathMatrices.h"
#include "Platform/Video/Headers/RenderStateBlock.h"

#include <thread>

namespace Divide {

class Kernel;
class Light;
class SubMesh;

class Material;
class Object3D;
class TextLabel;
class Transform;
class GUIElement;
class SceneRenderState;
enum class ErrorCode : I32;

template <typename T>
class Plane;
typedef vectorImpl<Plane<F32> > PlaneList;
class SceneGraph;

class GenericVertexData;
class VertexDataInterface;

FWD_DECLARE_MANAGED_CLASS(ShaderProgram);

typedef struct {
    // Video resolution
    I32 Width, Height;
    // Red bits per pixel
    I32 RedBits;
    // Green bits per pixel
    I32 GreenBits;
    // Blue bits per pixel
    I32 BlueBits;
} VideoModes;
// FWD DECLARE CLASSES

enum class RenderAPI : U32 {
    OpenGL,    ///< 4.x+
    OpenGLES,  ///< 3.x+
    Direct3D,  ///< 12.x+ (not supported yet)
    Vulkan,    ///< not supported yet
    None,      ///< not supported yet
    COUNT
};

struct IndirectDrawCommand {
    IndirectDrawCommand()
        : indexCount(0),
          primCount(1),
          firstIndex(0),
          baseVertex(0),
          baseInstance(0)
    {
    }

    U32 indexCount;    // 4  bytes
    U32 primCount;     // 8  bytes
    U32 firstIndex;    // 12 bytes
    U32 baseVertex;    // 20 bytes
    U32 baseInstance;  // 24 bytes

    inline void set(const IndirectDrawCommand& other) {
        indexCount = other.indexCount;
        primCount = other.primCount;
        firstIndex = other.firstIndex;
        baseVertex = other.baseVertex;
        baseInstance = other.baseInstance;
    }

    bool operator==(const IndirectDrawCommand &other) const {
        return  indexCount == other.indexCount &&
                primCount == other.primCount &&
                firstIndex == other.firstIndex &&
                baseVertex == other.baseVertex &&
                baseInstance == other.baseInstance;
    }
};

struct GenericDrawCommand {
   public:
    enum class RenderOptions : U32 {
        RENDER_GEOMETRY = toBit(1),
        RENDER_WIREFRAME = toBit(2),
        RENDER_BOUNDS = toBit(3),
        COUNT = 3
    };

   private:
    // order is important to avoid padding for alignment reasons
    // state hash is not size_t to avoid any platform specific awkward typedefing
    U32 __padding__reserved__;               // 64 bytes
    IndirectDrawCommand _cmd;                // 60 bytes
    ShaderProgram* _shaderProgram;           // 40 bytes
    VertexDataInterface* _sourceBuffer;      // 32 bytes
    PrimitiveType _type;                     // 24 bytes
    U64 _stateHash;                          // 20 bytes
    U32 _commandOffset;                      // 12 bytes
    U32 _renderOptions;                      // 8  bytes
    U16 _drawCount;                          // 4  bytes
    U8 _drawToBuffer;                        // 2  bytes
    U8 _lodIndex;                            // 1  bytes
   public:

    inline void drawCount(U16 count) { 
        _drawCount = count; 
    }

    inline void LoD(U8 lod) {
        _lodIndex = lod;
    }

    inline void stateHash(size_t hashValue) {
        _stateHash = static_cast<U64>(hashValue);
    }

    inline void drawToBuffer(U8 index) {
        _drawToBuffer = index;
    }

    inline void renderWireframe(bool state) {
        state ? SetBit(_renderOptions, to_const_uint(RenderOptions::RENDER_WIREFRAME))
              : ClearBit(_renderOptions, to_const_uint(RenderOptions::RENDER_WIREFRAME));
    }

    inline void renderBounds(bool state) {
        state ? SetBit(_renderOptions, to_const_uint(RenderOptions::RENDER_BOUNDS))
              : ClearBit(_renderOptions, to_const_uint(RenderOptions::RENDER_BOUNDS));
    }

    inline void renderGeometry(bool state) {
        state ? SetBit(_renderOptions, to_const_uint(RenderOptions::RENDER_GEOMETRY))
              : ClearBit(_renderOptions, to_const_uint(RenderOptions::RENDER_GEOMETRY));
    }

    inline void shaderProgram(const ShaderProgram_ptr& program) {
        _shaderProgram = program.get();
    }

    inline void sourceBuffer(VertexDataInterface* const sourceBuffer) {
        _sourceBuffer = sourceBuffer;
    }

    inline void primitiveType(PrimitiveType type) {
        _type = type;
    }

    inline void commandOffset(U32 offset) {
        _commandOffset = offset;
    }

    inline U8 LoD() const { return _lodIndex; }
    inline U16 drawCount() const { return _drawCount; }
    inline size_t stateHash() const { return static_cast<size_t>(_stateHash); }
    inline U8  drawToBuffer() const { return _drawToBuffer; }
    inline U32 commandOffset() const { return _commandOffset; }

    inline bool renderWireframe() const {
        return BitCompare(_renderOptions, to_const_uint(RenderOptions::RENDER_WIREFRAME));
    }
    inline bool renderGeometry() const {
        return BitCompare(_renderOptions, to_const_uint(RenderOptions::RENDER_GEOMETRY));
    }
    inline bool renderBounds() const {
        return BitCompare(_renderOptions, to_const_uint(RenderOptions::RENDER_BOUNDS));
    }

    inline const IndirectDrawCommand& cmd() const {
        return _cmd;
    }

    inline IndirectDrawCommand& cmd() {
        return _cmd;
    }

    inline ShaderProgram* shaderProgram() const { return _shaderProgram; }
    inline VertexDataInterface* sourceBuffer() const { return _sourceBuffer; }
    inline PrimitiveType primitiveType() const { return _type; }

    GenericDrawCommand()
        : GenericDrawCommand(PrimitiveType::TRIANGLE_STRIP, 0, 0)
    {
    }

    GenericDrawCommand(const PrimitiveType& type, U32 firstIndex, U32 indexCount,
                       U32 primCount = 1)
        : _lodIndex(0),
          _drawCount(1),
          _drawToBuffer(0),
          _stateHash(0),
    	  _type(type),
          _commandOffset(0),
          _shaderProgram(nullptr),
          _sourceBuffer(nullptr),
          __padding__reserved__(0)
    {
        SetBit(_renderOptions, to_const_uint(RenderOptions::RENDER_GEOMETRY));
        _cmd.indexCount = indexCount;
        _cmd.firstIndex = firstIndex;
        _cmd.primCount = primCount;

        static_assert(sizeof(IndirectDrawCommand) == 20, "Size of IndirectDrawCommand is incorrect!");
    }

    inline void set(const GenericDrawCommand& base) {
        _cmd.set(base._cmd);
        _lodIndex = base._lodIndex;
        _drawCount = base._drawCount;
        _drawToBuffer = base._drawToBuffer;
        _renderOptions = base._renderOptions;
        _stateHash = base._stateHash;
        _type = base._type;
        _shaderProgram = base._shaderProgram;
        _sourceBuffer = base._sourceBuffer;
        _commandOffset = base._commandOffset;
    }

    inline void reset() {
        set(GenericDrawCommand());
    }

    inline bool compatible(const GenericDrawCommand& other) const {
        return _lodIndex == other._lodIndex &&
               _drawToBuffer == other._drawToBuffer &&
               _renderOptions == other._renderOptions &&
               _stateHash == other._stateHash && _type == other._type &&
               (_shaderProgram != nullptr) == (other._shaderProgram != nullptr) &&
               (_sourceBuffer != nullptr) == (other._sourceBuffer != nullptr);
    }
};

class TextureData {
    public:
    TextureData()
        : _textureType(TextureType::TEXTURE_2D),
          _samplerHash(0),
          _textureHandle(0)
    {
    }

    TextureData(const TextureData& other)
        : _textureType(other._textureType),
          _samplerHash(other._samplerHash),
          _textureHandle(other._textureHandle)
    {
    }

    inline void set(const TextureData& other) {
        _textureHandle = other._textureHandle;
        _textureType = other._textureType;
        _samplerHash = other._samplerHash;
    }

    inline void setHandleHigh(U32 handle) {
        _textureHandle = (U64)handle << 32 | getHandleLow();
    }

    inline U32 getHandleHigh() const {
        return to_uint(_textureHandle >> 32);
    }

    inline void getHandleHigh(U32& handle) const {
        handle = getHandleHigh();
    }

    inline void setHandleLow(U32 handle) {
        _textureHandle |= handle;
    }

    inline U32 getHandleLow() const{
        return to_uint(_textureHandle);
    }
        
    inline void getHandleLow(U32& handle) const {
        handle = getHandleLow();
    }

    inline void setHandle(U64 handle) {
        _textureHandle = handle;
    }
        
    inline void getHandle(U64& handle) const {
        handle = _textureHandle;
    }

    // No need to cache this as it should already be pretty fast
    inline size_t getHash() const {
        size_t hash = 0;
        Util::Hash_combine(hash, to_uint(_textureType));
        Util::Hash_combine(hash, _samplerHash);
        Util::Hash_combine(hash, _textureHandle);
        return hash;
    }

    TextureType _textureType;
    size_t _samplerHash;
private:
    U64  _textureHandle;
};

typedef vectorImpl<TextureData> TextureDataContainer;
typedef std::array<IndirectDrawCommand, Config::MAX_VISIBLE_NODES> DrawCommandList;

enum class ShaderType : U32;
class Texture;
class TextureData;
class IMPrimitive;
class PixelBuffer;
class RenderTarget;
class ShaderBuffer;
class VertexBuffer;
class ShaderProgram;

class RingBuffer {
    public:
        explicit RingBuffer(U32 queueLength) : 
            _queueLength(std::max(queueLength, 1U))
        {
            _queueReadIndex = 0;
            _queueWriteIndex = _queueLength - 1;
        }

        virtual ~RingBuffer()
        {
        }

        const inline U32 queueLength() const { return _queueLength; }
        const inline U32 queueWriteIndex() const { return _queueWriteIndex; }
        const inline U32 queueReadIndex() const { return _queueReadIndex; }

        inline void incQueue() { 
            if (queueLength() > 1) {
                ++_queueWriteIndex %= _queueLength;
                ++_queueReadIndex  %= _queueLength;
            }
        }

        inline void decQueue() {
            if (queueLength() > 1) {
                --_queueWriteIndex %= _queueLength;
                --_queueReadIndex  %= _queueLength;
            }
        }

    private:
        const U32 _queueLength;
        U32 _queueReadIndex;
        U32 _queueWriteIndex;

};

/// Renderer Programming Interface
class NOINITVTABLE RenderAPIWrapper : private NonCopyable {
   protected:
    friend class GFXDevice;
    /*Application display frame*/
    /// Clear buffers, set default states, etc
    virtual void beginFrame() = 0;
    /// Clear shaders, restore active texture units, etc
    virtual void endFrame(bool swapBuffers) = 0;
    virtual IMPrimitive* newIMP(GFXDevice& context) const = 0;
    virtual RenderTarget* newRT(GFXDevice& context, bool multisampled) const = 0;
    virtual VertexBuffer* newVB(GFXDevice& context) const = 0;
    virtual ShaderBuffer* newSB(GFXDevice& context,
                                const U32 ringBufferLength = 1,
                                const bool unbound = false,
                                const bool persistentMapped = true,
                                BufferUpdateFrequency frequency =
                                    BufferUpdateFrequency::ONCE) const = 0;
    virtual GenericVertexData* newGVD(GFXDevice& context, 
                                      const U32 ringBufferLength = 1) const = 0;
    virtual PixelBuffer* newPB(GFXDevice& context,
                               const PBType& type = PBType::PB_TEXTURE_2D) const = 0;
    virtual Texture* newTexture(GFXDevice& context, const stringImpl& name, const stringImpl& resourceLocation, TextureType type, bool asyncLoad) const = 0;
    virtual ShaderProgram* newShaderProgram(GFXDevice& context, const stringImpl& name, const stringImpl& resourceLocation, bool asyncLoad) const = 0;

    virtual bool initShaders() = 0;
    virtual bool deInitShaders() = 0;

    virtual ErrorCode initRenderingAPI(I32 argc, char** argv) = 0;
    virtual void closeRenderingAPI() = 0;


    virtual void toggleDepthWrites(bool state) = 0;
    virtual void toggleRasterization(bool state) = 0;
    virtual void drawText(const TextLabel& textLabel,
                          const vec2<F32>& position) = 0;

    virtual void updateClipPlanes() = 0;
    virtual U64  getFrameDurationGPU() = 0;
    virtual void activateStateBlock(const RenderStateBlock& newBlock,
                                    const RenderStateBlock& oldBlock) const = 0;

    virtual void draw(const GenericDrawCommand& cmd) = 0;

   protected:
    virtual void changeViewport(const vec4<I32>& newViewport) const = 0;
    virtual void syncToThread(std::thread::id threadID) = 0;
    virtual void registerCommandBuffer(const ShaderBuffer& commandBuffer) const = 0;

    virtual bool makeTexturesResident(const TextureDataContainer& textureData) = 0;
    virtual bool makeTextureResident(const TextureData& textureData) = 0;
};

};  // namespace Divide

#endif

Commits for Divide-Framework/trunk/Source Code/Platform/Video/Headers/RenderAPIWrapper.h

Diff revisions: vs.
Revision Author Commited Message
727 Diff Diff IonutCava picture IonutCava Tue 14 Jun, 2016 16:01:38 +0000

[IonutCava]
- Remove node and cmd buffers from GFXDevice and add them as a new BufferData struct to RenderPass class.
— Each render pass holds its own buffers
- Improvements / fixes to the CSM code
- Added a global toggle to enable/disable custom memory allocators

726 Diff Diff IonutCava picture IonutCava Fri 10 Jun, 2016 16:13:52 +0000

[IonutCava]
- More static analysis based corrections
- Added initial stub code for a command pool system for GenericDrawCommands

725 Diff Diff IonutCava picture IonutCava Fri 10 Jun, 2016 12:44:46 +0000

[IonutCava]
- Removed GFX drawPoints and drawTriangle calls and replaced them with a single draw command that takes a GenericDrawCommand parameter.
- Updated CSM code. Shadows now render in the correct position again. Still has issues.

722 Diff Diff IonutCava picture IonutCava Thu 09 Jun, 2016 16:15:33 +0000

[IonutCava]
- Restructure RenderTarget system:
— Separate attachments, use an attachment pool, use draw descriptors, require explicit information for RT calls, etc

720 Diff Diff IonutCava picture IonutCava Mon 06 Jun, 2016 15:51:06 +0000

[IonutCava]
- PVS-Studio guided static analysis fixes and optimizations
- Added a flag to SceneState to bypass the Save/Load system not needed for certain types of scenes (menus, cinematics, etc)

718 Diff Diff IonutCava picture IonutCava Thu 02 Jun, 2016 16:02:48 +0000

[IonutCava]
- Code cleanup:
— Rename Framebuffer to RenderTarget. Only OpenGL uses the Framebuffer nomenclature.
— Remove base Shader class as only OpenGL will have separate shaders (and eventually move to pipeline objects) as Direct3D uses FX files and may end up with different structure
— Remove drawBox3D, drawSphere3D and drawLines from GFXDevice class and add them as member functions to the IMPrimitive class (renamed to fromXYZ)
— Pull some elements from SceneManager and move them to standalone classes (W.I.P.)

714 Diff Diff IonutCava picture IonutCava Thu 26 May, 2016 16:26:23 +0000

[IonutCava]
- Initial code for multithreaded scene load:
— Tasks can have a “sync with gpu” flag that will cause them to use a shared context (so they can call GL functions)
- Added per scene GUI lists that get passed to the main GUI class (still need a base class to hold the map and accessors to avoid code duplication)
- Re-enabled threading unit tests

712 Diff Diff IonutCava picture IonutCava Tue 24 May, 2016 16:18:37 +0000

[IonutCava]
- Added the concept of “buffer” to be used by GenericVertexData and ShaderBuffer without caring if it’s persistent or not.
— Persistent buffers handle their own locking instead of relying on the parent class
- HiZ occlusion culling fixes and optimizations

710 Diff Diff IonutCava picture IonutCava Fri 20 May, 2016 16:24:40 +0000

[IonutCava]
- Code cleanup
- Initial work on Scene loading and unloading with and without unloading assets:
— Need to change AIManager from a Singleton to a per-scene component

709 IonutCava picture IonutCava Thu 19 May, 2016 16:21:46 +0000

[IonutCava]
- Massive rewrite of the resource system:
— ResourceCache now uses a map of weak pointers and passes shared pointers to the call point of CreateResource
— RemoveResource is no longer needed, but resource based smart pointers use a custom deleter that calls unload()
— Keep a shared_ptr of the resource in the entity that needs it and pass shared_ptr references from getter methods
-— All of this removed the need for calls to RemoveResource, REGISTER_TRACKED_DEPENDENCY and UNREGISTER_TRACKED_DEPENDENCY reducing the number of mutex locks and atomic exchanges
- Singleton cleanup:
— Removed ShaderManager singleton and merged it’s data and responsibilities in static methods in both ShaderProgram and Shader classes.
Explanation: Due to the complex interdependency chains in the system, copying, updating, tracking the cache in a thread safe way became a lot more slower than a straight forward smart pointer based implementation. (e.g. scene graph nodes have 3d objects and material instances, 3d objects have materials, materials have textures and shaders, etc)