tutorial 1 and 2

This commit is contained in:
h4570
2022-08-20 18:53:27 +02:00
parent d251df8704
commit b4b48ae479
31 changed files with 377 additions and 405 deletions
+9
View File
@@ -19,7 +19,16 @@ class FileUtils {
FileUtils();
~FileUtils();
/**
* Get current working directory.
* Example: "host:" or "mass:"
*/
static std::string getCwd();
/**
* Add current working directory prefix to path
* Example: "host:..." or "mass:..."
*/
static std::string fromCwd(const std::string& relativePath);
static std::string fromCwd(const char* relativePath);
+11
View File
@@ -18,6 +18,17 @@ namespace Tyra {
class Packet2TyraUtils {
public:
inline static void addUnpackData(packet2_t* packet2,
const u32& t_dest_address,
const void* t_data, const u32& t_size,
const u8& t_use_top) {
packet2_chain_ref(packet2, t_data, t_size, 0, 0, 0);
packet2_vif_stcycl(packet2, 0, 0x0101, 0);
packet2_vif_open_unpack(packet2, P2_UNPACK_V4_32, t_dest_address, t_use_top,
0, 1, 0);
packet2_vif_close_unpack_manual(packet2, t_size);
}
inline static void addM4x4(packet2_t* packet2, const M4x4& val) {
*(reinterpret_cast<M4x4*>(packet2->next)) = val;
packet2_advance_next(packet2, sizeof(MATRIX));
+2 -1
View File
@@ -21,7 +21,8 @@ class Renderer2D {
~Renderer2D();
void init(RendererCore* rendererCore);
void render(Sprite* sprite);
void render(const Sprite* sprite);
void render(const Sprite& sprite);
private:
RendererCore* core;
@@ -23,7 +23,7 @@ class DynPipColorBag {
~DynPipColorBag();
/** Mandatory */
Color* single;
const Color* single;
};
} // namespace Tyra
@@ -45,7 +45,8 @@ class DynamicPipeline : public Renderer3DPipeline {
* Render dynamic model.
* This render() method is a bridge to core.render() method.
*/
void render(DynamicMesh* mesh, const DynPipOptions* options = nullptr);
void render(const DynamicMesh& mesh, const DynPipOptions* options = nullptr);
void render(const DynamicMesh* mesh, const DynPipOptions* options = nullptr);
private:
RendererCore* rendererCore;
@@ -57,29 +58,29 @@ class DynamicPipeline : public Renderer3DPipeline {
void sendRestOfBuffers(DynPipBag* buffers, u16* bufferIndex);
void addVertices(MeshMaterialFrame* materialFrameFrom,
MeshMaterialFrame* materialFrameTo, DynPipBag* bag,
void addVertices(const MeshMaterialFrame* materialFrameFrom,
const MeshMaterialFrame* materialFrameTo, DynPipBag* bag,
const u32& startIndex) const;
DynPipInfoBag* getInfoBag(DynamicMesh* mesh, const DynPipOptions* options,
M4x4* model) const;
DynPipInfoBag* getInfoBag(const DynamicMesh* mesh,
const DynPipOptions* options, M4x4* model) const;
DynPipColorBag* getColorBag(MeshMaterial* material) const;
DynPipColorBag* getColorBag(const MeshMaterial* material) const;
DynPipTextureBag* getTextureBag(Texture* texture,
MeshMaterialFrame* materialFrameFrom,
MeshMaterialFrame* materialFrameTo,
const MeshMaterialFrame* materialFrameFrom,
const MeshMaterialFrame* materialFrameTo,
const u32& startIndex);
DynPipLightingBag* getLightingBag(MeshMaterialFrame* materialFrameFrom,
MeshMaterialFrame* materialFrameTo,
DynPipLightingBag* getLightingBag(const MeshMaterialFrame* materialFrameFrom,
const MeshMaterialFrame* materialFrameTo,
M4x4* model, const DynPipOptions* options,
PipelineDirLightsBag* dirLightsBag,
const u32& startIndex) const;
void setLightingColorsCache(PipelineLightingOptions* lightingOptions);
void freeBuffer(DynPipBag* bag);
void setBuffersDefaultVars(DynPipBag* buffers, DynamicMesh* mesh,
void setBuffersDefaultVars(DynPipBag* buffers, const DynamicMesh* mesh,
DynPipInfoBag* infoBag);
void setBuffersColorBag(DynPipBag* buffers, DynPipColorBag* colorBag);
};
@@ -22,10 +22,10 @@ class StaPipBagPackage {
StaPipBag* bag;
Vec4* vertices;
Vec4* sts;
Vec4* normals;
Vec4* colors;
const Vec4* vertices;
const Vec4* sts;
const Vec4* normals;
const Vec4* colors;
/** maxVertCount is max value, because we decided to put max maxVertCount
* verts to single quad buffer in VU1 */
u16 size;
@@ -23,10 +23,10 @@ class StaPipColorBag {
~StaPipColorBag();
/** Optional. Single color for all vertices. */
Color* single;
const Color* single;
/** Optional. Color per vertex. */
Color* many;
const Color* many;
};
} // namespace Tyra
@@ -45,28 +45,30 @@ class StaticPipeline : public Renderer3DPipeline {
* Render static model
* This render() method is a bridge to core.render() method.
*/
void render(StaticMesh* mesh, const StaPipOptions* options = nullptr);
void render(const StaticMesh& mesh, const StaPipOptions* options = nullptr);
void render(const StaticMesh* mesh, const StaPipOptions* options = nullptr);
private:
RendererCore* rendererCore;
Vec4* colorsCache;
void addVertices(MeshMaterialFrame* materialFrame, StaPipBag* bag) const;
void addVertices(const MeshMaterialFrame* materialFrame,
StaPipBag* bag) const;
StaPipInfoBag* getInfoBag(StaticMesh* mesh, const StaPipOptions* options,
M4x4* model) const;
StaPipInfoBag* getInfoBag(const StaticMesh* mesh,
const StaPipOptions* options, M4x4* model) const;
StaPipColorBag* getColorBag(MeshMaterial* material,
MeshMaterialFrame* materialFrame) const;
StaPipColorBag* getColorBag(const MeshMaterial* material,
const MeshMaterialFrame* materialFrame) const;
StaPipTextureBag* getTextureBag(MeshMaterial* material,
MeshMaterialFrame* materialFrame);
StaPipTextureBag* getTextureBag(const MeshMaterial* material,
const MeshMaterialFrame* materialFrame);
StaPipLightingBag* getLightingBag(MeshMaterialFrame* materialFrame,
StaPipLightingBag* getLightingBag(const MeshMaterialFrame* materialFrame,
M4x4* model,
const StaPipOptions* options) const;
void deallocDrawBags(StaPipBag* bag, MeshMaterial* material) const;
void deallocDrawBags(StaPipBag* bag, const MeshMaterial* material) const;
void setLightingColorsCache(PipelineLightingOptions* lightingOptions);
};
@@ -27,8 +27,8 @@ class RendererCore2D {
void init(RendererSettings* settings, clutbuffer_t* clutBuffer);
void render(Sprite* sprite, const RendererCoreTextureBuffers& texBuffers,
Texture* texture);
void render(const Sprite& sprite,
const RendererCoreTextureBuffers& texBuffers, Texture* texture);
private:
void setPrim();
@@ -108,6 +108,8 @@ class TextureRepository {
* Texture IS destructed.
*/
void free(const u32& t_texId);
void free(const Texture* t_tex);
void free(const Texture& t_tex);
private:
std::vector<Texture*> textures;
+5 -3
View File
@@ -17,11 +17,13 @@ Renderer2D::~Renderer2D() {}
void Renderer2D::init(RendererCore* t_rendererCore) { core = t_rendererCore; }
void Renderer2D::render(Sprite* sprite) {
auto* texture = core->texture.repository.getBySpriteOrMesh(sprite->id);
void Renderer2D::render(const Sprite* sprite) { render(*sprite); }
void Renderer2D::render(const Sprite& sprite) {
auto* texture = core->texture.repository.getBySpriteOrMesh(sprite.id);
TYRA_ASSERT(
texture, "Texture for sprite with id: ", sprite->id,
texture, "Texture for sprite with id: ", sprite.id,
"Was not found in texture repository! Did you forget to add texture?");
auto texBuffers = core->texture.useTexture(texture);
@@ -11,6 +11,8 @@
#include "renderer/3d/pipeline/dynamic/core/dynpip_renderer.hpp"
#include "renderer/3d/pipeline/dynamic/core/programs/dynpip_vu1_shared_defines.h"
#include <dma.h>
#include <utility>
#include "packet2/packet2_tyra_utils.hpp"
namespace Tyra {
@@ -114,8 +116,8 @@ void DynPipRenderer::sendObjectData(
if (singleColorEnabled) // Color is placed in 4th slot of
// VU1_LIGHTS_MATRIX_ADDR
packet2_utils_vu_add_unpack_data(objectDataPacket, VU1_SINGLE_COLOR_ADDR,
bag->color->single->rgba, 1, false);
Packet2TyraUtils::addUnpackData(objectDataPacket, VU1_SINGLE_COLOR_ADDR,
bag->color->single->rgba, 1, false);
packet2_utils_vu_open_unpack(objectDataPacket, VU1_OPTIONS_ADDR, false);
{
@@ -51,7 +51,13 @@ void DynamicPipeline::onUseEnd() {
core.deallocateOnUse();
}
void DynamicPipeline::render(DynamicMesh* mesh, const DynPipOptions* options) {
void DynamicPipeline::render(const DynamicMesh& mesh,
const DynPipOptions* options) {
render(&mesh, options);
}
void DynamicPipeline::render(const DynamicMesh* mesh,
const DynPipOptions* options) {
bool optionsManuallyAllocated = false;
if (!options) {
@@ -185,7 +191,7 @@ void DynamicPipeline::sendRestOfBuffers(DynPipBag* buffers, u16* bufferIndex) {
}
void DynamicPipeline::setBuffersDefaultVars(DynPipBag* buffers,
DynamicMesh* mesh,
const DynamicMesh* mesh,
DynPipInfoBag* infoBag) {
for (u32 i = 0; i < buffersCount; i++) {
buffers[i].info = infoBag;
@@ -210,7 +216,7 @@ void DynamicPipeline::freeBuffer(DynPipBag* bag) {
}
}
DynPipInfoBag* DynamicPipeline::getInfoBag(DynamicMesh* mesh,
DynPipInfoBag* DynamicPipeline::getInfoBag(const DynamicMesh* mesh,
const DynPipOptions* options,
M4x4* model) const {
auto* result = new DynPipInfoBag();
@@ -230,22 +236,23 @@ DynPipInfoBag* DynamicPipeline::getInfoBag(DynamicMesh* mesh,
return result;
}
void DynamicPipeline::addVertices(MeshMaterialFrame* materialFrameFrom,
MeshMaterialFrame* materialFrameTo,
void DynamicPipeline::addVertices(const MeshMaterialFrame* materialFrameFrom,
const MeshMaterialFrame* materialFrameTo,
DynPipBag* bag, const u32& startIndex) const {
bag->verticesFrom = &materialFrameFrom->vertices[startIndex];
bag->verticesTo = &materialFrameTo->vertices[startIndex];
}
DynPipColorBag* DynamicPipeline::getColorBag(MeshMaterial* material) const {
DynPipColorBag* DynamicPipeline::getColorBag(
const MeshMaterial* material) const {
auto* result = new DynPipColorBag();
result->single = &material->ambient;
return result;
}
DynPipTextureBag* DynamicPipeline::getTextureBag(
Texture* texture, MeshMaterialFrame* materialFrameFrom,
MeshMaterialFrame* materialFrameTo, const u32& startIndex) {
Texture* texture, const MeshMaterialFrame* materialFrameFrom,
const MeshMaterialFrame* materialFrameTo, const u32& startIndex) {
if (!materialFrameFrom->textureCoords) return nullptr;
auto* result = new DynPipTextureBag();
@@ -259,9 +266,10 @@ DynPipTextureBag* DynamicPipeline::getTextureBag(
}
DynPipLightingBag* DynamicPipeline::getLightingBag(
MeshMaterialFrame* materialFrameFrom, MeshMaterialFrame* materialFrameTo,
M4x4* model, const DynPipOptions* options,
PipelineDirLightsBag* dirLightsBag, const u32& startIndex) const {
const MeshMaterialFrame* materialFrameFrom,
const MeshMaterialFrame* materialFrameTo, M4x4* model,
const DynPipOptions* options, PipelineDirLightsBag* dirLightsBag,
const u32& startIndex) const {
if (!materialFrameFrom->normals || options == nullptr ||
options->lighting == nullptr)
return nullptr;
@@ -43,7 +43,8 @@ StaPipBagPackage* StaPipBagPackager::create(u16* o_size, StaPipBag* data,
if (data->texture) result[i].sts = &data->texture->coordinates[i * size];
if (data->color->many)
result[i].colors = reinterpret_cast<Vec4*>(&data->color->many[i * size]);
result[i].colors =
reinterpret_cast<const Vec4*>(&data->color->many[i * size]);
if (data->lighting) result[i].normals = &data->lighting->normals[i * size];
@@ -37,10 +37,12 @@ void StaPipQBuffer::fillByPointer(const StaPipBagPackage& pkg) {
deallocateDynamicData();
vertices = pkg.vertices;
sts = pkg.sts;
colors = pkg.colors;
normals = pkg.normals;
// Too bad, but in reality const is not violated.
// This class needs refactor
vertices = const_cast<Vec4*>(pkg.vertices);
sts = const_cast<Vec4*>(pkg.sts);
colors = const_cast<Vec4*>(pkg.colors);
normals = const_cast<Vec4*>(pkg.normals);
size = pkg.size;
bag = pkg.bag;
}
@@ -10,6 +10,7 @@
#include "renderer/3d/pipeline/static/core/stapip_qbuffer_renderer.hpp"
#include "renderer/3d/pipeline/static/core/programs/stapip_vu1_shared_defines.h"
#include "packet2/packet2_tyra_utils.hpp"
// #define TYRA_QBUFF_RENDERER_VERBOSE_LOG 1
@@ -135,8 +136,8 @@ void StaPipQBufferRenderer::sendObjectData(
if (singleColorEnabled) // Color is placed in 4th slot of
// VU1_LIGHTS_MATRIX_ADDR
packet2_utils_vu_add_unpack_data(objectDataPacket, VU1_SINGLE_COLOR_ADDR,
bag->color->single->rgba, 1, false);
Packet2TyraUtils::addUnpackData(objectDataPacket, VU1_SINGLE_COLOR_ADDR,
bag->color->single->rgba, 1, false);
packet2_utils_vu_open_unpack(objectDataPacket, VU1_OPTIONS_ADDR, false);
{
@@ -39,7 +39,13 @@ void StaticPipeline::onUseEnd() {
core.deallocateOnUse();
}
void StaticPipeline::render(StaticMesh* mesh, const StaPipOptions* options) {
void StaticPipeline::render(const StaticMesh& mesh,
const StaPipOptions* options) {
render(&mesh, options);
}
void StaticPipeline::render(const StaticMesh* mesh,
const StaPipOptions* options) {
bool optionsManuallyAllocated = false;
if (!options) {
@@ -89,13 +95,13 @@ void StaticPipeline::render(StaticMesh* mesh, const StaPipOptions* options) {
if (optionsManuallyAllocated) delete options;
}
void StaticPipeline::addVertices(MeshMaterialFrame* materialFrame,
void StaticPipeline::addVertices(const MeshMaterialFrame* materialFrame,
StaPipBag* bag) const {
bag->count = materialFrame->count;
bag->vertices = materialFrame->vertices;
}
StaPipInfoBag* StaticPipeline::getInfoBag(StaticMesh* mesh,
StaPipInfoBag* StaticPipeline::getInfoBag(const StaticMesh* mesh,
const StaPipOptions* options,
M4x4* model) const {
auto* result = new StaPipInfoBag();
@@ -117,7 +123,8 @@ StaPipInfoBag* StaticPipeline::getInfoBag(StaticMesh* mesh,
}
StaPipColorBag* StaticPipeline::getColorBag(
MeshMaterial* material, MeshMaterialFrame* materialFrame) const {
const MeshMaterial* material,
const MeshMaterialFrame* materialFrame) const {
auto* result = new StaPipColorBag();
if (material->lightmapFlag) {
@@ -130,7 +137,7 @@ StaPipColorBag* StaticPipeline::getColorBag(
}
StaPipTextureBag* StaticPipeline::getTextureBag(
MeshMaterial* material, MeshMaterialFrame* materialFrame) {
const MeshMaterial* material, const MeshMaterialFrame* materialFrame) {
if (!materialFrame->textureCoords) return nullptr;
auto* result = new StaPipTextureBag();
@@ -149,7 +156,7 @@ StaPipTextureBag* StaticPipeline::getTextureBag(
}
StaPipLightingBag* StaticPipeline::getLightingBag(
MeshMaterialFrame* materialFrame, M4x4* model,
const MeshMaterialFrame* materialFrame, M4x4* model,
const StaPipOptions* options) const {
if (!materialFrame->normals || options == nullptr ||
options->lighting == nullptr)
@@ -181,7 +188,7 @@ void StaticPipeline::setLightingColorsCache(
}
void StaticPipeline::deallocDrawBags(StaPipBag* bag,
MeshMaterial* material) const {
const MeshMaterial* material) const {
if (bag->texture) {
delete bag->texture;
}
@@ -62,15 +62,15 @@ void RendererCore2D::init(RendererSettings* t_settings,
clutBuffer = t_clutBuffer;
}
void RendererCore2D::render(Sprite* sprite,
void RendererCore2D::render(const Sprite& sprite,
const RendererCoreTextureBuffers& texBuffers,
Texture* texture) {
auto* rect = rects[context];
float sizeX, sizeY;
if (sprite->mode == MODE_REPEAT) {
sizeX = sprite->size.x;
sizeY = sprite->size.y;
if (sprite.mode == MODE_REPEAT) {
sizeX = sprite.size.x;
sizeY = sprite.size.y;
} else {
sizeX = static_cast<float>(texture->getWidth());
sizeY = static_cast<float>(texture->getHeight());
@@ -84,24 +84,24 @@ void RendererCore2D::render(Sprite* sprite,
else if (sizeY > sizeX)
texS = texMax / (sizeY / sizeX);
rect->t0.s = sprite->flipHorizontal ? texS : 0.0F;
rect->t0.t = sprite->flipVertical ? texT : 0.0F;
rect->t1.s = sprite->flipHorizontal ? 0.0F : texS;
rect->t1.t = sprite->flipVertical ? 0.0F : texT;
rect->t0.s = sprite.flipHorizontal ? texS : 0.0F;
rect->t0.t = sprite.flipVertical ? texT : 0.0F;
rect->t1.s = sprite.flipHorizontal ? 0.0F : texS;
rect->t1.t = sprite.flipVertical ? 0.0F : texT;
rect->color.r = sprite->color.r;
rect->color.g = sprite->color.g;
rect->color.b = sprite->color.b;
rect->color.a = sprite->color.a;
rect->color.r = sprite.color.r;
rect->color.g = sprite.color.g;
rect->color.b = sprite.color.b;
rect->color.a = sprite.color.a;
rect->color.q = 0;
rect->v0.x = sprite->position.x;
rect->v0.y = sprite->position.y;
rect->v0.x = sprite.position.x;
rect->v0.y = sprite.position.y;
// rect->v0.y /= 2.0F; // interlacing
rect->v0.z = (u32)-1;
rect->v1.x = (sprite->size.x * sprite->scale) + sprite->position.x;
rect->v1.y = (sprite->size.y * sprite->scale) + sprite->position.y;
rect->v1.x = (sprite.size.x * sprite.scale) + sprite.position.x;
rect->v1.y = (sprite.size.y * sprite.scale) + sprite.position.y;
// rect->v1.y /= 2.0F; // interlacing
rect->v1.z = (u32)-1;
@@ -56,6 +56,10 @@ void TextureRepository::removeById(const u32& t_texId) {
removeByIndex(index);
}
void TextureRepository::free(const Texture* t_tex) { free(t_tex->id); }
void TextureRepository::free(const Texture& t_tex) { free(t_tex.id); }
void TextureRepository::free(const u32& t_texId) {
s32 index = getIndexOf(t_texId);
auto* tex = textures[index];