tutorial 04
This commit is contained in:
@@ -51,9 +51,13 @@ void DynamicPipeline::onUseEnd() {
|
||||
core.deallocateOnUse();
|
||||
}
|
||||
|
||||
void DynamicPipeline::render(const DynamicMesh& mesh,
|
||||
const DynPipOptions* options) {
|
||||
render(&mesh, options);
|
||||
void DynamicPipeline::onFrameEnd() {}
|
||||
|
||||
void DynamicPipeline::render(const DynamicMesh* mesh) { render(mesh, nullptr); }
|
||||
|
||||
void DynamicPipeline::render(const DynamicMesh* mesh,
|
||||
const DynPipOptions& options) {
|
||||
render(mesh, &options);
|
||||
}
|
||||
|
||||
void DynamicPipeline::render(const DynamicMesh* mesh,
|
||||
|
||||
@@ -78,6 +78,8 @@ void MinecraftPipeline::onUseEnd() {
|
||||
manager.deallocateOnUse();
|
||||
}
|
||||
|
||||
void MinecraftPipeline::onFrameEnd() {}
|
||||
|
||||
void MinecraftPipeline::initBBox() {
|
||||
const auto& block = manager.getBlockData();
|
||||
bbox = new RenderBBox(block.vertices, block.count);
|
||||
|
||||
@@ -72,7 +72,8 @@ u32 StaPipCore::getMaxVertCountByParams(const bool& isSingleColor,
|
||||
->getMaxVertCount(isSingleColor, qbufferRenderer.getBufferSize());
|
||||
}
|
||||
|
||||
void StaPipCore::render(StaPipBag* bag, StaPipBagPackagesBBox* bbox) {
|
||||
void StaPipCore::render(StaPipBag* bag, StaPipBagPackagesBBox* bbox,
|
||||
const u32& maxVertCount) {
|
||||
if (bag->count <= 0) return;
|
||||
|
||||
bool frustumCull =
|
||||
@@ -105,32 +106,20 @@ void StaPipCore::render(StaPipBag* bag, StaPipBagPackagesBBox* bbox) {
|
||||
TYRA_ASSERT(!(!frustumCull && bag->info->fullClipChecks == true),
|
||||
"Full clip checks are not supported with frustum culling = off!");
|
||||
|
||||
u32 maxVertCount = getMaxVertCountByBag(bag);
|
||||
setMaxVertCount(maxVertCount);
|
||||
|
||||
StaPipBagPackagesBBox* renderBbox = nullptr;
|
||||
|
||||
CoreBBoxFrustum frustumCheck = OUTSIDE_FRUSTUM;
|
||||
|
||||
if (frustumCull) {
|
||||
if (!bbox)
|
||||
renderBbox =
|
||||
new StaPipBagPackagesBBox(bag->vertices, bag->count, maxVertCount);
|
||||
else
|
||||
renderBbox = bbox;
|
||||
|
||||
frustumCheck = renderBbox->getMainBBox()->clipFrustumCheck(
|
||||
frustumCheck = bbox->getMainBBox()->clipFrustumCheck(
|
||||
rendererCore->renderer3D.frustumPlanes.getAll(), *bag->info->model);
|
||||
|
||||
if (frustumCheck == OUTSIDE_FRUSTUM) {
|
||||
if (!bbox) {
|
||||
delete renderBbox;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
packager.setRenderBBox(renderBbox);
|
||||
packager.setRenderBBox(bbox);
|
||||
|
||||
M4x4 mvp;
|
||||
|
||||
@@ -199,10 +188,6 @@ void StaPipCore::render(StaPipBag* bag, StaPipBagPackagesBBox* bbox) {
|
||||
}
|
||||
}
|
||||
|
||||
if (frustumCull && !bbox) {
|
||||
delete renderBbox;
|
||||
}
|
||||
|
||||
if (texBuffers) delete texBuffers;
|
||||
|
||||
qbufferRenderer.flushBuffers();
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
# _____ ____ ___
|
||||
# | \/ ____| |___|
|
||||
# | | | \ | |
|
||||
#-----------------------------------------------------------------------
|
||||
# Copyright 2022, tyra - https://github.com/h4570/tyra
|
||||
# Licensed under Apache License 2.0
|
||||
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
|
||||
*/
|
||||
|
||||
#include "renderer/3d/pipeline/static/stapip_bag_bboxes_cacher.hpp"
|
||||
#include <algorithm>
|
||||
|
||||
namespace Tyra {
|
||||
|
||||
StapipBagBBoxesCacher::StapipBagBBoxesCacher() {}
|
||||
|
||||
StapipBagBBoxesCacher::~StapipBagBBoxesCacher() {}
|
||||
|
||||
void StapipBagBBoxesCacher::onFrameEnd() {
|
||||
for (auto& item : storage) {
|
||||
if (item.framesLeftToDestroy > 0) {
|
||||
item.framesLeftToDestroy--;
|
||||
}
|
||||
}
|
||||
|
||||
storage.erase(std::remove_if(storage.begin(), storage.end(),
|
||||
[](const StapipBagBBoxesCacheItem& item) {
|
||||
return item.framesLeftToDestroy <= 0;
|
||||
}),
|
||||
storage.end());
|
||||
}
|
||||
|
||||
StaPipBagPackagesBBox* StapipBagBBoxesCacher::getBBoxesByMesh(
|
||||
const MeshMaterialFrame* frame, const u32& maxVertCount) {
|
||||
auto* cache = getCache(maxVertCount, frame->id);
|
||||
|
||||
if (cache) {
|
||||
cache->framesLeftToDestroy = cacheFramesCount * cacheSecondsCount;
|
||||
return cache->bboxes.get();
|
||||
}
|
||||
|
||||
auto bboxes = std::make_unique<StaPipBagPackagesBBox>(
|
||||
frame->vertices, frame->count, maxVertCount);
|
||||
|
||||
storage.push_back(
|
||||
StapipBagBBoxesCacheItem{maxVertCount, frame->id, std::move(bboxes),
|
||||
cacheFramesCount * cacheSecondsCount});
|
||||
|
||||
return storage.back().bboxes.get();
|
||||
}
|
||||
|
||||
StapipBagBBoxesCacheItem* StapipBagBBoxesCacher::getCache(
|
||||
const u32& maxVertCount, const u32& frameId) {
|
||||
for (auto& item : storage) {
|
||||
if (item.vu1MaxVertCount == maxVertCount && item.frameId == frameId) {
|
||||
return &item;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace Tyra
|
||||
@@ -11,7 +11,7 @@
|
||||
#include "renderer/3d/pipeline/static/static_pipeline.hpp"
|
||||
#include "debug/debug.hpp"
|
||||
#include "renderer/3d/pipeline/static/core/bag/stapip_info_bag.hpp"
|
||||
|
||||
#include <memory>
|
||||
namespace Tyra {
|
||||
|
||||
StaticPipeline::StaticPipeline() {}
|
||||
@@ -39,9 +39,13 @@ void StaticPipeline::onUseEnd() {
|
||||
core.deallocateOnUse();
|
||||
}
|
||||
|
||||
void StaticPipeline::render(const StaticMesh& mesh,
|
||||
const StaPipOptions* options) {
|
||||
render(&mesh, options);
|
||||
void StaticPipeline::onFrameEnd() { cacher.onFrameEnd(); }
|
||||
|
||||
void StaticPipeline::render(const StaticMesh* mesh) { render(mesh, nullptr); }
|
||||
|
||||
void StaticPipeline::render(const StaticMesh* mesh,
|
||||
const StaPipOptions& options) {
|
||||
render(mesh, &options);
|
||||
}
|
||||
|
||||
void StaticPipeline::render(const StaticMesh* mesh,
|
||||
@@ -55,6 +59,7 @@ void StaticPipeline::render(const StaticMesh* mesh,
|
||||
|
||||
auto model = mesh->getModelMatrix();
|
||||
auto* infoBag = getInfoBag(mesh, options, &model);
|
||||
auto dirLightsBag = std::make_unique<PipelineDirLightsBag>(true);
|
||||
|
||||
TYRA_ASSERT(
|
||||
!(options->frustumCulling != PipelineFrustumCulling_Precise &&
|
||||
@@ -85,11 +90,21 @@ void StaticPipeline::render(const StaticMesh* mesh,
|
||||
bag.info = infoBag;
|
||||
bag.color = getColorBag(material, materialFrame);
|
||||
bag.texture = getTextureBag(material, materialFrame);
|
||||
bag.lighting = getLightingBag(materialFrame, &model, options);
|
||||
core.render(&bag);
|
||||
bag.lighting =
|
||||
getLightingBag(materialFrame, dirLightsBag.get(), &model, options);
|
||||
|
||||
u32 maxVertCount = core.getMaxVertCountByBag(&bag);
|
||||
|
||||
StaPipBagPackagesBBox* bbox = nullptr;
|
||||
if (bag.info->frustumCulling == PipelineFrustumCulling_Precise) {
|
||||
bbox = cacher.getBBoxesByMesh(materialFrame, maxVertCount);
|
||||
}
|
||||
|
||||
core.render(&bag, bbox, maxVertCount);
|
||||
|
||||
deallocDrawBags(&bag, material);
|
||||
}
|
||||
|
||||
delete infoBag;
|
||||
|
||||
if (optionsManuallyAllocated) delete options;
|
||||
@@ -156,18 +171,15 @@ StaPipTextureBag* StaticPipeline::getTextureBag(
|
||||
}
|
||||
|
||||
StaPipLightingBag* StaticPipeline::getLightingBag(
|
||||
const MeshMaterialFrame* materialFrame, M4x4* model,
|
||||
const StaPipOptions* options) const {
|
||||
const MeshMaterialFrame* materialFrame, PipelineDirLightsBag* dirLightsBag,
|
||||
M4x4* model, const StaPipOptions* options) const {
|
||||
if (!materialFrame->normals || options == nullptr ||
|
||||
options->lighting == nullptr)
|
||||
return nullptr;
|
||||
|
||||
auto* result = new StaPipLightingBag();
|
||||
|
||||
// TODO: Make it once per rendering, very redundant
|
||||
auto* dirLightsBag = new PipelineDirLightsBag(true);
|
||||
result->dirLights = dirLightsBag;
|
||||
|
||||
result->lightMatrix = model;
|
||||
|
||||
result->dirLights->setLightsManually(
|
||||
@@ -194,7 +206,6 @@ void StaticPipeline::deallocDrawBags(StaPipBag* bag,
|
||||
}
|
||||
|
||||
if (bag->lighting) {
|
||||
delete bag->lighting->dirLights; // TODO
|
||||
delete bag->lighting;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,12 @@ Renderer3D::~Renderer3D() {}
|
||||
|
||||
void Renderer3D::init(RendererCore* core) { utility.init(core); }
|
||||
|
||||
void Renderer3D::onFrameEnd() {
|
||||
if (currentPipeline != nullptr) {
|
||||
currentPipeline->onFrameEnd();
|
||||
}
|
||||
}
|
||||
|
||||
void Renderer3D::usePipeline(Renderer3DPipeline& pipeline) {
|
||||
usePipeline(&pipeline);
|
||||
}
|
||||
|
||||
@@ -27,6 +27,9 @@ void Renderer::beginFrame(const CameraInfo3D& cameraInfo) {
|
||||
core.beginFrame(cameraInfo);
|
||||
}
|
||||
|
||||
void Renderer::endFrame() { core.endFrame(); }
|
||||
void Renderer::endFrame() {
|
||||
core.endFrame();
|
||||
renderer3D.onFrameEnd();
|
||||
}
|
||||
|
||||
} // namespace Tyra
|
||||
|
||||
Reference in New Issue
Block a user