Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions src/renderer/gl_engine/tvgGlEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ void GlEffect::update(RenderEffectGaussianBlur* effect, const Matrix& transform)
}


GlRenderTask* GlEffect::render(RenderEffectGaussianBlur* effect, GlRenderTarget* dstFbo, Array<GlRenderTargetPool*>& blendPool, const RenderRegion& vp, uint32_t voffset, uint32_t ioffset)
GlRenderTask* GlEffect::render(RenderEffectGaussianBlur* effect, GlRenderTarget* dstFbo, GlRenderTargetPool& pool, const RenderRegion& vp, uint32_t voffset, uint32_t ioffset)
{
if (!pBlurV) pBlurV = new GlProgram(EFFECT_VERTEX, GAUSSIAN_VERTICAL);
if (!pBlurH) pBlurH = new GlProgram(EFFECT_VERTEX, GAUSSIAN_HORIZONTAL);

// get current and intermidiate framebuffers
auto dstCopyFbo0 = blendPool[0]->getRenderTarget(vp);
auto dstCopyFbo1 = blendPool[1]->getRenderTarget(vp);
auto dstCopyFbo0 = pool.getRenderTarget();
auto dstCopyFbo1 = pool.getRenderTarget();

// add uniform data
float viewport[4] {(float)vp.min.x, (float)vp.min.y, (float)vp.max.x, (float)vp.max.y};
Expand All @@ -97,6 +97,8 @@ GlRenderTask* GlEffect::render(RenderEffectGaussianBlur* effect, GlRenderTarget*
task->vertTask->addBindResource(GlBindingResource{1, pBlurV->getUniformBlockIndex("Viewport"), gpuBuffer->getBufferId(), viewportOffset, sizeof(viewport)});
task->vertTask->addVertexLayout(GlVertexLayout{0, 2, 2 * sizeof(float), voffset});
task->vertTask->setDrawRange(ioffset, 6);
pool.freeRenderTarget(dstCopyFbo1);
pool.freeRenderTarget(dstCopyFbo0);

return task;
}
Expand Down Expand Up @@ -149,21 +151,19 @@ void GlEffect::update(RenderEffectDropShadow* effect, const Matrix& transform)
}


GlRenderTask* GlEffect::render(RenderEffectDropShadow* effect, GlRenderTarget* dstFbo, Array<GlRenderTargetPool*>& blendPool, const RenderRegion& vp, uint32_t voffset, uint32_t ioffset)
GlRenderTask* GlEffect::render(RenderEffectDropShadow* effect, GlRenderTarget* dstFbo, GlRenderTargetPool& pool, const RenderRegion& vp, uint32_t voffset, uint32_t ioffset)
{
if (!pBlurV) pBlurV = new GlProgram(EFFECT_VERTEX, GAUSSIAN_VERTICAL);
if (!pBlurH) pBlurH = new GlProgram(EFFECT_VERTEX, GAUSSIAN_HORIZONTAL);
if (!pDropShadow) pDropShadow = new GlProgram(EFFECT_VERTEX, EFFECT_DROPSHADOW);

// get current and intermidiate framebuffers
auto dstCopyFbo0 = blendPool[0]->getRenderTarget(vp);
auto dstCopyFbo1 = blendPool[1]->getRenderTarget(vp);
auto dstCopyFbo0 = pool.getRenderTarget();
auto dstCopyFbo1 = pool.getRenderTarget();

// add uniform data
float viewport[4] {(float)vp.min.x, (float)vp.min.y, (float)vp.max.x, (float)vp.max.y};
GlDropShadow* params = (GlDropShadow*)(effect->rd);
auto paramsOffset = gpuBuffer->push(params, sizeof(GlDropShadow), true);
auto viewportOffset = gpuBuffer->push(viewport, sizeof(viewport), true);

// create gaussian blur tasks
auto task = new GlEffectDropShadowTask(pDropShadow, dstFbo, dstCopyFbo0, dstCopyFbo1);
Expand All @@ -176,16 +176,16 @@ GlRenderTask* GlEffect::render(RenderEffectDropShadow* effect, GlRenderTarget* d
// horizontal blur task and geometry
task->horzTask = new GlRenderTask(pBlurH);
task->horzTask->addBindResource(GlBindingResource{0, pBlurH->getUniformBlockIndex("Gaussian"), gpuBuffer->getBufferId(), paramsOffset, sizeof(GlGaussianBlur)});
task->horzTask->addBindResource(GlBindingResource{1, pBlurH->getUniformBlockIndex("Viewport"), gpuBuffer->getBufferId(), viewportOffset, sizeof(viewport)});
task->horzTask->addVertexLayout(GlVertexLayout{0, 2, 2 * sizeof(float), voffset});
task->horzTask->setDrawRange(ioffset, 6);

// vertical blur task and geometry
task->vertTask = new GlRenderTask(pBlurV);
task->vertTask->addBindResource(GlBindingResource{0, pBlurV->getUniformBlockIndex("Gaussian"), gpuBuffer->getBufferId(), paramsOffset, sizeof(GlGaussianBlur)});
task->vertTask->addBindResource(GlBindingResource{1, pBlurV->getUniformBlockIndex("Viewport"), gpuBuffer->getBufferId(), viewportOffset, sizeof(viewport)});
task->vertTask->addVertexLayout(GlVertexLayout{0, 2, 2 * sizeof(float), voffset});
task->vertTask->setDrawRange(ioffset, 6);
pool.freeRenderTarget(dstCopyFbo1);
pool.freeRenderTarget(dstCopyFbo0);

return task;
}
Expand Down Expand Up @@ -259,7 +259,7 @@ void GlEffect::update(RenderEffectTritone* effect, const Matrix& transform)
}


GlRenderTask* GlEffect::render(RenderEffect* effect, GlRenderTarget* dstFbo, Array<GlRenderTargetPool*>& blendPool, const RenderRegion& vp, uint32_t voffset, uint32_t ioffset)
GlRenderTask* GlEffect::render(RenderEffect* effect, GlRenderTarget* dstFbo, GlRenderTargetPool& pool, const RenderRegion& vp, uint32_t voffset, uint32_t ioffset)
{
//common color replacement effects
GlProgram* program = nullptr;
Expand All @@ -275,7 +275,8 @@ GlRenderTask* GlEffect::render(RenderEffect* effect, GlRenderTarget* dstFbo, Arr
} else return nullptr;

// get current and intermidiate framebuffers
auto dstCopyFbo = blendPool[0]->getRenderTarget(vp);
auto dstCopyFbo = pool.getRenderTarget();


// add uniform data
auto params = (GlEffectParams*)(effect->rd);
Expand All @@ -287,6 +288,7 @@ GlRenderTask* GlEffect::render(RenderEffect* effect, GlRenderTarget* dstFbo, Arr
task->addBindResource(GlBindingResource{0, program->getUniformBlockIndex("Params"), gpuBuffer->getBufferId(), paramsOffset, sizeof(GlEffectParams)});
task->addVertexLayout(GlVertexLayout{0, 2, 2 * sizeof(float), voffset});
task->setDrawRange(ioffset, 6);
pool.freeRenderTarget(dstCopyFbo);

return task;
}
Expand Down Expand Up @@ -319,7 +321,7 @@ bool GlEffect::region(RenderEffect* effect)
}


bool GlEffect::render(RenderEffect* effect, GlRenderPass* pass, Array<GlRenderTargetPool*>& blendPool)
bool GlEffect::render(RenderEffect* effect, GlRenderPass* pass, GlRenderTargetPool& pool)
{
if (pass->isEmpty()) return false;
auto vp = pass->getViewport();
Expand All @@ -332,11 +334,11 @@ bool GlEffect::render(RenderEffect* effect, GlRenderPass* pass, Array<GlRenderTa
GlRenderTask* output = nullptr;

if (effect->type == SceneEffect::GaussianBlur) {
output = render(static_cast<RenderEffectGaussianBlur*>(effect), pass->getFbo(), blendPool, vp, voffset, ioffset);
output = render(static_cast<RenderEffectGaussianBlur*>(effect), pass->getFbo(), pool, vp, voffset, ioffset);
} else if (effect->type == SceneEffect::DropShadow) {
output = render(static_cast<RenderEffectDropShadow*>(effect), pass->getFbo(), blendPool, vp, voffset, ioffset);
output = render(static_cast<RenderEffectDropShadow*>(effect), pass->getFbo(), pool, vp, voffset, ioffset);
} else {
output = render(effect, pass->getFbo(), blendPool, vp, voffset, ioffset);
output = render(effect, pass->getFbo(), pool, vp, voffset, ioffset);
}

if (!output) return false;
Expand Down
8 changes: 4 additions & 4 deletions src/renderer/gl_engine/tvgGlEffect.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ class GlEffect
bool region(RenderEffectGaussianBlur* effect);
bool region(RenderEffectDropShadow* effect);

GlRenderTask* render(RenderEffectGaussianBlur* effect, GlRenderTarget* dstFbo, Array<GlRenderTargetPool*>& blendPool, const RenderRegion& vp, uint32_t voffset, uint32_t ioffset);
GlRenderTask* render(RenderEffectDropShadow* effect, GlRenderTarget* dstFbo, Array<GlRenderTargetPool*>& blendPool, const RenderRegion& vp, uint32_t voffset, uint32_t ioffset);
GlRenderTask* render(RenderEffect* effect, GlRenderTarget* dstFbo, Array<GlRenderTargetPool*>& blendPool, const RenderRegion& vp, uint32_t voffset, uint32_t ioffset);
GlRenderTask* render(RenderEffectGaussianBlur* effect, GlRenderTarget* dstFbo, GlRenderTargetPool& pool, const RenderRegion& vp, uint32_t voffset, uint32_t ioffset);
GlRenderTask* render(RenderEffectDropShadow* effect, GlRenderTarget* dstFbo, GlRenderTargetPool& pool, const RenderRegion& vp, uint32_t voffset, uint32_t ioffset);
GlRenderTask* render(RenderEffect* effect, GlRenderTarget* dstFbo, GlRenderTargetPool& pool, const RenderRegion& vp, uint32_t voffset, uint32_t ioffset);

public:
GlEffect(GlStageBuffer* buffer);
~GlEffect();

void update(RenderEffect* effect, const Matrix& transform);
bool region(RenderEffect* effect);
bool render(RenderEffect* effect, GlRenderPass* pass, Array<GlRenderTargetPool*>& blendPool);
bool render(RenderEffect* effect, GlRenderPass* pass, GlRenderTargetPool& pool);
};

#endif /* _TVG_GL_EFFECT_H_ */
75 changes: 41 additions & 34 deletions src/renderer/gl_engine/tvgGlRenderTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

#include "tvgGlRenderTarget.h"

/************************************************************************/
/* GlRenderTarget Class Implementation */
/************************************************************************/

GlRenderTarget::GlRenderTarget() {}

GlRenderTarget::~GlRenderTarget()
Expand Down Expand Up @@ -90,49 +94,52 @@ void GlRenderTarget::reset()
mFbo = mColorBuffer = mDepthStencilBuffer = mResolveFbo = mColorTex = 0;
}

GlRenderTargetPool::GlRenderTargetPool(uint32_t maxWidth, uint32_t maxHeight): mMaxWidth(maxWidth), mMaxHeight(maxHeight), mPool() {}
/************************************************************************/
/* GlRenderTargetPool Class Implementation */
/************************************************************************/

GlRenderTargetPool::GlRenderTargetPool(){}


GlRenderTargetPool::~GlRenderTargetPool()
{
for (uint32_t i = 0; i < mPool.count; i++) {
delete mPool[i];
}
reset();
}

uint32_t alignPow2(uint32_t value)

void GlRenderTargetPool::init(uint32_t width, uint32_t height)
{
uint32_t ret = 1;
while (ret < value) {
ret <<= 1;
}
return ret;
mWidth = width;
mHeight = height;
}

GlRenderTarget* GlRenderTargetPool::getRenderTarget(const RenderRegion& vp, GLuint resolveId)

void GlRenderTargetPool::reset()
{
auto width = vp.w();
auto height = vp.h();

// pow2 align width and height
if (width >= mMaxWidth) width = mMaxWidth;
else width = alignPow2(width);
if (width >= mMaxWidth) width = mMaxWidth;

if (height >= mMaxHeight) height = mMaxHeight;
else height = alignPow2(height);
if (height >= mMaxHeight) height = mMaxHeight;

for (uint32_t i = 0; i < mPool.count; i++) {
auto rt = mPool[i];
if (rt->getWidth() == width && rt->getHeight() == height) {
rt->setViewport(vp);
return rt;
}
for (uint32_t i = 0; i < mPool.count; i++)
delete mPool[i];
mPool.clear();
mWidth = 0;
mHeight = 0;
}


GlRenderTarget* GlRenderTargetPool::getRenderTarget(GLuint resolveId)
{
GlRenderTarget* renderTarget{};
if (mPool.count > 0) {
renderTarget = mPool.last();
mPool.pop();
} else {
renderTarget = new GlRenderTarget;
renderTarget->init(mWidth, mHeight, resolveId);
renderTarget->setViewport({{0,0},{(int32_t)mWidth, (int32_t)mHeight}});
}
return renderTarget;
}

auto rt = new GlRenderTarget();
rt->init(width, height, resolveId);
rt->setViewport(vp);
mPool.push(rt);
return rt;

void GlRenderTargetPool::freeRenderTarget(GlRenderTarget* renderTarget)
{
if (renderTarget) mPool.push(renderTarget);
}
24 changes: 19 additions & 5 deletions src/renderer/gl_engine/tvgGlRenderTarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

#include "tvgGlCommon.h"

/************************************************************************/
/* GlRenderTarget Class Implementation */
/************************************************************************/

class GlRenderTarget
{
public:
Expand All @@ -41,7 +45,9 @@ class GlRenderTarget
uint32_t getWidth() const { return mWidth; }
uint32_t getHeight() const { return mHeight; }

void setViewport(const RenderRegion& vp) { mViewport = vp; }
void setViewport(const RenderRegion& vp) {
mViewport = vp;
}
const RenderRegion& getViewport() const { return mViewport; }

bool invalid() const { return mFbo == 0; }
Expand All @@ -57,15 +63,23 @@ class GlRenderTarget
GLuint mColorTex = 0;
};

/************************************************************************/
/* GlRenderTargetPool Class Implementation */
/************************************************************************/

class GlRenderTargetPool {
public:
GlRenderTargetPool(uint32_t maxWidth, uint32_t maxHeight);
GlRenderTargetPool();
~GlRenderTargetPool();

GlRenderTarget* getRenderTarget(const RenderRegion& vp, GLuint resolveId = 0);
void init(uint32_t width, uint32_t height);
void reset();

GlRenderTarget* getRenderTarget(GLuint resolveId = 0);
void freeRenderTarget(GlRenderTarget* renderTarget);
private:
uint32_t mMaxWidth = 0;
uint32_t mMaxHeight = 0;
uint32_t mWidth = 0;
uint32_t mHeight = 0;
Array<GlRenderTarget*> mPool;
};

Expand Down
7 changes: 2 additions & 5 deletions src/renderer/gl_engine/tvgGlRenderTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,6 @@ void GlComposeTask::run()
GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
GL_CHECK(glDepthMask(0));

GL_CHECK(glViewport(0, 0, mRenderWidth, mRenderHeight));
GL_CHECK(glScissor(0, 0, mRenderWidth, mRenderHeight));

ARRAY_FOREACH(p, mTasks) {
(*p)->run();
}
Expand Down Expand Up @@ -244,7 +241,7 @@ void GlComposeTask::onResolve()
{
GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, getSelfFbo()));
GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, getResolveFboId()));
GL_CHECK(glBlitFramebuffer(0, 0, mRenderWidth, mRenderHeight, 0, 0, mRenderWidth, mRenderHeight, GL_COLOR_BUFFER_BIT, GL_NEAREST));
GL_CHECK(glBlitFramebuffer(0, 0, mFbo->getWidth(), mFbo->getHeight(), 0, 0, mFbo->getWidth(), mFbo->getHeight(), GL_COLOR_BUFFER_BIT, GL_NEAREST));
}


Expand Down Expand Up @@ -305,7 +302,7 @@ void GlDrawBlitTask::run()
GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo()));

GL_CHECK(glViewport(0, 0, mParentWidth, mParentHeight));
GL_CHECK(glScissor(0, 0, mParentWidth, mParentWidth));
GL_CHECK(glScissor(0, 0, mParentWidth, mParentHeight));
GlRenderTask::run();
}

Expand Down
Loading
Loading