finished tutorial 9

This commit is contained in:
h4570
2022-08-24 19:27:16 +02:00
parent 5c375dd24f
commit a21bcee671
11 changed files with 130 additions and 41 deletions
-3
View File
@@ -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
@@ -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();
@@ -11,7 +11,7 @@
#pragma once
#include <tamtypes.h>
#include "core/bag/packaging/stapip_bag_packages_bbox.hpp"
#include "./bag/packaging/stapip_bag_packages_bbox.hpp"
#include "renderer/3d/mesh/mesh.hpp"
#include <memory>
#include <vector>
@@ -20,7 +20,7 @@ namespace Tyra {
struct StapipBagBBoxesCacheItem {
u32 vu1MaxVertCount;
u32 frameId;
u32 id;
std::unique_ptr<StaPipBagPackagesBBox> 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<StapipBagBBoxesCacheItem> storage;
};
@@ -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;
@@ -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;
@@ -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;
@@ -8,7 +8,7 @@
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#include "renderer/3d/pipeline/static/stapip_bag_bboxes_cacher.hpp"
#include "renderer/3d/pipeline/static/core/stapip_bag_bboxes_cacher.hpp"
#include <algorithm>
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<StaPipBagPackagesBBox>(
frame->vertices, frame->count, maxVertCount);
auto bboxes =
std::make_unique<StaPipBagPackagesBBox>(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;
}
}
@@ -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<u32>(bag->vertices), maxVertCount);
}
setMaxVertCount(maxVertCount);
CoreBBoxFrustum frustumCheck = OUTSIDE_FRUSTUM;
@@ -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);
}
@@ -11,6 +11,7 @@
#pragma once
#include <tyra>
#include <memory>
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<Vec4, 3> vertices;
std::array<Color, 3> colors;
std::unique_ptr<StaPipBag> bag;
std::unique_ptr<StaPipInfoBag> infoBag;
std::unique_ptr<StaPipColorBag> colorBag;
};
} // namespace Tyra
+75 -3
View File
@@ -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<StaPipInfoBag>();
infoBag->model = &model;
infoBag->shadingType = TyraShadingGouraud;
/** Create colors bag and set data pointer */
colorBag = std::make_unique<StaPipColorBag>();
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<StaPipBag>();
bag->color = colorBag.get();
bag->info = infoBag.get();
bag->vertices = vertices.begin();
bag->count = vertices.size();
}
} // namespace Tyra