From 8cc6e9a4f003439b588cc976b281d215c2d852b1 Mon Sep 17 00:00:00 2001 From: Eidolon Date: Fri, 24 Mar 2023 16:37:48 -0500 Subject: [PATCH] rhi: Add texture wrap modes They cannot be changed after creation, for simplicity. --- src/hwr2/pass_blit_rect.cpp | 2 +- src/hwr2/pass_imgui.cpp | 8 +++- src/hwr2/pass_postprocess.cpp | 8 +++- src/hwr2/pass_resource_managers.cpp | 62 ++++++++++++++++++++++++----- src/hwr2/pass_software.cpp | 8 +++- src/hwr2/pass_twodee.cpp | 24 +++++++++-- src/rhi/gl3_core/gl3_core_rhi.cpp | 19 ++++++++- src/rhi/rhi.hpp | 9 +++++ 8 files changed, 121 insertions(+), 19 deletions(-) diff --git a/src/hwr2/pass_blit_rect.cpp b/src/hwr2/pass_blit_rect.cpp index 21c7cf343..38f0cf5cf 100644 --- a/src/hwr2/pass_blit_rect.cpp +++ b/src/hwr2/pass_blit_rect.cpp @@ -165,7 +165,7 @@ void BlitRectPass::transfer(Rhi& rhi, Handle ctx) glm::mat3( glm::vec3(1.f, 0.f, 0.f), glm::vec3(0.f, output_flip_ ? -1.f : 1.f, 0.f), - glm::vec3(0.f, 0.f, 1.f) + glm::vec3(0.f, output_flip_ ? 1.f : 0.f, 1.f) ) }; diff --git a/src/hwr2/pass_imgui.cpp b/src/hwr2/pass_imgui.cpp index f710b22fc..3707a4a3d 100644 --- a/src/hwr2/pass_imgui.cpp +++ b/src/hwr2/pass_imgui.cpp @@ -64,7 +64,13 @@ void ImguiPass::prepass(Rhi& rhi) uint32_t uwidth = static_cast(width); uint32_t uheight = static_cast(height); - font_atlas_ = rhi.create_texture({TextureFormat::kRGBA, uwidth, uheight}); + font_atlas_ = rhi.create_texture({ + TextureFormat::kRGBA, + uwidth, + uheight, + TextureWrapMode::kRepeat, + TextureWrapMode::kRepeat + }); io.Fonts->SetTexID(font_atlas_); } diff --git a/src/hwr2/pass_postprocess.cpp b/src/hwr2/pass_postprocess.cpp index f3eb722a9..a35dea645 100644 --- a/src/hwr2/pass_postprocess.cpp +++ b/src/hwr2/pass_postprocess.cpp @@ -166,7 +166,13 @@ void PostprocessWipePass::prepass(Rhi& rhi) } } - wipe_tex_ = rhi.create_texture({TextureFormat::kLuminance, mask_w_, mask_h_}); + wipe_tex_ = rhi.create_texture({ + TextureFormat::kLuminance, + mask_w_, + mask_h_, + TextureWrapMode::kClamp, + TextureWrapMode::kClamp + }); } void PostprocessWipePass::transfer(Rhi& rhi, Handle ctx) diff --git a/src/hwr2/pass_resource_managers.cpp b/src/hwr2/pass_resource_managers.cpp index b94c9c9af..58ea993eb 100644 --- a/src/hwr2/pass_resource_managers.cpp +++ b/src/hwr2/pass_resource_managers.cpp @@ -71,7 +71,13 @@ void FramebufferManager::prepass(Rhi& rhi) // Recreate the framebuffer textures if (main_color_ == kNullHandle) { - main_color_ = rhi.create_texture({TextureFormat::kRGBA, current_width, current_height}); + main_color_ = rhi.create_texture({ + TextureFormat::kRGBA, + current_width, + current_height, + TextureWrapMode::kClamp, + TextureWrapMode::kClamp + }); } if (main_depth_ == kNullHandle) { @@ -80,20 +86,44 @@ void FramebufferManager::prepass(Rhi& rhi) if (post_colors_[0] == kNullHandle) { - post_colors_[0] = rhi.create_texture({TextureFormat::kRGBA, current_width, current_height}); + post_colors_[0] = rhi.create_texture({ + TextureFormat::kRGBA, + current_width, + current_height, + TextureWrapMode::kClamp, + TextureWrapMode::kClamp + }); } if (post_colors_[1] == kNullHandle) { - post_colors_[1] = rhi.create_texture({TextureFormat::kRGBA, current_width, current_height}); + post_colors_[1] = rhi.create_texture({ + TextureFormat::kRGBA, + current_width, + current_height, + TextureWrapMode::kClamp, + TextureWrapMode::kClamp + }); } if (wipe_start_color_ == kNullHandle) { - wipe_start_color_ = rhi.create_texture({TextureFormat::kRGBA, current_width, current_height}); + wipe_start_color_ = rhi.create_texture({ + TextureFormat::kRGBA, + current_width, + current_height, + TextureWrapMode::kClamp, + TextureWrapMode::kClamp + }); } if (wipe_end_color_ == kNullHandle) { - wipe_end_color_ = rhi.create_texture({TextureFormat::kRGBA, current_width, current_height}); + wipe_end_color_ = rhi.create_texture({ + TextureFormat::kRGBA, + current_width, + current_height, + TextureWrapMode::kClamp, + TextureWrapMode::kClamp + }); } } @@ -119,7 +149,7 @@ void MainPaletteManager::prepass(Rhi& rhi) { if (!palette_) { - palette_ = rhi.create_texture({TextureFormat::kRGBA, 256, 1}); + palette_ = rhi.create_texture({TextureFormat::kRGBA, 256, 1, TextureWrapMode::kClamp, TextureWrapMode::kClamp}); } } @@ -148,9 +178,15 @@ void CommonResourcesManager::prepass(Rhi& rhi) { if (!init_) { - black_ = rhi.create_texture({TextureFormat::kRGBA, 1, 1}); - white_ = rhi.create_texture({TextureFormat::kRGBA, 1, 1}); - transparent_ = rhi.create_texture({TextureFormat::kRGBA, 1, 1}); + black_ = rhi.create_texture({TextureFormat::kRGBA, 1, 1, TextureWrapMode::kClamp, TextureWrapMode::kClamp}); + white_ = rhi.create_texture({TextureFormat::kRGBA, 1, 1, TextureWrapMode::kClamp, TextureWrapMode::kClamp}); + transparent_ = rhi.create_texture({ + TextureFormat::kRGBA, + 1, + 1, + TextureWrapMode::kClamp, + TextureWrapMode::kClamp + }); } } @@ -266,7 +302,13 @@ Handle FlatTextureManager::find_or_create_indexed(Rhi& rhi, lumpnum_t l } uint32_t flat_size = get_flat_size(lump); - Handle new_tex = rhi.create_texture({TextureFormat::kLuminanceAlpha, flat_size, flat_size}); + Handle new_tex = rhi.create_texture({ + TextureFormat::kLuminanceAlpha, + flat_size, + flat_size, + TextureWrapMode::kRepeat, + TextureWrapMode::kRepeat + }); flats_.insert({lump, new_tex}); to_upload_.push_back(lump); return new_tex; diff --git a/src/hwr2/pass_software.cpp b/src/hwr2/pass_software.cpp index bee9a2234..0a514cd9b 100644 --- a/src/hwr2/pass_software.cpp +++ b/src/hwr2/pass_software.cpp @@ -46,7 +46,13 @@ void SoftwarePass::prepass(Rhi& rhi) if (!screen_texture_) { - screen_texture_ = rhi.create_texture({TextureFormat::kLuminance, width_, height_}); + screen_texture_ = rhi.create_texture({ + TextureFormat::kLuminance, + width_, + height_, + TextureWrapMode::kClamp, + TextureWrapMode::kClamp + }); } // If the screen width won't fit the unpack alignment, we need to copy the screen. diff --git a/src/hwr2/pass_twodee.cpp b/src/hwr2/pass_twodee.cpp index 955831825..8b0f22b71 100644 --- a/src/hwr2/pass_twodee.cpp +++ b/src/hwr2/pass_twodee.cpp @@ -88,7 +88,13 @@ static Rect trimmed_patch_dim(const patch_t* patch); static void create_atlas(Rhi& rhi, TwodeePassData& pass_data) { Atlas new_atlas; - new_atlas.tex = rhi.create_texture({TextureFormat::kLuminanceAlpha, 2048, 2048}); + new_atlas.tex = rhi.create_texture({ + TextureFormat::kLuminanceAlpha, + 2048, + 2048, + TextureWrapMode::kClamp, + TextureWrapMode::kClamp + }); new_atlas.tex_width = 2048; new_atlas.tex_height = 2048; new_atlas.rp_ctx = std::make_unique(); @@ -501,12 +507,24 @@ void TwodeePass::prepass(Rhi& rhi) if (!data_->default_tex) { - data_->default_tex = rhi.create_texture({TextureFormat::kLuminanceAlpha, 2, 1}); + data_->default_tex = rhi.create_texture({ + TextureFormat::kLuminanceAlpha, + 2, + 1, + TextureWrapMode::kClamp, + TextureWrapMode::kClamp + }); data_->upload_default_tex = true; } if (!data_->default_colormap_tex) { - data_->default_colormap_tex = rhi.create_texture({TextureFormat::kLuminance, 256, 1}); + data_->default_colormap_tex = rhi.create_texture({ + TextureFormat::kLuminance, + 256, + 1, + TextureWrapMode::kClamp, + TextureWrapMode::kClamp + }); data_->upload_default_tex = true; } if (!render_pass_) diff --git a/src/rhi/gl3_core/gl3_core_rhi.cpp b/src/rhi/gl3_core/gl3_core_rhi.cpp index 54ce211bf..5e82010ac 100644 --- a/src/rhi/gl3_core/gl3_core_rhi.cpp +++ b/src/rhi/gl3_core/gl3_core_rhi.cpp @@ -110,6 +110,21 @@ constexpr GLenum map_texture_format(rhi::TextureFormat format) } } +constexpr GLenum map_texture_wrap(rhi::TextureWrapMode wrap) +{ + switch (wrap) + { + case rhi::TextureWrapMode::kClamp: + return GL_CLAMP_TO_EDGE; + case rhi::TextureWrapMode::kRepeat: + return GL_REPEAT; + case rhi::TextureWrapMode::kMirroredRepeat: + return GL_MIRRORED_REPEAT; + default: + return GL_NEAREST; + } +} + constexpr GLenum map_internal_texture_format(rhi::TextureFormat format) { switch (format) @@ -527,9 +542,9 @@ rhi::Handle GlCoreRhi::create_texture(const rhi::TextureDesc& desc GL_ASSERT; gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); GL_ASSERT; - gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, map_texture_wrap(desc.u_wrap)); GL_ASSERT; - gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, map_texture_wrap(desc.v_wrap)); GL_ASSERT; gl_->TexImage2D(GL_TEXTURE_2D, 0, internal_format, desc.width, desc.height, 0, format, GL_UNSIGNED_BYTE, nullptr); GL_ASSERT; diff --git a/src/rhi/rhi.hpp b/src/rhi/rhi.hpp index c1d477b00..fd4669742 100644 --- a/src/rhi/rhi.hpp +++ b/src/rhi/rhi.hpp @@ -432,11 +432,20 @@ struct RenderbufferDesc uint32_t height; }; +enum class TextureWrapMode +{ + kRepeat, + kMirroredRepeat, + kClamp +}; + struct TextureDesc { TextureFormat format; uint32_t width; uint32_t height; + TextureWrapMode u_wrap; + TextureWrapMode v_wrap; }; struct BufferDesc