tyrav2 init

This commit is contained in:
h4570
2022-07-17 10:21:35 +02:00
parent 44c1ee4fe8
commit c8b22ff331
249 changed files with 18187 additions and 0 deletions
@@ -0,0 +1,44 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include "debug/debug.hpp"
#include "renderer/core/2d/sprite/sprite.hpp"
#include "renderer/core/texture/renderer_core_texture_buffers.hpp"
#include "renderer/core/texture/models/texture.hpp"
#include "renderer/renderer_settings.hpp"
#include <packet2_utils.h>
#include <draw2d.h>
namespace Tyra {
class RendererCore2D {
public:
RendererCore2D();
~RendererCore2D();
void init(RendererSettings* settings, clutbuffer_t* clutBuffer);
void render(Sprite* sprite, const RendererCoreTextureBuffers& texBuffers,
Texture* texture);
private:
static const float GS_CENTER;
static const float SCREEN_CENTER;
u8 context;
RendererSettings* settings;
clutbuffer_t* clutBuffer;
packet2_t* packets[2];
texrect_t* rects[2];
};
} // namespace Tyra
@@ -0,0 +1,71 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <draw_types.h>
#include <draw_buffers.h>
#include "./sprite_mode.hpp"
#include "math/vec2.hpp"
#include "renderer/models/color.hpp"
namespace Tyra {
class Sprite {
public:
Sprite();
~Sprite();
Vec2 position, size;
float scale;
Color color;
// ----
// Getters
// ----
/** Auto generated unique Id. */
inline const u32& getId() const { return id; };
/** Get sprite drawing mode. */
inline const SpriteMode& getMode() const { return mode; };
// ----
// Setters
// ----
/** Set sprite drawing mode. */
void setMode(const SpriteMode& t_val) { mode = t_val; }
// ----
// Other
// ----
/** Flip texture horizontally. */
inline void flipHorizontally(const u8& t_set) { _flipH = t_set; };
/** Flip texture vertically. */
inline void flipVertically(const u8& t_set) { _flipV = t_set; };
/** Check if texture is vertically flipped. */
inline const u8& isFlippedVertically() const { return _flipV; };
/** Check if texture is horizontally flipped. */
inline const u8& isFlippedHorizontally() const { return _flipH; };
private:
u32 id;
SpriteMode mode;
u8 _isSizeSet, _flipH, _flipV;
void setDefaultColor();
void setDefaultLODAndClut();
};
} // namespace Tyra
@@ -0,0 +1,17 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
namespace Tyra {
enum SpriteMode { MODE_STRETCH, MODE_REPEAT };
}
@@ -0,0 +1,43 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <vector>
#include "./core_bbox_frustum.hpp"
#include "math/m4x4.hpp"
#include "math/plane.hpp"
namespace Tyra {
/** Bounding box */
class CoreBBox {
public:
explicit CoreBBox(Vec4* t_vertices, u32* faces, u32 t_count);
explicit CoreBBox(Vec4* t_vertices, u32 t_count);
explicit CoreBBox(Vec4* t_vertices);
explicit CoreBBox(CoreBBox** t_bboxes, const u32& count);
explicit CoreBBox(const std::vector<CoreBBox>& t_bboxes,
const u32& startIndex, const u32& stopIndex);
const Vec4& operator[](const u8& i) { return _vertices[i]; }
const Vec4& getVertex(const u8& i) { return _vertices[i]; }
Vec4* getVertices() { return _vertices; }
void print() const;
void print(const char* name) const;
void print(const std::string& name) const { print(name.c_str()); }
std::string getPrint(const char* name = nullptr) const;
protected:
Vec4 _vertices[8];
};
} // namespace Tyra
@@ -0,0 +1,17 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
namespace Tyra {
enum CoreBBoxFrustum { IN_FRUSTUM, OUTSIDE_FRUSTUM, PARTIALLY_IN_FRUSTUM };
} // namespace Tyra
@@ -0,0 +1,35 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <vector>
#include "./core_bbox.hpp"
namespace Tyra {
/** Bounding box */
class RenderBBox : public CoreBBox {
public:
explicit RenderBBox(Vec4* t_vertices, u32* faces, u32 t_count);
explicit RenderBBox(Vec4* t_vertices, u32 t_count);
explicit RenderBBox(Vec4* t_vertices);
explicit RenderBBox(CoreBBox** t_bboxes, const u32& count);
explicit RenderBBox(const std::vector<CoreBBox>& t_bboxes,
const u32& startIndex, const u32& stopIndex);
CoreBBoxFrustum isInFrustum(const Plane* frustumPlanes, const M4x4& model,
const float* margins = nullptr) const;
CoreBBoxFrustum clipIsInFrustum(const Plane* frustumPlanes,
const M4x4& model) const;
};
} // namespace Tyra
@@ -0,0 +1,29 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include "math/vec4.hpp"
namespace Tyra {
class CameraInfo3D {
public:
CameraInfo3D(Vec4* cameraPosition, Vec4* cameraLooksAt,
Vec4* cameraUp = nullptr);
~CameraInfo3D();
Vec4 *position, *looksAt, *up;
private:
static Vec4 defaultCameraUp;
};
} // namespace Tyra
@@ -0,0 +1,45 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <sstream>
#include "renderer/renderer_settings.hpp"
#include "./camera_info_3d.hpp"
#include "math/plane.hpp"
namespace Tyra {
class Renderer3DFrustumPlanes {
public:
Renderer3DFrustumPlanes();
~Renderer3DFrustumPlanes();
void init(RendererSettings* settings, const float& fov);
void update(const CameraInfo3D& cameraInfo, const float& fov);
const Plane& get(u8 index) const { return frustumPlanes[index]; }
const Plane* getAll() const { return frustumPlanes; }
const Plane& operator[](u8 index) const { return frustumPlanes[index]; }
void print() const;
void print(const char* name) const;
void print(const std::string& name) const { print(name.c_str()); }
std::string getPrint(const char* name = nullptr) const;
private:
void computeStaticData(const float& fov);
float lastFov;
RendererSettings* settings;
Plane frustumPlanes[6];
float nearHeight, nearWidth, farHeight, farWidth;
Vec4 nearCenter, farCenter, X, Y, Z, ntl, ntr, nbl, nbr, ftl, fbr, ftr, fbl;
};
} // namespace Tyra
@@ -0,0 +1,90 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <algorithm>
#include "renderer/renderer_settings.hpp"
#include "./renderer_3d_frustum_planes.hpp"
#include "./camera_info_3d.hpp"
#include "renderer/core/texture/models/texture.hpp"
#include "renderer/core/3d/bbox/core_bbox.hpp"
#include "renderer/core/paths/path1/path1.hpp"
namespace Tyra {
class RendererCore3D {
public:
RendererCore3D();
~RendererCore3D();
/** Current camera frustum planes. */
Renderer3DFrustumPlanes frustumPlanes;
/** Called by renderer. */
void init(RendererSettings* settings, Path1* t_path1);
const float& getFov() const { return fov; }
void setFov(const float& t_fov);
/**
* Called by beginFrame();
* Updates camera info, to get proper frustum culling.
*/
void update(const CameraInfo3D& cameraInfo);
/** Get projection (screen) matrix */
const M4x4& getProjection() { return projection; }
/**
* Get view (camera) matrix
* Updated at every beginFrame()
*/
const M4x4& getView() { return view; }
/**
* Get projection * view matrix
* Updated at every beginFrame()
*/
const M4x4& getViewProj() { return viewProj; }
/**
* @brief
* Upload VU1 program.
* Please pay attention that you will be REPLACING
* current VU1 programs
*
* @param address Starting address of your program.
* @return Address of the end of your program, so next program can start from
* this + 1
*/
u32 uploadVU1Program(VU1Program* program, const u32& address);
/**
* @brief Set VU1 double buffer
*
* @param startingAddress Starting address. Example 10.
* @param bufferSize Buffer size. Example 490, so second buffer will start
* from 490+10
*/
void setVU1DoubleBuffers(const u16& startingAddress, const u16& bufferSize);
private:
M4x4 view, projection, viewProj;
float fov;
RendererSettings* settings;
Path1* path1;
void setProjection();
};
} // namespace Tyra
@@ -0,0 +1,51 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include "renderer/renderer_settings.hpp"
namespace Tyra {
class RendererCoreGS {
public:
RendererCoreGS();
~RendererCoreGS();
prim_t prim;
lod_t lod;
zbuffer_t zBuffer;
void init(RendererSettings* settings);
void flipBuffers();
float getVRamFreeSpaceInMB() const {
return 4.0F - spaceOccupiedByFrameBuffers - spaceOccupiedByZBuffer;
}
private:
constexpr static float gsCenter = 4096.0F;
constexpr static float screenCenter = gsCenter / 2.0F;
float spaceOccupiedByFrameBuffers, spaceOccupiedByZBuffer;
RendererSettings* settings;
framebuffer_t frameBuffers[2];
packet2_t* flipPacket;
u8 context;
void allocateBuffers();
void initDrawingEnvironment();
void setPrim();
void setLod();
void initChannels();
};
} // namespace Tyra
@@ -0,0 +1,21 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include "math/vec4.hpp"
namespace Tyra {
struct Path1ClipVertex {
Vec4 position, normal, st, color;
};
} // namespace Tyra
@@ -0,0 +1,52 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <vector>
#include "./path1_clip_vertex.hpp"
#include "debug/debug.hpp"
#include "renderer/renderer_settings.hpp"
namespace Tyra {
struct Path1EEClipAlgorithmSettings {
bool lerpNormals, lerpTexCoords, lerpColors;
};
class Path1EEClipAlgorithm {
public:
Path1EEClipAlgorithm();
~Path1EEClipAlgorithm();
void init(const RendererSettings& settings);
void clip(std::vector<Path1ClipVertex>* o_vertices,
const std::vector<Path1ClipVertex>& vertices,
const Path1EEClipAlgorithmSettings& settings);
static float clipMargin;
private:
float halfWidth, halfHeight, near, far;
std::vector<Path1ClipVertex> tempVertices;
float getValueByPlane(const Path1ClipVertex& v, const int& plane);
bool isInside(const int& plane, const float& v, const float& w,
const float& planeLimitValue);
void clipAgainstPlane(const std::vector<Path1ClipVertex>& original,
std::vector<Path1ClipVertex>* clipped, const int& plane,
const float& planeLimitValue,
const Path1EEClipAlgorithmSettings& settings);
};
} // namespace Tyra
@@ -0,0 +1,36 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <dma.h>
#include <packet2_utils.h>
#include "debug/debug.hpp"
#include "./vu1_program.hpp"
namespace Tyra {
class Path1 {
public:
Path1();
~Path1();
u32 uploadProgram(VU1Program* program, const u32& address);
packet2_t* createProgramsCache(VU1Program** programs, const u32& count,
const u32& address);
void setDoubleBuffer(const u16& startingAddress, const u16& bufferSize);
private:
packet2_t* doubleBufferPacket;
};
} // namespace Tyra
@@ -0,0 +1,28 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <packet2_utils.h>
#include <string>
#include "../../vu1_program.hpp"
namespace Tyra {
class VU1DrawFinish : public VU1Program {
public:
VU1DrawFinish();
~VU1DrawFinish();
std::string getStringName() const;
void addTag(packet2_t* packet, prim_t* prim) const;
};
} // namespace Tyra
@@ -0,0 +1,41 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <tamtypes.h>
#include <packet2_utils.h>
#include <string>
namespace Tyra {
class VU1Program {
public:
VU1Program(u32* start, u32* end);
~VU1Program();
const u32& getPacketSize() const;
const u32& getProgramSize() const;
const u32& getDestinationAddress() const;
u32* getStart() const;
u32* getEnd() const;
virtual std::string getStringName() const = 0;
void setDestinationAddress(const u32& addr);
protected:
u32 packetSize, destinationAddress, programSize;
u32 *start, *end;
u32 calculateProgramSize() const;
};
} // namespace Tyra
@@ -0,0 +1,42 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <dma.h>
#include <packet2_chain.h>
#include <draw.h>
#include "renderer/core/texture/models/texture.hpp"
#include "renderer/renderer_settings.hpp"
#include "renderer/models/color.hpp"
#include "renderer/core/texture/renderer_core_texture_buffers.hpp"
namespace Tyra {
class Path3 {
public:
Path3();
~Path3();
void init(RendererSettings* settings);
void addDrawFinishTag();
void clearScreen(zbuffer_t* z, const Color& color);
void sendTexture(Texture* texture,
const RendererCoreTextureBuffers& texBuffers);
private:
packet2_t* drawFinishPacket;
packet2_t* clearScreenPacket;
packet2_t* texturePacket;
RendererSettings* settings;
};
} // namespace Tyra
@@ -0,0 +1,73 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <tamtypes.h>
#include "./2d/renderer_core_2d.hpp"
#include "./3d/renderer_core_3d.hpp"
#include "./gs/renderer_core_gs.hpp"
#include "./texture/renderer_core_texture.hpp"
#include "./paths/path3/path3.hpp"
#include "./paths/path1/path1.hpp"
#include "./renderer_core_sync.hpp"
namespace Tyra {
class RendererCore {
public:
RendererCore();
~RendererCore();
/** Responsible for initializing GS. */
RendererCoreGS gs;
/** All logic responsible for 3D drawing. */
RendererCore3D renderer3D;
/** All logic responsible for 2D drawing. */
RendererCore2D renderer2D;
/** Texture transferring. */
RendererCoreTexture texture;
/** EE <-> VU1 synchronization */
RendererCoreSync sync;
/** Called by renderer */
void init();
/** World background color */
void setClearScreenColor(const Color& color);
/** Clear screen and update view frustum for frustum culling. */
void beginFrame(const CameraInfo3D& cameraInfo);
/** VSync and swap frame double buffer. */
void endFrame();
void setFrameLimit(const bool& onoff) { isFrameLimitOn = onoff; }
/** Get screen settings */
const RendererSettings& getSettings() const { return settings; }
Path1* getPath1() { return &path1; }
Path3* getPath3() { return &path3; }
private:
bool isFrameLimitOn;
Color bgColor;
RendererSettings settings;
Path3 path3;
Path1 path1;
};
} // namespace Tyra
@@ -0,0 +1,50 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <draw.h>
#include <gs_privileged.h>
#include "./paths/path3/path3.hpp"
namespace Tyra {
/**
* Synchronization class.
* Mainly between VU1 and EE.
*
* For example you can set texture, render X vertices, then add() wait, and
* wait() for it. Without it, there is risk for example to send new texture
* during drawing with previous one.
*/
class RendererCoreSync {
public:
RendererCoreSync();
~RendererCoreSync();
void init(Path3* path3);
/** Send draw finish tag via VU1 to GS. */
void add();
/** Check if finish flag was set by GS. */
u8 check();
/** Clear finish tag flag. */
void clear();
/** Wait for finish flag and clear it. */
void waitAndClear();
/** Clear() -> Add() -> waitAndClear() */
void align();
private:
Path3* path3;
};
} // namespace Tyra
@@ -0,0 +1,48 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <tamtypes.h>
#include <draw_buffers.h>
#include <vector>
#include "renderer/core/texture/models/texture.hpp"
#include "./renderer_texture_cm_analysis.hpp"
#include "../renderer_core_texture_buffers.hpp"
namespace Tyra {
class RendererTextureCacheManager {
public:
RendererTextureCacheManager();
~RendererTextureCacheManager();
void onFrameChange();
void addRequestedTexture(Texture* texture);
u32 getTextureIdToDealloc(
const std::vector<RendererCoreTextureBuffers>& currentAllocs);
private:
RendererTextureCMAnalysis statistics;
void tryAddIdThatNotExistInAnalysis(
std::vector<u32>* result,
const std::vector<RendererCoreTextureBuffers>& currentAllocs,
const std::vector<u32>& analysis);
void addIdThatExistInAnalysisButIsOnTheBottom(
std::vector<u32>* result,
const std::vector<RendererCoreTextureBuffers>& currentAllocs,
const std::vector<u32>& analysis);
};
} // namespace Tyra
@@ -0,0 +1,73 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <algorithm>
#include "debug/debug.hpp"
#include <tamtypes.h>
#include <vector>
namespace Tyra {
struct RendererTextureCMAnalysisWeight {
u32 id;
u16 weight;
};
struct RendererTextureCMAnalysisOccurence {
u32 id;
u8 occurence;
};
class RendererTextureCMAnalysis {
public:
RendererTextureCMAnalysis();
~RendererTextureCMAnalysis();
void changeFrame();
void addNewRequest(const u32& id);
/**
* @brief Returns top list (from most used to least used) of textures to be
* deallocated.
*
* @param count Number of textures to be returned.
*/
std::vector<u32> getTopTextureIdsForNextChanges(const u8& count);
bool isReady();
bool isProbablyNotCorrect();
private:
std::vector<u32>*current, *last;
std::vector<u32> buffers[2];
u32 currentIndex;
void flipBuffers();
std::vector<u32> getNextTextureIds(const u8& count);
std::vector<RendererTextureCMAnalysisOccurence> getOccurencesForTextureIds(
const std::vector<u32>& textureIds);
std::vector<u32> getTextureIdsUnique(const std::vector<u32>& textureIds);
std::vector<RendererTextureCMAnalysisWeight> getWeights(
const std::vector<u32>& uniqueTexIds,
const std::vector<RendererTextureCMAnalysisOccurence>& occurencies);
std::vector<u32> weightsToResult(
std::vector<RendererTextureCMAnalysisWeight>* weights);
};
} // namespace Tyra
@@ -0,0 +1,140 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2020, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <tamtypes.h>
#include <vector>
#include <draw_sampling.h>
#include "./texture_link.hpp"
#include "./texture_wrap.hpp"
#include "../texture_data.hpp"
#include "loaders/texture/builder/texture_builder_data.hpp"
#include "debug/debug.hpp"
namespace Tyra {
/**
* Class which contains texture data.
* Textures are paired with meshes/sprites via addLink() and
* removeLink() functions which use meshId/spriteId and materialId (for mesh).
*/
class Texture {
public:
Texture(TextureBuilderData* data);
~Texture();
inline const u32& getId() const { return id; }
inline const int& getWidth() const { return core->width; }
inline const int& getHeight() const { return core->height; }
float getSizeInMB() const;
inline u32 getTextureLinksCount() const {
return static_cast<u32>(links.size());
}
inline texwrap_t* getWrapSettings() { return &wrap; }
const TextureData& getCoreData() const { return *core; }
TextureData* getClutData() { return clut; }
/**
* Get texture name.
* For "textures/abc.bmp" result will be: "abc"
*/
inline const std::string& getName() const { return name; }
/** Array of texture links. Size of getTextureLinksCount() */
inline const std::vector<TextureLink>& getTextureLinks() const {
return links;
}
/**
* Returns index of link.
* -1 if not found.
* @param t_id
* For 3D: MeshMaterial material id.
* For 2D: Sprite id.
*/
const s32 getIndexOfLink(const u32& t_id) const {
for (u32 i = 0; i < links.size(); i++)
if (links[i].id == t_id) return i;
return -1;
}
/** Set texture wrapping */
void setWrapSettings(const TextureWrap t_horizontal,
const TextureWrap t_vertical);
// ----
// Other
// ----
/** Assign texture to mesh and mesh material. */
void addLink(const u32& t_meshId, const u32& t_materialId);
/** Assign texture to sprite. */
void addLink(const u32& t_spriteId);
u32 getTextureSize() const;
/**
* Check if texture is linked with MeshMaterial/Sprite.
* @param t_id
* For 3D: MeshMaterial material id.
* For 2D: Sprite id.
*/
const u8 isLinkedWith(const u32& t_id) const {
s32 index = getIndexOfLink(t_id);
if (index != -1)
return true;
else
return false;
}
void removeLinkByIndex(const u32& t_index) {
links.erase(links.begin() + t_index);
}
/**
* Remove texture link with given MeshMaterial/Sprite.
* @param t_id
* For 3D: MeshMaterial material id.
* For 2D: Sprite id.
*/
void removeLinkById(const u32& t_id) {
s32 index = getIndexOfLink(t_id);
TYRA_ASSERT(index != -1, "Cant remove link, because it was not found!");
removeLinkByIndex(index);
}
void print() const;
void print(const char* name) const;
void print(const std::string& name) const { print(name.c_str()); }
std::string getPrint(const char* name = nullptr) const;
private:
void setPsm();
void setDefaultWrapSettings();
u32 id;
std::string name;
TextureData* core;
TextureData* clut;
texwrap_t wrap;
std::vector<TextureLink> links;
};
} // namespace Tyra
@@ -0,0 +1,44 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2020, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
namespace Tyra {
enum TextureBpp {
// Tyra has 1.3MB~ free VRAM for textures.
/**
* @brief 32-bit RGBA - Slowest
* Tyra can cache about 4~ of these textures (256x256)
*/
bpp32 = 32,
/**
* @brief 24-bit RGB - Slow
* Tyra can cache about 6~ of these textures (256x256)
*/
bpp24 = 24,
/**
* @brief 8-bit palletized (indexed) - Super fast
* Tyra can cache about 19~ of these textures (256x256)
*/
bpp8 = 8,
/**
* @brief 4-bit palletized (indexed) - Super, super fast
* Tyra can cache about 39~ of these textures (256x256)
*/
bpp4 = 4,
};
} // namespace Tyra
@@ -0,0 +1,23 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2020, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <tamtypes.h>
namespace Tyra {
struct TextureLink {
/**
* For 3D: MeshMaterial material id.
* For 2D: Sprite id.
*/
u32 id;
};
} // namespace Tyra
@@ -0,0 +1,15 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2020, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
namespace Tyra {
enum TextureWrap { Repeat = 0, Clamp = 1, RegionClamp = 2, RegionRepeat = 3 };
}
@@ -0,0 +1,55 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <tamtypes.h>
#include <vector>
#include "./texture_repository.hpp"
#include "./renderer_core_texture_sender.hpp"
#include "renderer/core/paths/path3/path3.hpp"
#include "./cache_manager/renderer_texture_cache_manager.hpp"
#include "./renderer_core_texture_buffers.hpp"
namespace Tyra {
class RendererCoreTexture {
public:
RendererCoreTexture();
~RendererCoreTexture();
clutbuffer_t clut;
TextureRepository repository;
RendererCoreTextureBuffers useTexture(Texture* t_tex);
/** Called by renderer during initialization */
void init(RendererCoreGS* gs, Path3* path3);
/** Called by renderer during beginFrame() */
void onFrameChange();
/** Called by renderer during rendering */
void updateClutBuffer(texbuffer_t* clutBuffer);
private:
RendererTextureCacheManager cacheManager;
std::vector<RendererCoreTextureBuffers> currentAllocations;
void initClut();
void registerAllocation(const RendererCoreTextureBuffers& t_buffers);
void unregisterAllocation(const u32& textureId);
RendererCoreTextureBuffers getAllocatedBuffersByTextureId(const u32& id);
RendererCoreTextureSender sender;
Path3* path3;
};
} // namespace Tyra
@@ -0,0 +1,35 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <tamtypes.h>
#include <draw_buffers.h>
namespace Tyra {
struct RendererCoreTextureBuffers {
/** Texture id */
u32 id;
/**
* Texture data.
* Used in: 4bpp, 8bpp, 24bpp, 32bpp.
*/
texbuffer_t* core;
/**
* Texture pallete data.
* Used in: 4bpp, 8bpp (otherwise nullptr).
*/
texbuffer_t* clut;
};
} // namespace Tyra
@@ -0,0 +1,49 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <tamtypes.h>
#include <draw_buffers.h>
#include <graph.h>
#include "./models/texture.hpp"
#include "renderer/core/paths/path3/path3.hpp"
#include "renderer/core/gs/renderer_core_gs.hpp"
#include "./renderer_core_texture_buffers.hpp"
namespace Tyra {
class RendererCoreTextureSender {
public:
RendererCoreTextureSender();
~RendererCoreTextureSender();
void init(Path3* path3, RendererCoreGS* gs);
RendererCoreTextureBuffers allocate(Texture* t_texture);
void deallocate(const RendererCoreTextureBuffers& texBuffers);
float getFreeVRamInMB();
float getSizeInMB(texbuffer_t* texBuffer);
private:
float allocatedVRamMemForTextures;
RendererCoreGS* gs;
Path3* path3;
TextureBpp getBppByPsm(const u32& psm);
texbuffer_t* allocateTextureCore(Texture* t_texture);
texbuffer_t* allocateTextureClut(Texture* t_texture);
u8 isTextureVRAMAllocated, isVSyncEnabled;
};
} // namespace Tyra
@@ -0,0 +1,40 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2020, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <tamtypes.h>
#include <string>
#include "renderer/core/texture/models/texture_bpp.hpp"
namespace Tyra {
class TextureData {
public:
TextureData(unsigned char* data, const TextureBpp& bpp,
const unsigned char& gsComponents, const int& width,
const int& height);
~TextureData();
int width, height;
u32 psm;
unsigned char* data;
TextureBpp bpp;
unsigned char components;
void print() const;
void print(const unsigned char* name) const;
void print(const std::string& name) const { print(name.c_str()); }
std::string getPrint(const unsigned char* name = nullptr) const;
private:
u32 getPsmByBpp(const TextureBpp& bpp);
};
} // namespace Tyra
@@ -0,0 +1,128 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2020, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <tamtypes.h>
#include <draw_buffers.h>
#include <vector>
#include "./models/texture.hpp"
#include "renderer/3d/mesh/mesh.hpp"
#include "loaders/texture/base/texture_loader_selector.hpp"
#include <string>
namespace Tyra {
class TextureRepository {
public:
TextureRepository();
~TextureRepository();
/** Returns all repository textures. */
std::vector<Texture*>* getAll() { return &textures; }
u32 getTexturesCount() const { return static_cast<u32>(textures.size()); }
/**
* Returns single texture.
* nullptr if not found.
* @param t_id
* For 3D: MeshMaterial id.
* For 2D: Sprite id.
*/
Texture* getBySpriteOrMesh(const u32& t_id) {
for (u32 i = 0; i < textures.size(); i++)
if (textures[i]->isLinkedWith(t_id)) return textures[i];
return nullptr;
}
/**
* Returns single texture.
* nullptr if not found.
*/
Texture* getByTextureId(const u32& t_id) const {
for (u32 i = 0; i < textures.size(); i++)
if (t_id == textures[i]->getId()) return textures[i];
return nullptr;
}
/**
* Returns index of link.
* -1 if not found.
*/
const s32 getIndexOf(const u32& t_texId) const {
for (u32 i = 0; i < textures.size(); i++)
if (textures[i]->getId() == t_texId) return i;
return -1;
}
// ----
// Setters
// ----
// ----
// Other
// ----
/**
* Add unlinked texture.
* @param fullpath Full path to texture file. Example: "host:texture.png"
*/
Texture* add(const char* fullpath);
/**
* Add unlinked texture.
* @param fullpath Full path to texture file. Example: "host:texture.png"
*/
inline Texture* add(const std::string& fullpath) {
return add(fullpath.c_str());
}
/**
* Add unlinked texture.
* @param texture Your own texture
*/
Texture* add(Texture* texture);
/**
* Add linked textures in given path for mesh material names.
*/
void addByMesh(Mesh* mesh, const char* directory, const char* extension);
/**
* Add linked textures in given path for mesh material names.
*/
inline void addByMesh(Mesh* mesh, const std::string& directory,
const char* extension) {
addByMesh(mesh, directory.c_str(), extension);
}
/**
* Remove texture from repository.
* Texture is NOT destructed.
*/
void removeByIndex(const u32& t_index) {
textures.erase(textures.begin() + t_index);
}
/**
* Remove texture from repository.
* Texture is NOT destructed.
*/
const void removeById(const u32& t_texId) {
s32 index = getIndexOf(t_texId);
TYRA_ASSERT(index != -1, "Cant remove texture, because it was not found!");
removeByIndex(index);
}
private:
std::vector<Texture*> textures;
TextureLoaderSelector texLoaderSelector;
};
} // namespace Tyra