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
#include "Headers/ParticleEmitter.h"

#include "Core/Headers/Application.h"
#include "Platform/Video/Headers/GFXDevice.h"
#include "Core/Resources/Headers/ResourceCache.h"
#include "Core/Headers/ParamHandler.h"
#include "Graphs/Headers/SceneGraphNode.h"
#include "Core/Math/Headers/Transform.h"
#include "Scenes/Headers/SceneState.h"
#include "Geometry/Material/Headers/Material.h"
#include "Platform/Video/Buffers/Framebuffer/Headers/Framebuffer.h"
#include "Platform/Video/Buffers/VertexBuffer/Headers/GenericVertexData.h"

namespace Divide {

namespace {
    U32 _readOffset = 0;
    U32 _writeOffset = 2;
}

ParticleEmitter::ParticleEmitter()
    : SceneNode(SceneNodeType::TYPE_PARTICLE_EMITTER),
      _drawImpostor(false),
      _particleStateBlockHash(0),
      _particleStateBlockHashDepth(0),
      _enabled(false),
      _uploaded(false),
      _particleTexture(nullptr),
      _particleShader(nullptr),
      _particleGPUBuffer(nullptr),
      _particleDepthShader(nullptr) {
    _readOffset = 0;
    _writeOffset = 2;
}

ParticleEmitter::~ParticleEmitter() { 
    unload(); 
}

bool ParticleEmitter::initData(std::shared_ptr<ParticleData> particleData) {
    // assert if double init!
    DIVIDE_ASSERT(_particleGPUBuffer == nullptr,
                  "ParticleEmitter::initData error: Double initData detected!");

    _particleGPUBuffer = GFX_DEVICE.newGVD(true);
    _particleGPUBuffer->create(3);

    // Not using Quad3D to improve performance
    static F32 particleQuad[] = {
        -0.5f, -0.5f, 0.0f, 
         0.5f, -0.5f, 0.0f,
        -0.5f,  0.5f, 0.0f,
         0.5f,  0.5f, 0.0f,
    };

    _particleGPUBuffer->setBuffer(0, 12, sizeof(F32), 1, particleQuad, false,
                                  false, true);
    _particleGPUBuffer->getDrawAttribDescriptor(
                            to_uint(
                                AttribLocation::VERTEX_POSITION))
        .set(0, 0, 3, false, 0, 0, GFXDataFormat::FLOAT_32);

    updateData(particleData);

    // Generate a render state
    RenderStateBlock particleRenderState;
    particleRenderState.setCullMode(CullMode::NONE);
    particleRenderState.setBlend(true, BlendProperty::SRC_ALPHA,
                                  BlendProperty::INV_SRC_ALPHA);
    particleRenderState.setZWrite(false);
    particleRenderState.setZFunc(ComparisonFunction::LEQUAL);
    _particleStateBlockHash = particleRenderState.getHash();

    particleRenderState.setZFunc(ComparisonFunction::LESS);
    _particleStateBlockHashDepth = particleRenderState.getHash();

    bool useTexture = _particleTexture != nullptr;
    ResourceDescriptor particleShaderDescriptor(useTexture ? "particles.WithTexture" : "particles.NoTexture");
    if (useTexture){
        particleShaderDescriptor.setPropertyList("HAS_TEXTURE");
    }
    _particleShader = CreateResource<ShaderProgram>(particleShaderDescriptor);
    REGISTER_TRACKED_DEPENDENCY(_particleShader);

    ResourceDescriptor particleDepthShaderDescriptor("particles.Depth");
    _particleDepthShader = CreateResource<ShaderProgram>(particleDepthShaderDescriptor);
    REGISTER_TRACKED_DEPENDENCY(_particleDepthShader);

    //_renderState.addToDrawExclusionMask(RenderStage::SHADOW);

    return (_particleShader != nullptr);
}

bool ParticleEmitter::updateData(std::shared_ptr<ParticleData> particleData) {
    DIVIDE_ASSERT(particleData.get() != nullptr,
                  "ParticleEmitter::updateData error: Invalid particle data!");

    _particles = particleData;

    U32 particleCount = _particles->totalCount();

    _particleGPUBuffer->setBuffer(1, particleCount * 3, 4 * sizeof(F32), 1, NULL, true, true, true);
    _particleGPUBuffer->setBuffer(2, particleCount * 3, 4 * sizeof(U8), 1, NULL, true, true, true);

    _particleGPUBuffer->getDrawAttribDescriptor(13)
        .set(1, 1, 4, false, 4 * sizeof(F32), 0, GFXDataFormat::FLOAT_32);
    _particleGPUBuffer->getDrawAttribDescriptor(to_uint(AttribLocation::VERTEX_COLOR))
        .set(2, 1, 4, true, 4 * sizeof(U8), 0, GFXDataFormat::UNSIGNED_BYTE);

    for (U32 i = 0; i < particleCount; ++i) {
        // Distance to camera (squared)
        _particles->_misc[i].w = -1.0f;
    }

    if (_particleTexture) {
        UNREGISTER_TRACKED_DEPENDENCY(_particleTexture);
        RemoveResource(_particleTexture);
    }

    if (!_particles->_textureFileName.empty()) {
        SamplerDescriptor textureSampler;
        textureSampler.toggleSRGBColorSpace(true);

        ResourceDescriptor texture(_particles->_textureFileName);

        texture.setResourceLocation(
            ParamHandler::getInstance().getParam<stringImpl>("assetsLocation") + "/" +
            ParamHandler::getInstance().getParam<stringImpl>("defaultTextureLocation") + "/" +
            _particles->_textureFileName);

        texture.setPropertyDescriptor<SamplerDescriptor>(textureSampler);
        texture.setEnumValue(to_const_uint(TextureType::TEXTURE_2D));
        _particleTexture = CreateResource<Texture>(texture);

        REGISTER_TRACKED_DEPENDENCY(_particleTexture);
    }

    return true;
}

bool ParticleEmitter::unload() {
    if (getState() != ResourceState::RES_LOADED &&
        getState() != ResourceState::RES_LOADING) {
        return true;
    }
    if (_particleTexture) {
        UNREGISTER_TRACKED_DEPENDENCY(_particleTexture);
    }
    UNREGISTER_TRACKED_DEPENDENCY(_particleShader);
    UNREGISTER_TRACKED_DEPENDENCY(_particleDepthShader);
    RemoveResource(_particleTexture);
    RemoveResource(_particleShader);
    RemoveResource(_particleDepthShader);

    MemoryManager::DELETE(_particleGPUBuffer);

    _particles.reset();

    return SceneNode::unload();
}

void ParticleEmitter::postLoad(SceneGraphNode& sgn) {
    Framebuffer* depthBuffer = GFX_DEVICE.getRenderTarget(GFXDevice::RenderTarget::DEPTH);
    Texture* depthTexture = depthBuffer->getAttachment(TextureDescriptor::AttachmentType::Depth);
    TextureData depthBufferData = depthTexture->getData();
    depthBufferData.setHandleLow(to_uint(ShaderProgram::TextureUsage::DEPTH));
    sgn.getComponent<RenderingComponent>()->registerTextureDependency(depthBufferData);

    if (_particleTexture && _particleTexture->flushTextureState()) {
        TextureData particleTextureData = _particleTexture->getData();
        particleTextureData.setHandleLow(to_uint(ShaderProgram::TextureUsage::UNIT0));
        sgn.getComponent<RenderingComponent>()->registerTextureDependency(particleTextureData);
    }


    RenderingComponent* const renderable = sgn.getComponent<RenderingComponent>();
    assert(renderable != nullptr);

    GenericDrawCommand cmd(PrimitiveType::TRIANGLE_STRIP, 0, 4, 1);
    cmd.sourceBuffer(_particleGPUBuffer);
    for (U32 i = 0; i < to_uint(RenderStage::COUNT); ++i) {
        GFXDevice::RenderPackage& pkg = 
            Attorney::RenderingCompSceneNode::getDrawPackage(*renderable, static_cast<RenderStage>(i));
            pkg._drawCommands.push_back(cmd);
    }
    sgn.lockBBTransforms(true);

    SceneNode::postLoad(sgn);
}


void ParticleEmitter::onCameraUpdate(SceneGraphNode& sgn, Camera& camera) {

    vec3<F32> up(camera.getUpDir());
    vec3<F32> right(camera.getRightDir());

    if (_camUp != up ) {
        _camUp.set(up);
        _particleShader->Uniform("CameraUp_worldspace", up);
        _particleDepthShader->Uniform("CameraUp_worldspace", up);
    }

    if (_camRight != right) {
        _camRight.set(right);
        _particleShader->Uniform("CameraRight_worldspace", right);
        _particleDepthShader->Uniform("CameraRight_worldspace", right);
    }
}

bool ParticleEmitter::getDrawCommands(SceneGraphNode& sgn,
                                      RenderStage renderStage,
                                      const SceneRenderState& sceneRenderState,
                                      vectorImpl<GenericDrawCommand>& drawCommandsOut) {

    U32 particleCount = getAliveParticleCount();
    if (!_enabled || particleCount == 0) {
        return false;
    }

    GenericDrawCommand& cmd = drawCommandsOut.front();

    RenderingComponent* renderable = sgn.getComponent<RenderingComponent>();

    cmd.renderGeometry(renderable->renderGeometry());
    cmd.renderWireframe(renderable->renderWireframe());
    cmd.cmd().primCount = particleCount;

    if (GFX_DEVICE.isDepthStage()) {
        cmd.stateHash(_particleStateBlockHash);
        cmd.shaderProgram(_particleDepthShader);
    } else {
        cmd.stateHash(_particleStateBlockHashDepth);
        cmd.shaderProgram( _particleShader);
    }
    return SceneNode::getDrawCommands(sgn, renderStage, sceneRenderState, drawCommandsOut);
}

void ParticleEmitter::uploadToGPU() {
    if (_uploaded || getAliveParticleCount() == 0) {
        return;
    }
    
    U32 writeOffset = _writeOffset * to_uint(_particles->totalCount());
    U32 readOffset = _readOffset * to_uint(_particles->totalCount());

    _particleGPUBuffer->updateBuffer(1, _particles->aliveCount(), writeOffset,
                                        _particles->_renderingPositions.data());
    _particleGPUBuffer->updateBuffer(2, _particles->aliveCount(), writeOffset,
                                        _particles->_renderingColors.data());

    _particleGPUBuffer->getDrawAttribDescriptor(13)
        .set(1, 1, 4, false, 4 * sizeof(F32), readOffset, GFXDataFormat::FLOAT_32);
    _particleGPUBuffer->getDrawAttribDescriptor(to_uint(AttribLocation::VERTEX_COLOR))
        .set(2, 1, 4, true, 4 * sizeof(U8), readOffset, GFXDataFormat::UNSIGNED_BYTE);

    _writeOffset = (_writeOffset + 1) % 3;
    _readOffset = (_readOffset + 1) % 3;

    _uploaded = true;
}

/// The onDraw call will emit particles
bool ParticleEmitter::onDraw(SceneGraphNode& sgn,
                             RenderStage currentStage) {
    if (_enabled) {
        uploadToGPU();
        return true;
    }

    return false;
}

/// Pre-process particles
void ParticleEmitter::sceneUpdate(const U64 deltaTime,
                                  SceneGraphNode& sgn,
                                  SceneState& sceneState) {
    if (!_enabled && !_uploaded) {
        return;
    }

    bool validCount = getAliveParticleCount() > 0;
    renderState().setDrawState(validCount);

    _uploaded = false;

    PhysicsComponent* transform = sgn.getComponent<PhysicsComponent>();
    const vec3<F32>& eyePos = sceneState.renderState().getCameraConst().getEye();

    const vec3<F32>& pos = transform->getPosition();
    const Quaternion<F32>& rot = transform->getOrientation();

    F32 averageEmitRate = 0;
    for (std::shared_ptr<ParticleSource>& source : _sources) {
        source->updateTransform(pos, rot);
        source->emit(deltaTime, _particles);
        averageEmitRate += source->emitRate();
    }

    averageEmitRate /= _sources.size();

    U32 count = _particles->totalCount();
    for (U32 i = 0; i < count; ++i) {
        _particles->_misc[i].w =  _particles->_position[i].xyz().distanceSquared(eyePos);
        _particles->_position[i].w = 1.0f * _particles->_misc[i].z;
        _particles->_acceleration[i].set(0.0f);
    }

    for (std::shared_ptr<ParticleUpdater>& up : _updaters) {
        up->update(deltaTime, _particles);
    }

    // const vec3<F32>& origin = transform->getPosition();
    // const Quaternion<F32>& orientation = transform->getOrientation();

    _particles->sort();

    _boundingBox.first.reset();
    U32 aliveCount = _particles->aliveCount();
    for (U32 i = 0; i < aliveCount; i += to_uint(averageEmitRate) / 4) {
        _boundingBox.first.add(_particles->_position[i]);
    }
    _boundingBox.second = true;

    SceneNode::sceneUpdate(deltaTime, sgn, sceneState);
}

U32 ParticleEmitter::getAliveParticleCount() const {
    if (!_particles.get()) {
        return 0;
    }
    return _particles->aliveCount();
}
};

Commits for Divide-Framework/trunk/Source Code/Dynamics/Entities/Particles/ParticleEmitter.cpp

Diff revisions: vs.
Revision Author Commited Message
612 Diff Diff IonutCava picture IonutCava Sun 20 Dec, 2015 17:47:26 +0000

[IonutCava]
- Z-PrePass system improvements
— Depth only rendertarget and both eye render targets (screen and anaglyph) share the same depth buffer
— Screen render targets don’t clear or write the/to the depth buffer

610 Diff Diff IonutCava picture IonutCava Fri 18 Dec, 2015 13:25:07 +0000

[IonutCava]
- Z_PRE_PASS groundwork

600 Diff Diff IonutCava picture IonutCava Tue 08 Dec, 2015 17:22:11 +0000

[IonutCava]
- Cleanup SceneGraphNode class
- Rework AABB calculation and update system and fix various AABB problems (e.g. light AABB, parent mesh AABB, etc)
- Create a list of closest shadow casting lights for faster shadow map generation
- Texture creation bug fixed (related to descriptor enum value)
- Better FPS and Frametime calculation system

599 Diff Diff IonutCava picture IonutCava Mon 07 Dec, 2015 22:31:39 +0000

[IonutCava]
- Bounding box calculation updates for particle systems and skybox
- Small optimization for the particle shader

598 Diff Diff IonutCava picture IonutCava Mon 07 Dec, 2015 17:21:28 +0000

[IonutCava]
- Transform caching updates
- Light impostor rendering updates
- Temporarily disable persistent buffer mapping

593 Diff Diff IonutCava picture IonutCava Thu 03 Dec, 2015 17:17:30 +0000

[IonutCava]
- Culling, light rendering, physics transforms and impostor updates

591 Diff Diff IonutCava picture IonutCava Wed 02 Dec, 2015 17:17:23 +0000

[IonutCava]
- More work on the ForwardPlus renderer
- Singleton cleanup
- First steps for reducing scope of GFXDevice class

589 Diff Diff IonutCava picture IonutCava Tue 01 Dec, 2015 13:51:59 +0000

[IonutCava]
- Cleanup some indirections to improve cache locality

572 Diff Diff IonutCava picture IonutCava Wed 18 Nov, 2015 17:15:43 +0000

[Ionut]
- Attempt to pack all shadowmaps in Shadowmap arrays
- Cleanup lighting and material info
- Fix light impostor rendering

569 IonutCava picture IonutCava Mon 16 Nov, 2015 23:25:04 +0000

[IonutCava]
- Work on particle transform
- Work on multiple omni-light rendering