Skip to content

Commit e8a79f1

Browse files
committed
limit scale
- Limit scale so we don't exceed min or max texture dims, or stretch image.
1 parent fc9e2d2 commit e8a79f1

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

w-image-viewer/renderer.cpp

+19-16
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ namespace {
4242
DXGI_FORMAT_R16G16B16A16_FLOAT,
4343
DXGI_FORMAT_R32G32B32A32_FLOAT
4444
};
45+
46+
// Max texture size will be determined by D3D_FEATURE_LEVEL_, but D3D11 and D3D12 _REQ_TEXTURE2D_U_OR_V_DIMENSION should be the same.
47+
template<typename T>
48+
constexpr T MAX_TEX_UV{ D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION };
4549
}
4650

4751
void Renderer::create(HWND hwnd)
@@ -127,11 +131,8 @@ void Renderer::create_image()
127131

128132
// Create texture.
129133
const D3D11_TEXTURE2D_DESC texture2d_desc{
130-
131-
// Max texture size will be determined by D3D_FEATURE_LEVEL_, but D3D11 and D3D12 _REQ_TEXTURE2D_U_OR_V_DIMENSION should be the same.
132-
.Width{ std::clamp(image.get_width<UINT>(), 1u, static_cast<UINT>(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION)) },
133-
.Height{ std::clamp(image.get_height<UINT>(), 1u, static_cast<UINT>(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION)) },
134-
134+
.Width{ image.get_width<UINT>() },
135+
.Height{ image.get_height<UINT>() },
135136
.MipLevels{ 1 },
136137
.ArraySize{ 1 },
137138
.Format{ format },
@@ -721,11 +722,8 @@ void Renderer::draw_pass(UINT width, UINT height) noexcept
721722
{
722723
// Create texture.
723724
const D3D11_TEXTURE2D_DESC texture2d_desc{
724-
725-
// Max texture size will be determined by D3D_FEATURE_LEVEL_, but D3D11 and D3D12 _REQ_TEXTURE2D_U_OR_V_DIMENSION should be the same.
726-
.Width{ std::clamp(width, 1u, static_cast<UINT>(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION)) },
727-
.Height{ std::clamp(height, 1u, static_cast<UINT>(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION)) },
728-
725+
.Width{ width },
726+
.Height{ height },
729727
.MipLevels{ 1 },
730728
.ArraySize{ 1 },
731729
.Format{ WIV_PASS_FORMATS[g_config.pass_format.val] },
@@ -781,12 +779,8 @@ void Renderer::create_pixel_shader(const BYTE* shader, size_t shader_size) const
781779
void Renderer::create_viewport(float width, float height, bool adjust) const noexcept
782780
{
783781
D3D11_VIEWPORT viewport{
784-
785-
// Limit to maximum texture size, even it can go beyond.
786-
// Max texture size will be determined by D3D_FEATURE_LEVEL_, but D3D11 and D3D12 _REQ_TEXTURE2D_U_OR_V_DIMENSION should be the same.
787-
.Width{ std::min(width, static_cast<float>(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION)) },
788-
.Height{ std::min(height, static_cast<float>(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION)) }
789-
782+
.Width{ width },
783+
.Height{ height }
790784
};
791785

792786
// Offset image in order to center it in the window + apply panning.
@@ -818,6 +812,15 @@ void Renderer::update_scale_and_dims_output() noexcept
818812
auto_zoom = std::log2(get_ratio<float>(dims_swap_chain.width, image_w));
819813

820814
scale = std::pow(2.0f, auto_zoom + user_interface.image_zoom);
815+
816+
// Limit scale so we don't exceed min or max texture dims, or stretch image.
817+
const auto ws{ image_w * scale };
818+
const auto hs{ image_h * scale };
819+
if (ws > MAX_TEX_UV<float> || hs > MAX_TEX_UV<float>)
820+
scale = std::min(MAX_TEX_UV<float> / image_w, MAX_TEX_UV<float> / image_h);
821+
else if (ws < 1.0f || hs < 1.0f)
822+
scale = std::max(1.0f / image_w, 1.0f / image_h);
823+
821824
dims_output.width = static_cast<int>(std::ceil(image_w * scale));
822825
dims_output.height = static_cast<int>(std::ceil(image_h * scale));
823826

0 commit comments

Comments
 (0)