some optimizations

This commit is contained in:
h4570
2022-07-26 23:16:21 +02:00
parent 97e44507ff
commit 10164beadf
14 changed files with 177 additions and 107 deletions
@@ -48,8 +48,9 @@ class McpipClip {
packet2_t* staticPacket;
u16 vu1DBufferSize;
std::vector<EEClipVertex> clippedTriangle;
std::vector<EEClipVertex> inputTriangle;
Vec4 inputVerts[3];
EEClipVertexPtrs inputTriangle[3];
EEClipVertex clippedTriangle[9];
Vec4 vertexBuffers[2][108];
Vec4 texCoordBuffers[2][108];
@@ -18,7 +18,12 @@ namespace Tyra {
class PipelineOptions {
public:
PipelineOptions() { lighting = nullptr; }
PipelineOptions() {
lighting = nullptr;
antiAliasingEnabled = false;
blendingEnabled = true;
shadingType = TyraShadingFlat;
}
~PipelineOptions() {}
PipelineFrustumCulling frustumCulling;
@@ -39,6 +39,10 @@ class StaPipClipper {
EEClipAlgorithm algorithm;
M4x4* mvp;
Vec4 inputVerts[3];
EEClipVertexPtrs inputTriangle[3];
EEClipVertex clippedTriangle[9];
void perspectiveDivide(std::vector<EEClipVertex>* vertices);
void moveDataToBuffer(const std::vector<EEClipVertex>& vertices,
StaPipQBuffer* buffer);
@@ -28,25 +28,25 @@ class EEClipAlgorithm {
void init(const RendererSettings& settings);
void clip(std::vector<EEClipVertex>* o_vertices,
const std::vector<EEClipVertex>& vertices,
const EEClipAlgorithmSettings& settings);
u8 clip(EEClipVertex* o_vertices, EEClipVertexPtrs* i_vertices,
const EEClipAlgorithmSettings& settings);
static float clipMargin;
private:
float halfWidth, halfHeight, near, far;
std::vector<EEClipVertex> tempVertices;
EEClipVertex* tempVertices;
float getValueByPlane(const EEClipVertex& v, const int& plane);
bool isInside(const int& plane, const float& v, const float& w,
const float& planeLimitValue);
void clipAgainstPlane(const std::vector<EEClipVertex>& original,
std::vector<EEClipVertex>* clipped, const int& plane,
const float& planeLimitValue,
const EEClipAlgorithmSettings& settings);
/** @return clipped size */
u8 clipAgainstPlane(EEClipVertex* original, const u8& originalSize,
EEClipVertex* clipped, const int& plane,
const float& planeLimitValue,
const EEClipAlgorithmSettings& settings);
};
} // namespace Tyra
@@ -18,4 +18,8 @@ struct EEClipVertex {
Vec4 position, normal, st, color;
};
struct EEClipVertexPtrs {
Vec4 *position, *normal, *st, *color;
};
} // namespace Tyra
@@ -31,7 +31,9 @@ MeshBuilderData* TyrobjLoader::load(const char* fullpath, const u16& count,
auto rawFilename = getFilenameWithoutExtension(path);
auto extension = getExtensionOfFilename(path);
auto firstFile = rawFilename + "1." + extension;
std::string firstFile = path;
if (count > 1) firstFile = rawFilename + "1." + extension;
FILE* firstFileHandler = fopen(firstFile.c_str(), "rb");
TYRA_ASSERT(firstFileHandler != nullptr, "Failed to load: ", firstFile);
auto data = scan(firstFileHandler, count);
@@ -46,10 +48,12 @@ MeshBuilderData* TyrobjLoader::load(const char* fullpath, const u16& count,
result->manyColorsEnabled = data.colorsCount > 0;
for (u16 i = 1; i <= count; i++) {
auto path = rawFilename + std::to_string(i) + "." + extension;
FILE* fileHandler = fopen(path.c_str(), "rb");
TYRA_ASSERT(fileHandler != nullptr, "Failed to load: ", path);
loadFile(fileHandler, path, i - 1, data, result);
std::string filePath = rawFilename + std::to_string(i) + "." + extension;
if (count == 1) filePath = path;
FILE* fileHandler = fopen(filePath.c_str(), "rb");
TYRA_ASSERT(fileHandler != nullptr, "Failed to load: ", filePath);
loadFile(fileHandler, filePath, i - 1, data, result);
fclose(fileHandler);
}
@@ -64,24 +64,20 @@ void McpipClip::addData(McpipBlock* block, const bool& isMulti,
for (u32 i = 0; i < blockData->count / 3; i++) {
for (u8 j = 0; j < 3; j++) {
EEClipVertex vert = {mvp * blockData->vertices[i * 3 + j], Vec4(),
blockData->textureCoords[i * 3 + j], Vec4()};
inputTriangle.push_back(vert);
inputVerts[j] = mvp * blockData->vertices[i * 3 + j];
inputTriangle[j] = {&inputVerts[j], nullptr,
&blockData->textureCoords[i * 3 + j], nullptr};
}
clippedTriangle.clear();
u8 clippedSize =
algorithm.clip(clippedTriangle, inputTriangle, algoSettings);
algorithm.clip(&clippedTriangle, inputTriangle, algoSettings);
if (clippedSize == 0) continue;
inputTriangle.clear();
if (clippedTriangle.size() == 0) continue;
auto va = clippedTriangle.at(0);
for (u32 j = 1; j <= clippedTriangle.size() - 2; j++) {
auto vb = clippedTriangle.at(j);
auto vc = clippedTriangle.at((j + 1) % clippedTriangle.size());
auto va = clippedTriangle[0];
for (u8 j = 1; j <= clippedSize - 2; j++) {
auto vb = clippedTriangle[j];
auto vc = clippedTriangle[(j + 1) % clippedSize];
clippedVertices.push_back(va);
clippedVertices.push_back(vb);
clippedVertices.push_back(vc);
@@ -52,6 +52,11 @@ StaPipBagPackagesBBox::StaPipBagPackagesBBox(Vec4* t_vertices,
mainBBox = new RenderBBox(*bboxParts, 0, partsCount);
}
StaPipBagPackagesBBox::~StaPipBagPackagesBBox() {
delete bboxParts;
delete mainBBox;
}
const RenderBBox& StaPipBagPackagesBBox::getChildBBox1By3(
const u32& index) const {
TYRA_ASSERT(index < partsCount,
@@ -106,9 +111,4 @@ std::string StaPipBagPackagesBBox::getPrint(const char* name) const {
return res.str();
}
StaPipBagPackagesBBox::~StaPipBagPackagesBBox() {
delete bboxParts;
delete mainBBox;
}
} // namespace Tyra
@@ -34,26 +34,24 @@ void StaPipClipper::clip(StaPipQBuffer* buffer) {
std::vector<EEClipVertex> clippedVertices;
for (u32 i = 0; i < buffer->size / 3; i++) {
std::vector<EEClipVertex> inputTriangle;
for (u8 j = 0; j < 3; j++) {
EEClipVertex vert = {
*mvp * buffer->vertices[i * 3 + j],
buffer->bag->lighting ? buffer->normals[i * 3 + j] : Vec4(),
buffer->bag->texture ? buffer->sts[i * 3 + j] : Vec4(),
buffer->bag->color->many ? buffer->colors[i * 3 + j] : Vec4()};
inputTriangle.push_back(vert);
inputVerts[j] = *mvp * buffer->vertices[i * 3 + j];
inputTriangle[j] = {
&inputVerts[j],
buffer->bag->lighting ? &buffer->normals[i * 3 + j] : nullptr,
buffer->bag->texture ? &buffer->sts[i * 3 + j] : nullptr,
buffer->bag->color->many ? &buffer->colors[i * 3 + j] : nullptr};
}
std::vector<EEClipVertex> clippedTriangle;
algorithm.clip(&clippedTriangle, inputTriangle, algoSettings);
u8 clippedSize =
algorithm.clip(clippedTriangle, inputTriangle, algoSettings);
if (clippedTriangle.size() == 0) continue;
if (clippedSize == 0) continue;
auto va = clippedTriangle.at(0);
for (u32 j = 1; j <= clippedTriangle.size() - 2; j++) {
auto vb = clippedTriangle.at(j);
auto vc = clippedTriangle.at((j + 1) % clippedTriangle.size());
auto va = clippedTriangle[0];
for (u8 j = 1; j <= clippedSize - 2; j++) {
auto vb = clippedTriangle[j];
auto vc = clippedTriangle[(j + 1) % clippedSize];
clippedVertices.push_back(va);
clippedVertices.push_back(vb);
clippedVertices.push_back(vc);
@@ -91,7 +91,12 @@ void StaPipCore::render(StaPipBag* bag, const bool& frustumCull,
frustumCheck = renderBbox->getMainBBox()->clipIsInFrustum(
rendererCore->renderer3D.frustumPlanes.getAll(), *bag->info->model);
if (frustumCheck == OUTSIDE_FRUSTUM) return;
if (frustumCheck == OUTSIDE_FRUSTUM) {
if (frustumCull && !bbox) {
delete renderBbox;
}
return;
}
}
packager.setRenderBBox(renderBbox);
@@ -157,7 +162,9 @@ void StaPipCore::render(StaPipBag* bag, const bool& frustumCull,
}
}
if (frustumCull && !bbox) delete renderBbox;
if (frustumCull && !bbox) {
delete renderBbox;
}
if (texBuffers) delete texBuffers;
qbufferRenderer.flushBuffers();
@@ -12,34 +12,52 @@
namespace Tyra {
EEClipAlgorithm::EEClipAlgorithm() {}
EEClipAlgorithm::EEClipAlgorithm() { tempVertices = new EEClipVertex[9]; }
EEClipAlgorithm::~EEClipAlgorithm() {}
EEClipAlgorithm::~EEClipAlgorithm() { delete[] tempVertices; }
float EEClipAlgorithm::clipMargin = -10.0F;
void EEClipAlgorithm::init(const RendererSettings& settings) {
halfWidth = settings.getWidth() / 2;
halfHeight = settings.getHeight() / 2;
// halfWidth = settings.getWidth() / 2;
// halfHeight = settings.getHeight() / 2;
halfWidth = 0.5F;
halfHeight = 0.5F;
near = settings.getNear() - (-clipMargin);
far = -settings.getFar();
}
void EEClipAlgorithm::clip(std::vector<EEClipVertex>* o_vertices,
const std::vector<EEClipVertex>& vertices,
const EEClipAlgorithmSettings& settings) {
tempVertices.clear();
for (u32 i = 0; i < vertices.size(); i++) {
o_vertices->push_back(vertices.at(i));
u8 EEClipAlgorithm::clip(EEClipVertex* o_vertices, EEClipVertexPtrs* i_vertices,
const EEClipAlgorithmSettings& settings) {
for (int i = 0; i < 3; i++) {
o_vertices[i].position = *i_vertices[i].position;
if (settings.lerpColors) o_vertices[i].color = *i_vertices[i].color;
if (settings.lerpNormals) o_vertices[i].normal = *i_vertices[i].normal;
if (settings.lerpTexCoords) o_vertices[i].st = *i_vertices[i].st;
}
clipAgainstPlane(*o_vertices, &tempVertices, 1, halfWidth, settings);
clipAgainstPlane(tempVertices, o_vertices, 1, -halfWidth, settings);
clipAgainstPlane(*o_vertices, &tempVertices, 2, halfHeight, settings);
clipAgainstPlane(tempVertices, o_vertices, 2, -halfHeight, settings);
clipAgainstPlane(*o_vertices, &tempVertices, 3, near, settings);
clipAgainstPlane(tempVertices, o_vertices, 4, far, settings);
u8 tempVerticesSize = 0;
u8 outputSize = 0;
tempVerticesSize =
clipAgainstPlane(o_vertices, 3, tempVertices, 1, halfWidth, settings);
outputSize = clipAgainstPlane(tempVertices, tempVerticesSize, o_vertices, 1,
-halfWidth, settings);
tempVerticesSize = clipAgainstPlane(o_vertices, outputSize, tempVertices, 2,
halfHeight, settings);
outputSize = clipAgainstPlane(tempVertices, tempVerticesSize, o_vertices, 2,
-halfHeight, settings);
tempVerticesSize =
clipAgainstPlane(o_vertices, outputSize, tempVertices, 3, near, settings);
outputSize = clipAgainstPlane(tempVertices, tempVerticesSize, o_vertices, 4,
far, settings);
return outputSize;
}
float EEClipAlgorithm::getValueByPlane(const EEClipVertex& v,
@@ -70,22 +88,23 @@ bool EEClipAlgorithm::isInside(const int& plane, const float& v, const float& w,
}
}
void EEClipAlgorithm::clipAgainstPlane(
const std::vector<EEClipVertex>& original,
std::vector<EEClipVertex>* clipped, const int& plane,
const float& planeLimitValue, const EEClipAlgorithmSettings& settings) {
clipped->clear();
u8 EEClipAlgorithm::clipAgainstPlane(EEClipVertex* original,
const u8& originalSize,
EEClipVertex* clipped, const int& plane,
const float& planeLimitValue,
const EEClipAlgorithmSettings& settings) {
int clippedSize = 0;
for (u32 i = 0; i < original.size(); i++) {
auto a = original.at(i);
auto b = original.at((i + 1) % original.size());
for (u32 i = 0; i < originalSize; i++) {
auto a = original[i];
auto b = original[(i + 1) % originalSize];
auto apx = getValueByPlane(a, plane);
auto bpx = getValueByPlane(b, plane);
auto aIsInside = isInside(plane, apx, a.position.w, planeLimitValue);
auto bIsInside = isInside(plane, bpx, b.position.w, planeLimitValue);
if (aIsInside) {
clipped->push_back(a);
clipped[clippedSize++] = a;
}
if (aIsInside != bIsInside) {
@@ -96,17 +115,22 @@ void EEClipAlgorithm::clipAgainstPlane(
((b.position.w - a.position.w) * planeLimitValue -
(bpx - apx));
EEClipVertex nb = {
Vec4::getByLerp(a.position, b.position, p),
settings.lerpNormals ? Vec4::getByLerp(a.normal, b.normal, p)
: Vec4(),
settings.lerpTexCoords ? Vec4::getByLerp(a.st, b.st, p) : Vec4(),
settings.lerpColors ? Vec4::getByLerp(a.color, b.color, p) : Vec4(),
};
auto index = clippedSize++;
clipped->push_back(nb);
clipped[index].position = Vec4::getByLerp(a.position, b.position, p);
if (settings.lerpNormals)
clipped[index].normal = Vec4::getByLerp(a.normal, b.normal, p);
if (settings.lerpTexCoords)
clipped[index].st = Vec4::getByLerp(a.st, b.st, p);
if (settings.lerpColors)
clipped[index].color = Vec4::getByLerp(a.color, b.color, p);
}
}
return clippedSize;
}
} // namespace Tyra
@@ -77,7 +77,6 @@ void RendererCoreGS::allocateBuffers() {
zBuffer.address =
graph_vram_allocate(frameBuffers[0].width, frameBuffers[0].height,
zBuffer.zsm, GRAPH_ALIGN_PAGE);
TYRA_LOG("Framebuffers, zBuffer set and allocated!");
graph_set_mode(GRAPH_MODE_INTERLACED, GRAPH_MODE_NTSC, GRAPH_MODE_FRAME,
GRAPH_ENABLE);
@@ -95,6 +94,8 @@ void RendererCoreGS::allocateBuffers() {
spaceOccupiedByZBuffer = spaceOccupiedByFrameBuffers;
spaceOccupiedByFrameBuffers *= 2;
TYRA_LOG("Framebuffers, zBuffer set and allocated!");
}
void RendererCoreGS::initDrawingEnvironment() {