From a21bcee671d67fd98d25462bd373708c0456817e Mon Sep 17 00:00:00 2001 From: h4570 Date: Wed, 24 Aug 2022 19:27:16 +0200 Subject: [PATCH] finished tutorial 9 --- ROADMAP.txt | 3 - .../packaging/stapip_bag_packages_bbox.hpp | 6 +- .../{ => core}/stapip_bag_bboxes_cacher.hpp | 11 ++- .../3d/pipeline/static/core/stapip_core.hpp | 7 +- .../3d/pipeline/static/static_pipeline.hpp | 2 - .../packaging/stapip_bag_packages_bbox.cpp | 5 +- .../{ => core}/stapip_bag_bboxes_cacher.cpp | 19 ++--- .../3d/pipeline/static/core/stapip_core.cpp | 13 +++- .../3d/pipeline/static/static_pipeline.cpp | 11 +-- tutorials/09-manual-mode/inc/tutorial_09.hpp | 16 ++++ tutorials/09-manual-mode/src/tutorial_09.cpp | 78 ++++++++++++++++++- 11 files changed, 130 insertions(+), 41 deletions(-) rename engine/inc/renderer/3d/pipeline/static/{ => core}/stapip_bag_bboxes_cacher.hpp (71%) rename engine/src/renderer/3d/pipeline/static/{ => core}/stapip_bag_bboxes_cacher.cpp (70%) diff --git a/ROADMAP.txt b/ROADMAP.txt index c0385ba..7bce026 100644 --- a/ROADMAP.txt +++ b/ROADMAP.txt @@ -1,7 +1,4 @@ ------------ Tyra's v2.0 roadmap to publish on GitHub ------------ - -- [Tutorials] 9. - - [General] Check all left TODOs -> Github issues - [General] Readme Credits: clipping, obj loader - [Video] How to setup environment diff --git a/engine/inc/renderer/3d/pipeline/static/core/bag/packaging/stapip_bag_packages_bbox.hpp b/engine/inc/renderer/3d/pipeline/static/core/bag/packaging/stapip_bag_packages_bbox.hpp index 1208989..7be3bd4 100644 --- a/engine/inc/renderer/3d/pipeline/static/core/bag/packaging/stapip_bag_packages_bbox.hpp +++ b/engine/inc/renderer/3d/pipeline/static/core/bag/packaging/stapip_bag_packages_bbox.hpp @@ -20,9 +20,9 @@ namespace Tyra { */ class StaPipBagPackagesBBox { public: - StaPipBagPackagesBBox(Vec4* t_vertices, u32* t_faces, const u32& t_facesCount, - const u32& t_maxVertCount); - StaPipBagPackagesBBox(Vec4* t_vertices, const u32& t_counts, + StaPipBagPackagesBBox(const Vec4* t_vertices, u32* t_faces, + const u32& t_facesCount, const u32& t_maxVertCount); + StaPipBagPackagesBBox(const Vec4* t_vertices, const u32& t_counts, const u32& t_maxVertCount); ~StaPipBagPackagesBBox(); diff --git a/engine/inc/renderer/3d/pipeline/static/stapip_bag_bboxes_cacher.hpp b/engine/inc/renderer/3d/pipeline/static/core/stapip_bag_bboxes_cacher.hpp similarity index 71% rename from engine/inc/renderer/3d/pipeline/static/stapip_bag_bboxes_cacher.hpp rename to engine/inc/renderer/3d/pipeline/static/core/stapip_bag_bboxes_cacher.hpp index 774bfa5..22ee0d3 100644 --- a/engine/inc/renderer/3d/pipeline/static/stapip_bag_bboxes_cacher.hpp +++ b/engine/inc/renderer/3d/pipeline/static/core/stapip_bag_bboxes_cacher.hpp @@ -11,7 +11,7 @@ #pragma once #include -#include "core/bag/packaging/stapip_bag_packages_bbox.hpp" +#include "./bag/packaging/stapip_bag_packages_bbox.hpp" #include "renderer/3d/mesh/mesh.hpp" #include #include @@ -20,7 +20,7 @@ namespace Tyra { struct StapipBagBBoxesCacheItem { u32 vu1MaxVertCount; - u32 frameId; + u32 id; std::unique_ptr bboxes; int framesLeftToDestroy; }; @@ -35,12 +35,11 @@ class StapipBagBBoxesCacher { void onFrameEnd(); - StaPipBagPackagesBBox* getBBoxesByMesh(const MeshMaterialFrame* frame, - const u32& maxVertCount); + StaPipBagPackagesBBox* getBBoxes(const Vec4* vertices, const u32& count, + const u32& id, const u32& maxVertCount); private: - StapipBagBBoxesCacheItem* getCache(const u32& maxVertCount, - const u32& frameId); + StapipBagBBoxesCacheItem* getCache(const u32& maxVertCount, const u32& id); std::vector storage; }; diff --git a/engine/inc/renderer/3d/pipeline/static/core/stapip_core.hpp b/engine/inc/renderer/3d/pipeline/static/core/stapip_core.hpp index 0ebfc3e..bd57b53 100644 --- a/engine/inc/renderer/3d/pipeline/static/core/stapip_core.hpp +++ b/engine/inc/renderer/3d/pipeline/static/core/stapip_core.hpp @@ -16,6 +16,7 @@ #include "./bag/packaging/stapip_bag_package.hpp" #include "./bag/packaging/stapip_bag_packager.hpp" #include "./stapip_qbuffer_renderer.hpp" +#include "./stapip_bag_bboxes_cacher.hpp" #include "renderer/3d/pipeline/shared/pipeline_frustum_culling.hpp" namespace Tyra { @@ -27,9 +28,10 @@ class StaPipCore { void init(RendererCore* t_core); + void onFrameEnd(); + /** Render 3D via "bags" */ - void render(StaPipBag* bag, StaPipBagPackagesBBox* bbox, - const u32& maxVertCount); + void render(StaPipBag* bag); /** Get max vert count of VU1 qbuffer (for optimizations) */ u32 getMaxVertCountByParams(const bool& isSingleColor, @@ -59,6 +61,7 @@ class StaPipCore { u32 maxVertCount; RendererCore* rendererCore; + StapipBagBBoxesCacher cacher; void setMaxVertCount(const u32& count); StaPipBagPackager packager; diff --git a/engine/inc/renderer/3d/pipeline/static/static_pipeline.hpp b/engine/inc/renderer/3d/pipeline/static/static_pipeline.hpp index 859fe9b..05b8ef4 100644 --- a/engine/inc/renderer/3d/pipeline/static/static_pipeline.hpp +++ b/engine/inc/renderer/3d/pipeline/static/static_pipeline.hpp @@ -17,7 +17,6 @@ #include "renderer/3d/mesh/static/static_mesh.hpp" #include "./core/stapip_core.hpp" #include "./stapip_options.hpp" -#include "./stapip_bag_bboxes_cacher.hpp" namespace Tyra { @@ -51,7 +50,6 @@ class StaticPipeline : public Renderer3DPipeline { void render(const StaticMesh* mesh, const StaPipOptions* options); private: - StapipBagBBoxesCacher cacher; RendererCore* rendererCore; Vec4* colorsCache; diff --git a/engine/src/renderer/3d/pipeline/static/core/bag/packaging/stapip_bag_packages_bbox.cpp b/engine/src/renderer/3d/pipeline/static/core/bag/packaging/stapip_bag_packages_bbox.cpp index 4b564cd..c415ad6 100644 --- a/engine/src/renderer/3d/pipeline/static/core/bag/packaging/stapip_bag_packages_bbox.cpp +++ b/engine/src/renderer/3d/pipeline/static/core/bag/packaging/stapip_bag_packages_bbox.cpp @@ -18,7 +18,8 @@ namespace Tyra { -StaPipBagPackagesBBox::StaPipBagPackagesBBox(Vec4* t_vertices, u32* t_faces, +StaPipBagPackagesBBox::StaPipBagPackagesBBox(const Vec4* t_vertices, + u32* t_faces, const u32& t_facesCount, const u32& t_maxVertCount) { u32 splitPartSize = t_maxVertCount / 3; @@ -35,7 +36,7 @@ StaPipBagPackagesBBox::StaPipBagPackagesBBox(Vec4* t_vertices, u32* t_faces, mainBBox = new RenderBBox(*bboxParts, 0, partsCount); } -StaPipBagPackagesBBox::StaPipBagPackagesBBox(Vec4* t_vertices, +StaPipBagPackagesBBox::StaPipBagPackagesBBox(const Vec4* t_vertices, const u32& t_count, const u32& t_maxVertCount) { u32 splitPartSize = t_maxVertCount / 3; diff --git a/engine/src/renderer/3d/pipeline/static/stapip_bag_bboxes_cacher.cpp b/engine/src/renderer/3d/pipeline/static/core/stapip_bag_bboxes_cacher.cpp similarity index 70% rename from engine/src/renderer/3d/pipeline/static/stapip_bag_bboxes_cacher.cpp rename to engine/src/renderer/3d/pipeline/static/core/stapip_bag_bboxes_cacher.cpp index 3ef3703..19483bf 100644 --- a/engine/src/renderer/3d/pipeline/static/stapip_bag_bboxes_cacher.cpp +++ b/engine/src/renderer/3d/pipeline/static/core/stapip_bag_bboxes_cacher.cpp @@ -8,7 +8,7 @@ # Sandro Sobczyński */ -#include "renderer/3d/pipeline/static/stapip_bag_bboxes_cacher.hpp" +#include "renderer/3d/pipeline/static/core/stapip_bag_bboxes_cacher.hpp" #include namespace Tyra { @@ -31,29 +31,30 @@ void StapipBagBBoxesCacher::onFrameEnd() { storage.end()); } -StaPipBagPackagesBBox* StapipBagBBoxesCacher::getBBoxesByMesh( - const MeshMaterialFrame* frame, const u32& maxVertCount) { - auto* cache = getCache(maxVertCount, frame->id); +StaPipBagPackagesBBox* StapipBagBBoxesCacher::getBBoxes( + const Vec4* vertices, const u32& count, const u32& id, + const u32& maxVertCount) { + auto* cache = getCache(maxVertCount, id); if (cache) { cache->framesLeftToDestroy = cacheFramesCount * cacheSecondsCount; return cache->bboxes.get(); } - auto bboxes = std::make_unique( - frame->vertices, frame->count, maxVertCount); + auto bboxes = + std::make_unique(vertices, count, maxVertCount); storage.push_back( - StapipBagBBoxesCacheItem{maxVertCount, frame->id, std::move(bboxes), + StapipBagBBoxesCacheItem{maxVertCount, id, std::move(bboxes), cacheFramesCount * cacheSecondsCount}); return storage.back().bboxes.get(); } StapipBagBBoxesCacheItem* StapipBagBBoxesCacher::getCache( - const u32& maxVertCount, const u32& frameId) { + const u32& maxVertCount, const u32& id) { for (auto& item : storage) { - if (item.vu1MaxVertCount == maxVertCount && item.frameId == frameId) { + if (item.vu1MaxVertCount == maxVertCount && item.id == id) { return &item; } } diff --git a/engine/src/renderer/3d/pipeline/static/core/stapip_core.cpp b/engine/src/renderer/3d/pipeline/static/core/stapip_core.cpp index 9073087..b394292 100644 --- a/engine/src/renderer/3d/pipeline/static/core/stapip_core.cpp +++ b/engine/src/renderer/3d/pipeline/static/core/stapip_core.cpp @@ -57,6 +57,8 @@ void StaPipCore::setLod() { lod.k = 0.0F; } +void StaPipCore::onFrameEnd() { cacher.onFrameEnd(); } + void StaPipCore::reinitVU1Programs() { qbufferRenderer.reinitVU1(); } u32 StaPipCore::getMaxVertCountByBag(const StaPipBag* bag) { @@ -72,8 +74,7 @@ u32 StaPipCore::getMaxVertCountByParams(const bool& isSingleColor, ->getMaxVertCount(isSingleColor, qbufferRenderer.getBufferSize()); } -void StaPipCore::render(StaPipBag* bag, StaPipBagPackagesBBox* bbox, - const u32& maxVertCount) { +void StaPipCore::render(StaPipBag* bag) { if (bag->count <= 0) return; bool frustumCull = @@ -106,6 +107,14 @@ 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); + + StaPipBagPackagesBBox* bbox = nullptr; + if (bag->info->frustumCulling == PipelineInfoBagFrustumCulling_Precise) { + bbox = cacher.getBBoxes(bag->vertices, bag->count, + reinterpret_cast(bag->vertices), maxVertCount); + } + setMaxVertCount(maxVertCount); CoreBBoxFrustum frustumCheck = OUTSIDE_FRUSTUM; diff --git a/engine/src/renderer/3d/pipeline/static/static_pipeline.cpp b/engine/src/renderer/3d/pipeline/static/static_pipeline.cpp index 240c49d..9c2e127 100644 --- a/engine/src/renderer/3d/pipeline/static/static_pipeline.cpp +++ b/engine/src/renderer/3d/pipeline/static/static_pipeline.cpp @@ -39,7 +39,7 @@ void StaticPipeline::onUseEnd() { core.deallocateOnUse(); } -void StaticPipeline::onFrameEnd() { cacher.onFrameEnd(); } +void StaticPipeline::onFrameEnd() { core.onFrameEnd(); } void StaticPipeline::render(const StaticMesh* mesh) { render(mesh, nullptr); } @@ -93,14 +93,7 @@ void StaticPipeline::render(const StaticMesh* mesh, bag.lighting = getLightingBag(materialFrame, dirLightsBag.get(), &model, options); - u32 maxVertCount = core.getMaxVertCountByBag(&bag); - - StaPipBagPackagesBBox* bbox = nullptr; - if (bag.info->frustumCulling == PipelineInfoBagFrustumCulling_Precise) { - bbox = cacher.getBBoxesByMesh(materialFrame, maxVertCount); - } - - core.render(&bag, bbox, maxVertCount); + core.render(&bag); deallocDrawBags(&bag, material); } diff --git a/tutorials/09-manual-mode/inc/tutorial_09.hpp b/tutorials/09-manual-mode/inc/tutorial_09.hpp index b82a756..27cb8c8 100644 --- a/tutorials/09-manual-mode/inc/tutorial_09.hpp +++ b/tutorials/09-manual-mode/inc/tutorial_09.hpp @@ -11,6 +11,7 @@ #pragma once #include +#include namespace Tyra { @@ -23,7 +24,22 @@ class Tutorial09 : public Game { void loop(); private: + void setDrawData(); + void loadBags(); + Engine* engine; + + StaticPipeline stapip; + + Vec4 cameraPosition, cameraLookAt; + M4x4 translation, rotation, scale, model; + + std::array vertices; + std::array colors; + + std::unique_ptr bag; + std::unique_ptr infoBag; + std::unique_ptr colorBag; }; } // namespace Tyra diff --git a/tutorials/09-manual-mode/src/tutorial_09.cpp b/tutorials/09-manual-mode/src/tutorial_09.cpp index 27ee5ab..1c44687 100644 --- a/tutorials/09-manual-mode/src/tutorial_09.cpp +++ b/tutorials/09-manual-mode/src/tutorial_09.cpp @@ -13,12 +13,84 @@ namespace Tyra { -Tutorial09::Tutorial09(Engine* t_engine) : engine(t_engine) {} +Tutorial09::Tutorial09(Engine* t_engine) : engine(t_engine) { + translation = M4x4::Identity; + rotation = M4x4::Identity; + scale = M4x4::Identity; + model = M4x4::Identity; +} Tutorial09::~Tutorial09() {} -void Tutorial09::init() {} +void Tutorial09::init() { + stapip.setRenderer(&engine->renderer.core); -void Tutorial09::loop() {} + cameraPosition = Vec4(0.0F, 0.0F, -10.0F); + cameraLookAt.unit(); + + setDrawData(); + loadBags(); +} + +void Tutorial09::loop() { + rotation.rotateY(0.1F); + model = translation * rotation * scale; + + engine->renderer.beginFrame(CameraInfo3D(&cameraPosition, &cameraLookAt)); + { + engine->renderer.renderer3D.usePipeline(stapip); + + /** + * Lets draw manually via "bags". + * You can do the same thing with dynamic pipeline. + */ + stapip.core.render(bag.get()); + } + engine->renderer.endFrame(); +} + +void Tutorial09::setDrawData() { + vertices = { + Vec4(-3.0F, -3.0F, 0.0F), + Vec4(3.0F, -3.0F, 0.0F), + Vec4(0.0F, 3.0F, 0.0F), + }; + + colors = { + Color(128.0F, 0.0F, 0.0F), + Color(0.0F, 128.0F, 0.0F), + Color(0.0F, 0.0F, 128.0F), + }; +} + +void Tutorial09::loadBags() { + /** + * Create info bag and fill it with model matrix and shading type. + * Keep rest of the options as default. + */ + infoBag = std::make_unique(); + infoBag->model = &model; + infoBag->shadingType = TyraShadingGouraud; + + /** Create colors bag and set data pointer */ + colorBag = std::make_unique(); + colorBag->many = colors.begin(); + + /** + * Create main bag and set: + * - vertices data + * - vertices count + * - pointers to other bags + * + * You can add here lighting and texture bag as well. + * If you want to have deeper look of this feature, please + * read static_pipeline.cpp code file. + */ + bag = std::make_unique(); + bag->color = colorBag.get(); + bag->info = infoBag.get(); + bag->vertices = vertices.begin(); + bag->count = vertices.size(); +} } // namespace Tyra