Add clamp mode to Draw2d (#6630)

Signed-off-by: abrmich <abrmich@amazon.com>
This commit is contained in:
michabr
2022-01-05 15:27:52 -08:00
committed by GitHub
parent 6c3d5c434e
commit 01c5fb7817
3 changed files with 35 additions and 8 deletions
+10 -1
View File
@@ -50,6 +50,7 @@ public: // types
{
AZ::Vector3 color = AZ::Vector3(1.0f, 1.0f, 1.0f);
Rounding pixelRounding = Rounding::Nearest;
bool m_clamp = false;
RenderState m_renderState;
};
@@ -145,6 +146,7 @@ public: // member functions
virtual void DrawQuad(AZ::Data::Instance<AZ::RPI::Image> image,
VertexPosColUV* verts,
Rounding pixelRounding = Rounding::Nearest,
bool clamp = false,
const RenderState& renderState = RenderState{});
//! Draw a line
@@ -257,6 +259,8 @@ protected: // types and constants
{
AZ::RHI::ShaderInputImageIndex m_imageInputIndex;
AZ::RHI::ShaderInputConstantIndex m_viewProjInputIndex;
AZ::RPI::ShaderVariantId m_shaderOptionsClamp;
AZ::RPI::ShaderVariantId m_shaderOptionsWrap;
};
class DeferredPrimitive
@@ -281,6 +285,7 @@ protected: // types and constants
AZ::Vector2 m_texCoords[4];
uint32 m_packedColors[4];
AZ::Data::Instance<AZ::RPI::Image> m_image;
bool m_clamp;
RenderState m_renderState;
};
@@ -455,11 +460,12 @@ public: // member functions
//! See IDraw2d:DrawQuad for parameter descriptions
void DrawQuad(AZ::Data::Instance<AZ::RPI::Image> image, CDraw2d::VertexPosColUV* verts,
IDraw2d::Rounding pixelRounding = IDraw2d::Rounding::Nearest,
bool clamp = false,
const CDraw2d::RenderState& renderState = CDraw2d::RenderState{})
{
if (m_draw2d)
{
m_draw2d->DrawQuad(image, verts, pixelRounding, renderState);
m_draw2d->DrawQuad(image, verts, pixelRounding, clamp, renderState);
}
}
@@ -545,6 +551,9 @@ public: // member functions
//! Set the base state (that blend mode etc is combined with) used for images, default is GS_NODEPTHTEST.
void SetImageDepthState(const AZ::RHI::DepthState& depthState) { m_imageOptions.m_renderState.m_depthState = depthState; }
//! Set image clamp mode
void SetImageClamp(bool clamp) { m_imageOptions.m_clamp = clamp; }
//! Set the text font.
void SetTextFont(AZStd::string_view fontName) { m_textOptions.fontName = fontName; }
+17 -5
View File
@@ -103,10 +103,7 @@ void CDraw2d::OnBootstrapSceneReady(AZ::RPI::Scene* bootstrapScene)
LyShinePassRequestBus::EventResult(uiCanvasPass, sceneId, &LyShinePassRequestBus::Events::GetUiCanvasPass);
m_dynamicDraw = AZ::RPI::DynamicDrawInterface::Get()->CreateDynamicDrawContext();
AZ::RPI::ShaderOptionList shaderOptions;
shaderOptions.push_back(AZ::RPI::ShaderOption(AZ::Name("o_useColorChannels"), AZ::Name("true")));
shaderOptions.push_back(AZ::RPI::ShaderOption(AZ::Name("o_clamp"), AZ::Name("false")));
m_dynamicDraw->InitShaderWithVariant(shader, &shaderOptions);
m_dynamicDraw->InitShader(shader);
m_dynamicDraw->InitVertexFormat(
{ {"POSITION", AZ::RHI::Format::R32G32B32_FLOAT},
{"COLOR", AZ::RHI::Format::B8G8R8A8_UNORM},
@@ -136,6 +133,16 @@ void CDraw2d::OnBootstrapSceneReady(AZ::RPI::Scene* bootstrapScene)
m_shaderData.m_viewProjInputIndex = layout->FindShaderInputConstantIndex(AZ::Name(worldToProjIndexName));
AZ_Error("Draw2d", m_shaderData.m_viewProjInputIndex.IsValid(), "Failed to find shader input constant %s.",
worldToProjIndexName);
// Cache shader variants that will be used
AZ::RPI::ShaderOptionList shaderOptionsClamp;
shaderOptionsClamp.push_back(AZ::RPI::ShaderOption(AZ::Name("o_clamp"), AZ::Name("true")));
shaderOptionsClamp.push_back(AZ::RPI::ShaderOption(AZ::Name("o_useColorChannels"), AZ::Name("true")));
m_shaderData.m_shaderOptionsClamp = m_dynamicDraw->UseShaderVariant(shaderOptionsClamp);
AZ::RPI::ShaderOptionList shaderOptionsWrap;
shaderOptionsWrap.push_back(AZ::RPI::ShaderOption(AZ::Name("o_clamp"), AZ::Name("false")));
shaderOptionsWrap.push_back(AZ::RPI::ShaderOption(AZ::Name("o_useColorChannels"), AZ::Name("true")));
m_shaderData.m_shaderOptionsWrap = m_dynamicDraw->UseShaderVariant(shaderOptionsWrap);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -181,6 +188,8 @@ void CDraw2d::DrawImage(AZ::Data::Instance<AZ::RPI::Image> image, AZ::Vector2 po
quad.m_image = image;
quad.m_clamp = actualImageOptions->m_clamp;
// add the blendMode flags to the base state
quad.m_renderState = actualImageOptions->m_renderState;
@@ -206,7 +215,7 @@ void CDraw2d::DrawImageAligned(AZ::Data::Instance<AZ::RPI::Image> image, AZ::Vec
////////////////////////////////////////////////////////////////////////////////////////////////////
void CDraw2d::DrawQuad(AZ::Data::Instance<AZ::RPI::Image> image, VertexPosColUV* verts, Rounding pixelRounding,
const CDraw2d::RenderState& renderState)
bool clamp, const CDraw2d::RenderState& renderState)
{
// define quad
DeferredQuad quad;
@@ -217,6 +226,7 @@ void CDraw2d::DrawQuad(AZ::Data::Instance<AZ::RPI::Image> image, VertexPosColUV*
quad.m_packedColors[i] = PackARGB8888(verts[i].color);
}
quad.m_image = image;
quad.m_clamp = clamp;
// add the blendMode flags to the base state
quad.m_renderState = renderState;
@@ -766,6 +776,8 @@ void CDraw2d::DeferredQuad::Draw(AZ::RHI::Ptr<AZ::RPI::DynamicDrawContext> dynam
vertices[i].st = Vec2(m_texCoords[j].GetX(), m_texCoords[j].GetY());
}
dynamicDraw->SetShaderVariant(m_clamp ? shaderData.m_shaderOptionsClamp : shaderData.m_shaderOptionsWrap);
// Set up per draw SRG
AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> drawSrg = dynamicDraw->NewDrawSrg();
+8 -2
View File
@@ -668,7 +668,7 @@ void CLyShine::LoadUiCursor()
{
if (!m_cursorImagePathToLoad.empty())
{
m_uiCursorTexture = CDraw2d::LoadTexture(m_cursorImagePathToLoad); // LYSHINE_ATOM_TODO - add clamp option to draw2d and set cursor to clamp
m_uiCursorTexture = CDraw2d::LoadTexture(m_cursorImagePathToLoad);
m_cursorImagePathToLoad.clear();
}
}
@@ -691,7 +691,13 @@ void CLyShine::RenderUiCursor()
AZ::RHI::Size cursorSize = m_uiCursorTexture->GetDescriptor().m_size;
const AZ::Vector2 dimensions(aznumeric_cast<float>(cursorSize.m_width), aznumeric_cast<float>(cursorSize.m_height));
m_draw2d->DrawImage(m_uiCursorTexture, position, dimensions);
CDraw2d::ImageOptions imageOptions;
imageOptions.m_clamp = true;
const float opacity = 1.0f;
const float rotation = 0.0f;
const AZ::Vector2* pivotPoint = nullptr;
const AZ::Vector2* minMaxTexCoords = nullptr;
m_draw2d->DrawImage(m_uiCursorTexture, position, dimensions, opacity, rotation, pivotPoint, minMaxTexCoords, &imageOptions);
}
#ifndef _RELEASE