Skip to content

Commit

Permalink
Wrap the acquisition of the vertex array object into a ResourceTask. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
domchen authored Jan 14, 2024
1 parent 95183af commit 0a22469
Show file tree
Hide file tree
Showing 18 changed files with 260 additions and 159 deletions.
17 changes: 9 additions & 8 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 7 additions & 8 deletions src/gpu/Gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ class Gpu {
public:
virtual ~Gpu() = default;

Context* context() {
return _context;
Context* getContext() {
return context;
}

virtual std::shared_ptr<RenderPass> getRenderPass() = 0;

virtual std::unique_ptr<TextureSampler> createSampler(int width, int height, PixelFormat format,
int mipLevelCount) = 0;

Expand All @@ -55,21 +57,18 @@ class Gpu {

virtual bool waitSemaphore(const Semaphore* semaphore) = 0;

virtual RenderPass* getRenderPass(std::shared_ptr<RenderTarget> renderTarget,
std::shared_ptr<Texture> renderTargetTexture) = 0;

virtual bool submitToGpu(bool syncCpu) = 0;

virtual void submit(RenderPass* renderPass) = 0;

void regenerateMipMapLevels(const TextureSampler* sampler);

protected:
explicit Gpu(Context* context) : _context(context) {
Context* context;

explicit Gpu(Context* context) : context(context) {
}

virtual void onRegenerateMipMapLevels(const TextureSampler* sampler) = 0;

Context* _context;
};
} // namespace tgfx
13 changes: 12 additions & 1 deletion src/gpu/RenderPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,22 @@
#include "RenderPass.h"

namespace tgfx {
void RenderPass::begin() {
bool RenderPass::begin(std::shared_ptr<RenderTargetProxy> renderTargetProxy) {
if (renderTargetProxy == nullptr) {
return false;
}
_renderTarget = renderTargetProxy->getRenderTarget();
if (_renderTarget == nullptr) {
return false;
}
_renderTargetTexture = renderTargetProxy->getTexture();
drawPipelineStatus = DrawPipelineStatus::NotConfigured;
return true;
}

void RenderPass::end() {
_renderTarget = nullptr;
_renderTargetTexture = nullptr;
resetActiveBuffers();
}

Expand Down
17 changes: 9 additions & 8 deletions src/gpu/RenderPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "gpu/RenderTarget.h"
#include "gpu/processors/GeometryProcessor.h"
#include "gpu/proxies/GpuBufferProxy.h"
#include "gpu/proxies/RenderTargetProxy.h"
#include "tgfx/core/BlendMode.h"
#include "tgfx/core/Color.h"
#include "tgfx/gpu/Context.h"
Expand All @@ -40,11 +41,10 @@ enum class PrimitiveType {

class RenderPass {
public:
explicit RenderPass(Context* context) : _context(context) {
}
virtual ~RenderPass() = default;

Context* context() {
return _context;
Context* getContext() {
return context;
}

std::shared_ptr<RenderTarget> renderTarget() {
Expand All @@ -55,9 +55,7 @@ class RenderPass {
return _renderTargetTexture;
}

virtual ~RenderPass() = default;

void begin();
bool begin(std::shared_ptr<RenderTargetProxy> renderTargetProxy);
void end();
void bindProgramAndScissorClip(const ProgramInfo* programInfo, const Rect& drawBounds);
void bindBuffers(std::shared_ptr<GpuBuffer> indexBuffer, std::shared_ptr<GpuBuffer> vertexBuffer);
Expand All @@ -66,6 +64,9 @@ class RenderPass {
void clear(const Rect& scissor, Color color);

protected:
explicit RenderPass(Context* context) : context(context) {
}

virtual bool onBindProgramAndScissorClip(const ProgramInfo* programInfo,
const Rect& drawBounds) = 0;
virtual void onBindBuffers(std::shared_ptr<GpuBuffer> indexBuffer,
Expand All @@ -74,7 +75,7 @@ class RenderPass {
virtual void onDrawIndexed(PrimitiveType primitiveType, size_t baseIndex, size_t indexCount) = 0;
virtual void onClear(const Rect& scissor, Color color) = 0;

Context* _context = nullptr;
Context* context = nullptr;
std::shared_ptr<RenderTarget> _renderTarget = nullptr;
std::shared_ptr<Texture> _renderTargetTexture = nullptr;
Program* _program = nullptr;
Expand Down
11 changes: 6 additions & 5 deletions src/gpu/ops/DrawOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
namespace tgfx {
static DstTextureInfo CreateDstTextureInfo(RenderPass* renderPass, Rect dstRect) {
DstTextureInfo dstTextureInfo = {};
if (renderPass->context()->caps()->textureBarrierSupport && renderPass->renderTargetTexture()) {
auto context = renderPass->getContext();
if (context->caps()->textureBarrierSupport && renderPass->renderTargetTexture()) {
dstTextureInfo.texture = renderPass->renderTargetTexture();
dstTextureInfo.requiresBarrier = true;
return dstTextureInfo;
Expand All @@ -40,16 +41,16 @@ static DstTextureInfo CreateDstTextureInfo(RenderPass* renderPass, Rect dstRect)
}
dstRect.roundOut();
dstTextureInfo.offset = {dstRect.x(), dstRect.y()};
auto dstTexture = Texture::MakeRGBA(renderPass->context(), static_cast<int>(dstRect.width()),
auto dstTexture = Texture::MakeRGBA(context, static_cast<int>(dstRect.width()),
static_cast<int>(dstRect.height()), false,
renderPass->renderTarget()->origin());
if (dstTexture == nullptr) {
LOGE("Failed to create dst texture(%f*%f).", dstRect.width(), dstRect.height());
return {};
}
dstTextureInfo.texture = dstTexture;
renderPass->context()->gpu()->copyRenderTargetToTexture(renderPass->renderTarget().get(),
dstTexture.get(), dstRect, Point::Zero());
context->gpu()->copyRenderTargetToTexture(renderPass->renderTarget().get(), dstTexture.get(),
dstRect, Point::Zero());
return dstTextureInfo;
}

Expand All @@ -62,7 +63,7 @@ std::unique_ptr<Pipeline> DrawOp::createPipeline(RenderPass* renderPass,
std::move(_masks.begin(), _masks.end(),
fragmentProcessors.begin() + static_cast<int>(numColorProcessors));
DstTextureInfo dstTextureInfo = {};
auto caps = renderPass->context()->caps();
auto caps = renderPass->getContext()->caps();
if (!BlendModeAsCoeff(blendMode) && !caps->frameBufferFetchSupport) {
dstTextureInfo = CreateDstTextureInfo(renderPass, bounds());
}
Expand Down
2 changes: 1 addition & 1 deletion src/gpu/ops/RRectOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ void RRectOp::execute(RenderPass* renderPass) {
auto pipeline = createPipeline(
renderPass, EllipseGeometryProcessor::Make(renderPass->renderTarget()->width(),
renderPass->renderTarget()->height(), false,
UseScale(renderPass->context()), localMatrix));
UseScale(renderPass->getContext()), localMatrix));
renderPass->bindProgramAndScissorClip(pipeline.get(), scissorRect());
renderPass->bindBuffers(indexBuffer, vertexBuffer);
renderPass->drawIndexed(PrimitiveType::Triangles, 0, rRectPaints.size() * kIndicesPerFillRRect);
Expand Down
13 changes: 5 additions & 8 deletions src/gpu/tasks/OpsRenderTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void OpsRenderTask::addOp(std::unique_ptr<Op> op) {
}

void OpsRenderTask::prepare(Context* context) {
renderPass = context->gpu()->getRenderPass();
for (auto& op : ops) {
op->prepare(context);
}
Expand All @@ -38,20 +39,16 @@ bool OpsRenderTask::execute(Gpu* gpu) {
if (ops.empty()) {
return false;
}
auto renderTarget = renderTargetProxy->getRenderTarget();
auto texture = renderTargetProxy->getTexture();
auto renderPass = gpu->getRenderPass(renderTarget, texture);
if (renderPass == nullptr) {
LOGE("OpsTask::execute() Failed to create render pass!");
if (!renderPass->begin(renderTargetProxy)) {
LOGE("OpsTask::execute() Failed to initialize the render pass!");
return false;
}
renderPass->begin();
auto tempOps = std::move(ops);
for (auto& op : tempOps) {
op->execute(renderPass);
op->execute(renderPass.get());
}
gpu->submit(renderPass.get());
renderPass->end();
gpu->submit(renderPass);
return true;
}
} // namespace tgfx
3 changes: 2 additions & 1 deletion src/gpu/tasks/OpsRenderTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class OpsRenderTask : public RenderTask {
bool execute(Gpu* gpu) override;

private:
std::vector<std::unique_ptr<Op>> ops;
std::shared_ptr<RenderPass> renderPass = nullptr;
std::vector<std::unique_ptr<Op>> ops = {};
};
} // namespace tgfx
6 changes: 3 additions & 3 deletions src/gpu/tasks/RenderTargetCreateTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ RenderTargetCreateTask::RenderTargetCreateTask(ResourceKey strongKey, ResourceKe
std::shared_ptr<Resource> RenderTargetCreateTask::onMakeResource(Context* context) {
auto texture = Resource::Get<Texture>(context, textureKey);
if (texture == nullptr) {
LOGE("RenderTargetCreateTask::onMakeResource() Failed to get texture!");
LOGE("RenderTargetCreateTask::onMakeResource() Failed to get the associated texture!");
return nullptr;
}
if (texture->getSampler()->format != pixelFormat) {
LOGE("RenderTargetCreateTask::onMakeResource() texture format mismatch!");
LOGE("RenderTargetCreateTask::onMakeResource() the texture format mismatch!");
return nullptr;
}
auto renderTarget = RenderTarget::MakeFrom(texture.get(), sampleCount);
if (renderTarget == nullptr) {
LOGE("RenderTargetCreateTask::onMakeResource() Failed to create render target!");
LOGE("RenderTargetCreateTask::onMakeResource() Failed to create the render target!");
}
return renderTarget;
}
Expand Down
6 changes: 3 additions & 3 deletions src/gpu/tasks/TextureCreateTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class EmptyTextureTask : public TextureCreateTask {
std::shared_ptr<Resource> onMakeResource(Context* context) override {
auto texture = Texture::MakeFormat(context, width, height, format, mipMapped, origin);
if (texture == nullptr) {
LOGE("EmptyTextureTask::onMakeResource() Failed to create texture!");
LOGE("EmptyTextureTask::onMakeResource() Failed to create the texture!");
}
return texture;
}
Expand Down Expand Up @@ -66,12 +66,12 @@ class ImageDecoderTask : public TextureCreateTask {
}
auto imageBuffer = decoder->decode();
if (imageBuffer == nullptr) {
LOGE("ImageDecoderTask::onMakeResource() Failed to decode image!");
LOGE("ImageDecoderTask::onMakeResource() Failed to decode the image!");
return nullptr;
}
auto texture = Texture::MakeFrom(context, imageBuffer, mipMapped);
if (texture == nullptr) {
LOGE("ImageDecoderTask::onMakeResource() Failed to create texture!");
LOGE("ImageDecoderTask::onMakeResource() Failed to create the texture!");
} else {
// Free the decoded image buffer immediately to reduce memory pressure.
decoder = nullptr;
Expand Down
Loading

0 comments on commit 0a22469

Please sign in to comment.