Changeset View
Changeset View
Standalone View
Standalone View
source/graphics/Canvas2D.cpp
Show All 33 Lines | |||||
{ | { | ||||
// Array of 2D elements unrolled into 1D array. | // Array of 2D elements unrolled into 1D array. | ||||
using PlaneArray2D = std::array<float, 12>; | using PlaneArray2D = std::array<float, 12>; | ||||
struct SBindingSlots | struct SBindingSlots | ||||
{ | { | ||||
int32_t transform; | int32_t transform; | ||||
int32_t translation; | |||||
int32_t colorAdd; | int32_t colorAdd; | ||||
int32_t colorMul; | int32_t colorMul; | ||||
int32_t grayscaleFactor; | int32_t grayscaleFactor; | ||||
int32_t tex; | int32_t tex; | ||||
}; | }; | ||||
inline void DrawTextureImpl( | inline void DrawTextureImpl( | ||||
Renderer::Backend::IDeviceCommandContext* deviceCommandContext, | Renderer::Backend::IDeviceCommandContext* deviceCommandContext, | ||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | void BindTechIfNeeded() | ||||
// The canvas technique must be loaded because we can't render UI without it. | // The canvas technique must be loaded because we can't render UI without it. | ||||
ENSURE(Tech); | ENSURE(Tech); | ||||
DeviceCommandContext->SetGraphicsPipelineState( | DeviceCommandContext->SetGraphicsPipelineState( | ||||
Tech->GetGraphicsPipelineStateDesc()); | Tech->GetGraphicsPipelineStateDesc()); | ||||
DeviceCommandContext->BeginPass(); | DeviceCommandContext->BeginPass(); | ||||
Renderer::Backend::IShaderProgram* shader = Tech->GetShader(); | Renderer::Backend::IShaderProgram* shader = Tech->GetShader(); | ||||
BindingSlots.transform = shader->GetBindingSlot(str_transform); | BindingSlots.transform = shader->GetBindingSlot(str_transform); | ||||
BindingSlots.translation = shader->GetBindingSlot(str_translation); | |||||
BindingSlots.colorAdd = shader->GetBindingSlot(str_colorAdd); | BindingSlots.colorAdd = shader->GetBindingSlot(str_colorAdd); | ||||
BindingSlots.colorMul = shader->GetBindingSlot(str_colorMul); | BindingSlots.colorMul = shader->GetBindingSlot(str_colorMul); | ||||
BindingSlots.grayscaleFactor = shader->GetBindingSlot(str_grayscaleFactor); | BindingSlots.grayscaleFactor = shader->GetBindingSlot(str_grayscaleFactor); | ||||
BindingSlots.tex = shader->GetBindingSlot(str_tex); | BindingSlots.tex = shader->GetBindingSlot(str_tex); | ||||
const CMatrix3D transform = GetTransform(); | const CMatrix3D transform = GetTransform(); | ||||
TransformScale = CVector2D(transform._11, transform._22); | |||||
Translation = CVector2D(transform._14, transform._24); | |||||
DeviceCommandContext->SetUniform( | DeviceCommandContext->SetUniform( | ||||
BindingSlots.transform, transform.AsFloatArray()); | BindingSlots.transform, | ||||
transform._11, transform._21, transform._12, transform._22); | |||||
DeviceCommandContext->SetUniform( | |||||
BindingSlots.translation, Translation.AsFloatArray()); | |||||
} | } | ||||
void UnbindTech() | void UnbindTech() | ||||
{ | { | ||||
if (!Tech) | if (!Tech) | ||||
return; | return; | ||||
DeviceCommandContext->EndPass(); | DeviceCommandContext->EndPass(); | ||||
Show All 18 Lines | CMatrix3D GetTransform() | ||||
transform = projection * transform; | transform = projection * transform; | ||||
return transform; | return transform; | ||||
} | } | ||||
uint32_t WidthInPixels = 1; | uint32_t WidthInPixels = 1; | ||||
uint32_t HeightInPixels = 1; | uint32_t HeightInPixels = 1; | ||||
float Scale = 1.0f; | float Scale = 1.0f; | ||||
CVector2D TransformScale; | |||||
CVector2D Translation; | |||||
Renderer::Backend::IDeviceCommandContext* DeviceCommandContext = nullptr; | Renderer::Backend::IDeviceCommandContext* DeviceCommandContext = nullptr; | ||||
CShaderTechniquePtr Tech; | CShaderTechniquePtr Tech; | ||||
// We assume that the shader can't be destroyed while it's bound. So these | // We assume that the shader can't be destroyed while it's bound. So these | ||||
// bindings remain valid while the shader is alive. | // bindings remain valid while the shader is alive. | ||||
SBindingSlots BindingSlots; | SBindingSlots BindingSlots; | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 271 Lines • ▼ Show 20 Lines | |||||
void CCanvas2D::DrawText(CTextRenderer& textRenderer) | void CCanvas2D::DrawText(CTextRenderer& textRenderer) | ||||
{ | { | ||||
m->BindTechIfNeeded(); | m->BindTechIfNeeded(); | ||||
m->DeviceCommandContext->SetUniform( | m->DeviceCommandContext->SetUniform( | ||||
m->BindingSlots.grayscaleFactor, 0.0f); | m->BindingSlots.grayscaleFactor, 0.0f); | ||||
textRenderer.Render(m->DeviceCommandContext, m->Tech->GetShader(), m->GetTransform()); | textRenderer.Render( | ||||
m->DeviceCommandContext, m->Tech->GetShader(), m->TransformScale, m->Translation); | |||||
} | } | ||||
void CCanvas2D::Flush() | void CCanvas2D::Flush() | ||||
{ | { | ||||
m->UnbindTech(); | m->UnbindTech(); | ||||
} | } |
Wildfire Games · Phabricator