Skip to content

Commit

Permalink
Merge pull request #181 from h4570/new-rel
Browse files Browse the repository at this point in the history
new-rel -> master
  • Loading branch information
h4570 authored Feb 11, 2024
2 parents 0c0fcd6 + 50a9070 commit 4f21626
Show file tree
Hide file tree
Showing 17 changed files with 385 additions and 26 deletions.
6 changes: 4 additions & 2 deletions engine/inc/renderer/core/texture/models/texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class Texture {

/** Set texture wrapping */
void setWrapSettings(const TextureWrap t_horizontal,
const TextureWrap t_vertical);
const TextureWrap t_vertical, int minu = 0, int minv = 0,
int maxu = 0, int maxv = 0);

// ----
// Other
Expand Down Expand Up @@ -96,9 +97,10 @@ class Texture {
void print(const std::string& name) const { print(name.c_str()); }
std::string getPrint(const char* name = nullptr) const;

void setDefaultWrapSettings();

private:
void setPsm();
void setDefaultWrapSettings();

texwrap_t wrap;
};
Expand Down
6 changes: 6 additions & 0 deletions engine/inc/renderer/core/texture/renderer_core_texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class RendererCoreTexture {

RendererCoreTextureBuffers useTexture(const Texture* t_tex);

/**
* Called by user after changing texture wrap settings
* Updates texture packet without reallocate it
*/
RendererCoreTextureBuffers updateTextureInfo(const Texture* t_tex);

/** Called by renderer during initialization */
void init(RendererCoreGS* gs, Path3* path3);

Expand Down
11 changes: 11 additions & 0 deletions engine/inc/renderer/core/texture/texture_repository.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "./models/texture.hpp"
#include "renderer/3d/mesh/mesh.hpp"
#include "renderer/core/2d/sprite/sprite.hpp"
#include "./renderer_core_texture_buffers.hpp"
#include "loaders/texture/base/texture_loader_selector.hpp"
#include <string>

Expand All @@ -25,6 +26,8 @@ class TextureRepository {
TextureRepository();
~TextureRepository();

void init(std::vector<RendererCoreTextureBuffers>* textureBuffers);

/** Returns all repository textures. */
std::vector<Texture*>* getAll() { return &textures; }

Expand Down Expand Up @@ -99,6 +102,13 @@ class TextureRepository {
void freeByMesh(const Mesh& mesh);
void freeByMesh(const Mesh* mesh);

/**
* remove texture buffer id if exist.
* Texture buffer is NOT destructed.
* easy way to create another texture buffer.
*/
int removeBufferId(const u32& t_texId);

/**
* Remove texture from repository.
* Texture is NOT destructed.
Expand All @@ -110,6 +120,7 @@ class TextureRepository {
void removeByIndex(const u32& t_index);

std::vector<Texture*> textures;
std::vector<RendererCoreTextureBuffers>* textureBuffers;
TextureLoaderSelector texLoaderSelector;
};
} // namespace Tyra
8 changes: 0 additions & 8 deletions engine/src/loaders/texture/png_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,6 @@ void PngLoader::handle8bppPalletized(TextureBuilderData* result,
auto* pixel = static_cast<unsigned char*>(result->data);
struct PngClut* clut = (struct PngClut*)result->clut;

for (int i = numPallete; i < 256; i++) {
memset(&clut[i], 0, sizeof(clut[i]));
}

for (int i = 0; i < numPallete; i++) {
clut[i].r = palette[i].red;
clut[i].g = palette[i].green;
Expand Down Expand Up @@ -273,10 +269,6 @@ void PngLoader::handle4bppPalletized(TextureBuilderData* result,
auto* pixel = static_cast<unsigned char*>(result->data);
struct PngClut* clut = (struct PngClut*)result->clut;

for (int i = numPallete; i < 16; i++) {
memset(&clut[i], 0, sizeof(clut[i]));
}

for (int i = 0; i < numPallete; i++) {
clut[i].r = palette[i].red;
clut[i].g = palette[i].green;
Expand Down
22 changes: 11 additions & 11 deletions engine/src/renderer/core/paths/path3/path3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,20 @@ void Path3::sendTexture(const Texture* texture,
const RendererCoreTextureBuffers& texBuffers) {
packet2_reset(texturePacket, false);

int coreWidth = texBuffers.core->width <= 64 ? 64 : texBuffers.core->width;
packet2_update(texturePacket,
draw_texture_transfer(texturePacket->base, texture->core->data,
texture->getWidth(),
texture->getHeight(), texture->core->psm,
texBuffers.core->address, coreWidth));
packet2_update(
texturePacket,
draw_texture_transfer(texturePacket->base, texture->core->data,
texture->getWidth(), texture->getHeight(),
texture->core->psm, texBuffers.core->address,
texBuffers.core->width));

if (texBuffers.clut != nullptr) {
auto* clut = texture->clut;
int clutWidth = texBuffers.clut->width <= 64 ? 64 : texBuffers.clut->width;
packet2_update(texturePacket,
draw_texture_transfer(texturePacket->next, clut->data,
clut->width, clut->height, clut->psm,
texBuffers.clut->address, clutWidth));
packet2_update(
texturePacket,
draw_texture_transfer(texturePacket->next, clut->data, clut->width,
clut->height, clut->psm, texBuffers.clut->address,
texBuffers.clut->width));
}

packet2_chain_open_cnt(texturePacket, 0, 0, 0);
Expand Down
20 changes: 18 additions & 2 deletions engine/src/renderer/core/texture/models/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,18 @@

namespace Tyra {

namespace TyraTexture {
u32 textureCounter = 1;
std::vector<u32> deletedIDs;
} // namespace TyraTexture

Texture::Texture(TextureBuilderData* t_data) {
id = rand() % 1000000;
if (TyraTexture::deletedIDs.empty() == false) {
id = TyraTexture::deletedIDs.front();
TyraTexture::deletedIDs.erase(TyraTexture::deletedIDs.begin());
} else {
id = TyraTexture::textureCounter++;
}

name = t_data->name;

Expand Down Expand Up @@ -50,6 +60,7 @@ Texture::Texture(TextureBuilderData* t_data) {
}

Texture::~Texture() {
TyraTexture::deletedIDs.push_back(id);
if (links.size() > 0) links.clear();
if (core) delete core;
if (clut) delete clut;
Expand Down Expand Up @@ -167,9 +178,14 @@ void Texture::setDefaultWrapSettings() {
}

void Texture::setWrapSettings(const TextureWrap t_horizontal,
const TextureWrap t_vertical) {
const TextureWrap t_vertical, int minu, int minv,
int maxu, int maxv) {
wrap.horizontal = t_horizontal;
wrap.vertical = t_vertical;
wrap.minu = minu;
wrap.minv = minv;
wrap.maxu = maxu;
wrap.maxv = maxv;
}

void Texture::addLink(const u32& t_id) {
Expand Down
12 changes: 12 additions & 0 deletions engine/src/renderer/core/texture/renderer_core_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ RendererCoreTexture::~RendererCoreTexture() {}
void RendererCoreTexture::init(RendererCoreGS* t_gs, Path3* t_path3) {
gs = t_gs;
sender.init(t_path3, t_gs);
repository.init(&currentAllocations);
path3 = t_path3;
initClut();
}
Expand Down Expand Up @@ -56,6 +57,17 @@ RendererCoreTextureBuffers RendererCoreTexture::useTexture(
return newTexBuffer;
}

RendererCoreTextureBuffers RendererCoreTexture::updateTextureInfo(
const Texture* t_tex) {
TYRA_ASSERT(t_tex != nullptr, "Provided nullptr texture!");

auto allocated = getAllocatedBuffersByTextureId(t_tex->id);
TYRA_ASSERT(allocated.id != 0, "Can't update an unallocated texture!");

path3->sendTexture(t_tex, allocated);
return allocated;
}

RendererCoreTextureBuffers RendererCoreTexture::getAllocatedBuffersByTextureId(
const u32& t_id) {
for (u32 i = 0; i < currentAllocations.size(); i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ texbuffer_t* RendererCoreTextureSender::allocateTextureCore(
auto* result = new texbuffer_t;
const auto* core = t_texture->core;

result->width = t_texture->getWidth();
int coreWidth = core->width <= 64 ? 64 : core->width;

result->width = coreWidth;
result->psm = core->psm;
result->info.components = core->components;

Expand All @@ -77,7 +79,9 @@ texbuffer_t* RendererCoreTextureSender::allocateTextureClut(
auto* result = new texbuffer_t;
const auto* clut = t_texture->clut;

result->width = clut->width;
int clutWidth = clut->width <= 64 ? 64 : clut->width;

result->width = clutWidth;
result->psm = clut->psm;
result->info.components = clut->components;

Expand Down
18 changes: 17 additions & 1 deletion engine/src/renderer/core/texture/texture_repository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ TextureRepository::~TextureRepository() {
}
}

void TextureRepository::init(
std::vector<RendererCoreTextureBuffers>* t_textureBuffers) {
textureBuffers = t_textureBuffers;
}

Texture* TextureRepository::getBySpriteId(const u32& t_id) const {
for (u32 i = 0; i < textures.size(); i++) {
if (textures[i]->isLinkedWith(t_id)) return textures[i];
Expand Down Expand Up @@ -57,10 +62,20 @@ void TextureRepository::removeByIndex(const u32& t_index) {
textures.erase(textures.begin() + t_index);
}

int TextureRepository::removeBufferId(const u32& t_texId) {
for (u32 i = 0; i < textureBuffers->size(); i++)
if ((*textureBuffers)[i].id == t_texId) {
(*textureBuffers)[i].id = -1;
return 0;
}
return -1;
}

void TextureRepository::removeById(const u32& t_texId) {
s32 index = getIndexOf(t_texId);
TYRA_ASSERT(index != -1, "Cant remove texture, because it was not found!");
removeByIndex(index);
removeBufferId(t_texId);
}

void TextureRepository::freeByMesh(const Mesh& mesh) {
Expand Down Expand Up @@ -93,6 +108,7 @@ void TextureRepository::free(const u32& t_texId) {

TYRA_ASSERT(index != -1, "Cant remove texture, because it was not found!");
removeByIndex(index);
removeBufferId(t_texId);

delete tex;
}
Expand All @@ -116,7 +132,7 @@ void TextureRepository::addByMesh(const Mesh* mesh, const char* directory,
if (dirFixed.back() != '/' && dirFixed.back() != ':') dirFixed += "/";

for (u32 i = 0; i < mesh->materials.size(); i++) {
if (!mesh->materials[i]->textureName.has_value()) {
if (!mesh->materials[i]->textureName.has_value()) {
continue;
}

Expand Down
24 changes: 24 additions & 0 deletions tutorials/11-texture-region-repeat/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
TARGET := tutorial_11.elf
ENGINEDIR := ../../engine

#The Directories, Source, Includes, Objects, Binary and Resources
SRCDIR := src
INCDIR := inc
BUILDDIR := obj
TARGETDIR := bin
RESDIR := res
SRCEXT := cpp
VSMEXT := vsm
VCLEXT := vcl
VCLPPEXT := vclpp
DEPEXT := d
OBJEXT := o

#Flags, Libraries and Includes
CFLAGS :=
LIB :=
LIBDIRS :=
INC := -I$(INCDIR)
INCDEP := -I$(INCDIR)

include ../Makefile.tutorials-base
4 changes: 4 additions & 0 deletions tutorials/11-texture-region-repeat/bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
58 changes: 58 additions & 0 deletions tutorials/11-texture-region-repeat/inc/tutorial_11.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
# _____ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2024, tyra - https://github.com/h4570/tyra
# Licensed under Apache License 2.0
# Wellington Carvalho <[email protected]>
*/

#pragma once

#include <tyra>
#include <memory>

namespace Tyra {

class Tutorial11 : public Game {
public:
explicit Tutorial11(Engine* engine);
~Tutorial11();

void init();
void loop();

private:
void setDrawData();
void loadBags();

void setTextureRegionRepeat();
void setTextureDefaultWrap();

const u8 MAX_TILES = 4;
u8 TILE_COL = 0;
u8 TILE_ROW = 0;

u8 shouldFlipTextureSettings = false;
float limitTimeToFlip = 2.00f;
float elapsedTime = 0;

Engine* engine;

Vec4 cameraPosition, cameraLookAt;
M4x4 translation, rotation, scale, model;
Texture* UVCheckerTex;

std::array<Vec4, 6> vertices;
std::array<Color, 6> colors;
std::array<Vec4, 6> uvMap;

StaticPipeline stapip;
std::unique_ptr<StaPipBag> bag;
std::unique_ptr<StaPipInfoBag> infoBag;
std::unique_ptr<StaPipColorBag> colorBag;
std::unique_ptr<StaPipTextureBag> texBag;
};

} // namespace Tyra
4 changes: 4 additions & 0 deletions tutorials/11-texture-region-repeat/obj/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
4 changes: 4 additions & 0 deletions tutorials/11-texture-region-repeat/res/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
4 changes: 4 additions & 0 deletions tutorials/11-texture-region-repeat/run.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$ConfigFile = Join-Path $PSScriptRoot '../../windows-pcsx2.ps1'
. $ConfigFile

RunPCSX2
Loading

0 comments on commit 4f21626

Please sign in to comment.