Merge branch 'revert-palette-manager' into 'master'

Revert "hwr2: Track colormaps and lighttables tex globally"

See merge request KartKrew/Kart!1249
This commit is contained in:
Eidolon 2023-05-23 14:46:36 +00:00
commit a8e122c36b
3 changed files with 66 additions and 164 deletions

View file

@ -12,7 +12,6 @@
#include <algorithm>
#include <cmath>
#include "../r_state.h"
#include "../v_video.h"
#include "../z_zone.h"
@ -140,148 +139,28 @@ void FramebufferManager::postpass(Rhi& rhi)
{
}
MainPaletteManager::MainPaletteManager() = default;
MainPaletteManager::~MainPaletteManager() = default;
MainPaletteManager::MainPaletteManager() : Pass()
{
}
constexpr std::size_t kPaletteSize = 256;
constexpr std::size_t kLighttableRows = LIGHTLEVELS;
MainPaletteManager::~MainPaletteManager() = default;
void MainPaletteManager::prepass(Rhi& rhi)
{
if (!palette_)
{
palette_ = rhi.create_texture({TextureFormat::kRGBA, kPaletteSize, 1, TextureWrapMode::kClamp, TextureWrapMode::kClamp});
palette_ = rhi.create_texture({TextureFormat::kRGBA, 256, 1, TextureWrapMode::kClamp, TextureWrapMode::kClamp});
}
if (!lighttable_)
{
lighttable_ = rhi.create_texture({TextureFormat::kLuminance, kPaletteSize, kLighttableRows, TextureWrapMode::kClamp, TextureWrapMode::kClamp});
}
if (!encore_lighttable_)
{
encore_lighttable_ = rhi.create_texture({TextureFormat::kLuminance, kPaletteSize, kLighttableRows, TextureWrapMode::kClamp, TextureWrapMode::kClamp});
}
if (!default_colormap_)
{
default_colormap_ = rhi.create_texture({TextureFormat::kLuminance, kPaletteSize, 1, TextureWrapMode::kClamp, TextureWrapMode::kClamp});
}
}
void MainPaletteManager::upload_palette(Rhi& rhi, Handle<TransferContext> ctx)
{
std::array<byteColor_t, kPaletteSize> palette_32;
for (std::size_t i = 0; i < kPaletteSize; i++)
{
palette_32[i] = V_GetColor(i).s;
}
rhi.update_texture(ctx, palette_, {0, 0, kPaletteSize, 1}, PixelFormat::kRGBA8, tcb::as_bytes(tcb::span(palette_32)));
}
void MainPaletteManager::upload_lighttables(Rhi& rhi, Handle<TransferContext> ctx)
{
tcb::span<const std::byte> colormap_bytes = tcb::as_bytes(tcb::span(colormaps, kPaletteSize * kLighttableRows));
rhi.update_texture(ctx, lighttable_, {0, 0, kPaletteSize, kLighttableRows}, PixelFormat::kR8, colormap_bytes);
if (encoremap != nullptr)
{
tcb::span<const std::byte> encoremap_bytes = tcb::as_bytes(tcb::span(encoremap, kPaletteSize * kLighttableRows));
rhi.update_texture(ctx, lighttable_, {0, 0, kPaletteSize, kLighttableRows}, PixelFormat::kR8, encoremap_bytes);
}
if (!lighttables_to_upload_.empty())
{
for (auto lighttable : lighttables_to_upload_)
{
Handle<Texture> lighttable_tex = find_extra_lighttable(lighttable);
SRB2_ASSERT(lighttable_tex != kNullHandle);
tcb::span<const std::byte> lighttable_bytes = tcb::as_bytes(tcb::span(lighttable, kPaletteSize * kLighttableRows));
rhi.update_texture(ctx, lighttable_tex, {0, 0, kPaletteSize, kLighttableRows}, PixelFormat::kR8, lighttable_bytes);
}
lighttables_to_upload_.clear();
}
}
void MainPaletteManager::upload_default_colormap(Rhi& rhi, Handle<TransferContext> ctx)
{
std::array<uint8_t, kPaletteSize> data;
for (std::size_t i = 0; i < kPaletteSize; i++)
{
data[i] = i;
}
rhi.update_texture(ctx, default_colormap_, {0, 0, kPaletteSize, 1}, PixelFormat::kR8, tcb::as_bytes(tcb::span(data)));
}
void MainPaletteManager::upload_colormaps(Rhi& rhi, Handle<TransferContext> ctx)
{
for (auto to_upload : colormaps_to_upload_)
{
SRB2_ASSERT(to_upload != nullptr);
SRB2_ASSERT(colormaps_.find(to_upload) != colormaps_.end());
rhi::Handle<rhi::Texture> map_texture = colormaps_.at(to_upload);
tcb::span<const std::byte> map_bytes = tcb::as_bytes(tcb::span(to_upload, kPaletteSize));
rhi.update_texture(ctx, map_texture, {0, 0, kPaletteSize, 1}, PixelFormat::kR8, map_bytes);
}
colormaps_to_upload_.clear();
}
rhi::Handle<rhi::Texture> MainPaletteManager::find_or_create_colormap(rhi::Rhi& rhi, srb2::NotNull<const uint8_t*> colormap)
{
if (colormaps_.find(colormap) != colormaps_.end())
{
return colormaps_[colormap];
}
Handle<Texture> texture = rhi.create_texture({TextureFormat::kLuminance, kPaletteSize, 1, TextureWrapMode::kClamp, TextureWrapMode::kClamp});
colormaps_.insert_or_assign(colormap, texture);
colormaps_to_upload_.push_back(colormap);
return texture;
}
rhi::Handle<rhi::Texture> MainPaletteManager::find_colormap(srb2::NotNull<const uint8_t*> colormap) const
{
if (colormaps_.find(colormap) == colormaps_.end())
{
return kNullHandle;
}
return colormaps_.at(colormap);
}
rhi::Handle<rhi::Texture> MainPaletteManager::find_or_create_extra_lighttable(Rhi& rhi, srb2::NotNull<const uint8_t*> lighttable)
{
if (lighttables_.find(lighttable) != lighttables_.end())
{
return lighttables_[lighttable];
}
Handle<Texture> texture = rhi.create_texture({TextureFormat::kLuminance, kPaletteSize, kLighttableRows, TextureWrapMode::kClamp, TextureWrapMode::kClamp});
lighttables_.insert_or_assign(lighttable, texture);
lighttables_to_upload_.push_back(lighttable);
return texture;
}
rhi::Handle<rhi::Texture> MainPaletteManager::find_extra_lighttable(srb2::NotNull<const uint8_t*> lighttable) const
{
if (lighttables_.find(lighttable) == lighttables_.end())
{
return kNullHandle;
}
return lighttables_.at(lighttable);
}
void MainPaletteManager::transfer(Rhi& rhi, Handle<TransferContext> ctx)
{
upload_palette(rhi, ctx);
upload_lighttables(rhi, ctx);
upload_default_colormap(rhi, ctx);
upload_colormaps(rhi, ctx);
std::array<byteColor_t, 256> palette_32;
for (std::size_t i = 0; i < 256; i++)
{
palette_32[i] = V_GetColor(i).s;
}
rhi.update_texture(ctx, palette_, {0, 0, 256, 1}, PixelFormat::kRGBA8, tcb::as_bytes(tcb::span(palette_32)));
}
void MainPaletteManager::graphics(Rhi& rhi, Handle<GraphicsContext> ctx)
@ -290,13 +169,6 @@ void MainPaletteManager::graphics(Rhi& rhi, Handle<GraphicsContext> ctx)
void MainPaletteManager::postpass(Rhi& rhi)
{
// Delete all colormap textures so they'll be recreated next frame
// Unfortunately the 256x1 translation colormaps are sometimes freed at runtime
for (auto& cm : colormaps_)
{
rhi.destroy_texture(cm.second);
}
colormaps_.clear();
}
CommonResourcesManager::CommonResourcesManager() = default;

View file

@ -74,19 +74,6 @@ public:
class MainPaletteManager final : public Pass
{
rhi::Handle<rhi::Texture> palette_;
rhi::Handle<rhi::Texture> lighttable_;
rhi::Handle<rhi::Texture> encore_lighttable_;
rhi::Handle<rhi::Texture> default_colormap_;
std::unordered_map<const uint8_t*, rhi::Handle<rhi::Texture>> colormaps_;
std::unordered_map<const uint8_t*, rhi::Handle<rhi::Texture>> lighttables_;
std::vector<const uint8_t*> colormaps_to_upload_;
std::vector<const uint8_t*> lighttables_to_upload_;
void upload_palette(rhi::Rhi& rhi, rhi::Handle<rhi::TransferContext> ctx);
void upload_lighttables(rhi::Rhi& rhi, rhi::Handle<rhi::TransferContext> ctx);
void upload_default_colormap(rhi::Rhi& rhi, rhi::Handle<rhi::TransferContext> ctx);
void upload_colormaps(rhi::Rhi& rhi, rhi::Handle<rhi::TransferContext> ctx);
public:
MainPaletteManager();
@ -98,14 +85,6 @@ public:
virtual void postpass(rhi::Rhi& rhi) override;
rhi::Handle<rhi::Texture> palette() const noexcept { return palette_; }
rhi::Handle<rhi::Texture> lighttable() const noexcept { return lighttable_; }
rhi::Handle<rhi::Texture> encore_lighttable() const noexcept { return encore_lighttable_; }
rhi::Handle<rhi::Texture> default_colormap() const noexcept { return default_colormap_; }
rhi::Handle<rhi::Texture> find_or_create_colormap(rhi::Rhi& rhi, srb2::NotNull<const uint8_t*> colormap);
rhi::Handle<rhi::Texture> find_colormap(srb2::NotNull<const uint8_t*> colormap) const;
rhi::Handle<rhi::Texture> find_or_create_extra_lighttable(rhi::Rhi& rhi, srb2::NotNull<const uint8_t*> lighttable);
rhi::Handle<rhi::Texture> find_extra_lighttable(srb2::NotNull<const uint8_t*> lighttable) const;
};
class CommonResourcesManager final : public Pass

View file

@ -26,6 +26,9 @@ using namespace srb2::rhi;
struct srb2::hwr2::TwodeePassData
{
Handle<Texture> default_tex;
Handle<Texture> default_colormap_tex;
std::unordered_map<const uint8_t*, Handle<Texture>> colormaps;
std::vector<const uint8_t*> colormaps_to_upload;
std::unordered_map<TwodeePipelineKey, Handle<Pipeline>> pipelines;
bool upload_default_tex = false;
};
@ -283,6 +286,17 @@ void TwodeePass::prepass(Rhi& rhi)
});
data_->upload_default_tex = true;
}
if (!data_->default_colormap_tex)
{
data_->default_colormap_tex = rhi.create_texture({
TextureFormat::kLuminance,
256,
1,
TextureWrapMode::kClamp,
TextureWrapMode::kClamp
});
data_->upload_default_tex = true;
}
if (!render_pass_)
{
render_pass_ = rhi.create_render_pass(
@ -300,6 +314,7 @@ void TwodeePass::prepass(Rhi& rhi)
// Stage 1 - command list patch detection
std::unordered_set<const patch_t*> found_patches;
std::unordered_set<const uint8_t*> found_colormaps;
for (const auto& list : *ctx_)
{
for (const auto& cmd : list.cmds)
@ -313,7 +328,7 @@ void TwodeePass::prepass(Rhi& rhi)
}
if (cmd.colormap != nullptr)
{
palette_manager_->find_or_create_colormap(rhi, cmd.colormap);
found_colormaps.insert(cmd.colormap);
}
},
[&](const Draw2dVertices& cmd) {}};
@ -327,6 +342,17 @@ void TwodeePass::prepass(Rhi& rhi)
}
patch_atlas_cache_->pack(rhi);
for (auto colormap : found_colormaps)
{
if (data_->colormaps.find(colormap) == data_->colormaps.end())
{
Handle<Texture> colormap_tex = rhi.create_texture({TextureFormat::kLuminance, 256, 1});
data_->colormaps.insert({colormap, colormap_tex});
}
data_->colormaps_to_upload.push_back(colormap);
}
size_t list_index = 0;
for (auto& list : *ctx_)
{
@ -510,9 +536,34 @@ void TwodeePass::transfer(Rhi& rhi, Handle<TransferContext> ctx)
std::array<uint8_t, 4> data = {0, 255, 0, 255};
rhi.update_texture(ctx, data_->default_tex, {0, 0, 2, 1}, PixelFormat::kRG8, tcb::as_bytes(tcb::span(data)));
std::array<uint8_t, 256> colormap_data;
for (size_t i = 0; i < 256; i++)
{
colormap_data[i] = i;
}
rhi.update_texture(
ctx,
data_->default_colormap_tex,
{0, 0, 256, 1},
PixelFormat::kR8,
tcb::as_bytes(tcb::span(colormap_data))
);
data_->upload_default_tex = false;
}
for (auto colormap : data_->colormaps_to_upload)
{
rhi.update_texture(
ctx,
data_->colormaps[colormap],
{0, 0, 256, 1},
rhi::PixelFormat::kR8,
tcb::as_bytes(tcb::span(colormap, 256))
);
}
data_->colormaps_to_upload.clear();
Handle<Texture> palette_tex = palette_manager_->palette();
// Update the buffers for each list
@ -556,11 +607,11 @@ void TwodeePass::transfer(Rhi& rhi, Handle<TransferContext> ctx)
}
const uint8_t* colormap = mcmd.colormap;
Handle<Texture> colormap_h = palette_manager_->default_colormap();
Handle<Texture> colormap_h = data_->default_colormap_tex;
if (colormap)
{
colormap_h = palette_manager_->find_colormap(colormap);
SRB2_ASSERT(colormap_h != kNullHandle);
SRB2_ASSERT(data_->colormaps.find(colormap) != data_->colormaps.end());
colormap_h = data_->colormaps[colormap];
}
tx[2] = {SamplerName::kSampler2, colormap_h};
mcmd.binding_set =