Skip to content

Commit

Permalink
v2.2.1 - use only single buffer type for everything, remove text drop…
Browse files Browse the repository at this point in the history
…shadows as user land responsibility
  • Loading branch information
inanevin committed Oct 12, 2024
1 parent f888937 commit f974db6
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 157 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ add_library(Lina::VG ALIAS ${PROJECT_NAME})
target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_compile_definitions(${PROJECT_NAME} PUBLIC LINAVG_VERSION_MAJOR=2)
target_compile_definitions(${PROJECT_NAME} PUBLIC LINAVG_VERSION_MINOR=2)
target_compile_definitions(${PROJECT_NAME} PUBLIC LINAVG_VERSION_PATCH=0)
target_compile_definitions(${PROJECT_NAME} PUBLIC LINAVG_VERSION_PATCH=1)

#--------------------------------------------------------------------
# Subdirectories & linking
Expand Down
1 change: 0 additions & 1 deletion Example/include/Backends/GL/GLBackend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ struct Texture

void StartFrame();
void DrawDefault(DrawBuffer* buf);
void DrawText(DrawBufferText* buf);
void EndFrame();
void SaveAPIState();
void RestoreAPIState();
Expand Down
71 changes: 46 additions & 25 deletions Example/src/Backends/GL/GLBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,21 +256,50 @@ namespace LinaVG::Examples
if (m_backendData.m_skipDraw)
return;


SetScissors(buf->clipPosX, buf->clipPosY, buf->clipSizeX, buf->clipSizeY);
ShaderData& data = m_backendData.m_defaultShaderData;
const Vec4 uv = buf->textureUV;

glUseProgram(data.m_handle);
glUniformMatrix4fv(data.m_uniformMap["proj"], 1, GL_FALSE, &m_backendData.m_proj[0][0]);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, buf->textureHandle == NULL_TEXTURE ? 0 : static_cast<Texture*>(buf->textureHandle)->handle);

glUniform1i(data.m_uniformMap["diffuse"], 0);
glUniform1i(data.m_uniformMap["hasTexture"], buf->textureHandle != NULL_TEXTURE);
glUniform4f(data.m_uniformMap["tilingAndOffset"], (GLfloat)uv.x, (GLfloat)uv.y, (GLfloat)uv.z, (GLfloat)uv.w);


if(buf->shapeType == DrawBufferShapeType::Text || buf->shapeType == DrawBufferShapeType::SDFText)
{
ShaderData& data = m_backendData.m_simpleTextShaderData;
glUseProgram(data.m_handle);

glUniformMatrix4fv(data.m_uniformMap["proj"], 1, GL_FALSE, &m_backendData.m_proj[0][0]);

const bool isSDF = buf->shapeType == DrawBufferShapeType::SDFText;
glUniform1i(data.m_uniformMap["isSDF"], isSDF);
glUniform1i(data.m_uniformMap["diffuse"], 0);

if(isSDF)
{
SDFMaterial* mat = static_cast<SDFMaterial*>(buf->userData);
const float thickness = 1.0f - Math::Clamp(mat->thickness, 0.0f, 1.0f);
const float softness = Math::Clamp(mat->softness, 0.0f, 10.0f) * 0.1f;
const float outlineThickness = Math::Clamp(mat->outlineThickness, 0.0f, 1.0f);
glUniform1f(data.m_uniformMap["thickness"], thickness);
glUniform1f(data.m_uniformMap["softness"], softness);
glUniform1i(data.m_uniformMap["outlineEnabled"], outlineThickness != 0.0f ? 1 : 0);
glUniform1f(data.m_uniformMap["outlineThickness"], outlineThickness);
glUniform4f(data.m_uniformMap["outlineColor"], mat->outlineColor.x, mat->outlineColor.y,mat->outlineColor.z, mat->outlineColor.w);
}

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_fontTexture);
}
else
{
ShaderData& data = m_backendData.m_defaultShaderData;
const Vec4 uv = buf->textureUV;

glUseProgram(data.m_handle);
glUniformMatrix4fv(data.m_uniformMap["proj"], 1, GL_FALSE, &m_backendData.m_proj[0][0]);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, buf->textureHandle == NULL_TEXTURE ? 0 : static_cast<Texture*>(buf->textureHandle)->handle);

glUniform1i(data.m_uniformMap["diffuse"], 0);
glUniform1i(data.m_uniformMap["hasTexture"], buf->textureHandle != NULL_TEXTURE);
glUniform4f(data.m_uniformMap["tilingAndOffset"], (GLfloat)uv.x, (GLfloat)uv.y, (GLfloat)uv.z, (GLfloat)uv.w);
}

glBindBuffer(GL_ARRAY_BUFFER, m_backendData.m_vbo);
glBufferData(GL_ARRAY_BUFFER, buf->vertexBuffer.m_size * sizeof(Vertex), (const GLvoid*)buf->vertexBuffer.begin(), GL_STREAM_DRAW);
Expand All @@ -284,7 +313,7 @@ namespace LinaVG::Examples
Config.debugCurrentTriangleCount += int((float)buf->indexBuffer.m_size / 3.0f);
Config.debugCurrentVertexCount += buf->vertexBuffer.m_size;
}

/*
void GLBackend::DrawText(DrawBufferText* buf)
{
if (m_backendData.m_skipDraw)
Expand All @@ -300,15 +329,7 @@ namespace LinaVG::Examples
if(buf->isSDF)
{
SDFMaterial* mat = static_cast<SDFMaterial*>(buf->userData);
const float thickness = 1.0f - Math::Clamp(mat->thickness, 0.0f, 1.0f);
const float softness = Math::Clamp(mat->softness, 0.0f, 10.0f) * 0.1f;
const float outlineThickness = Math::Clamp(mat->outlineThickness, 0.0f, 1.0f);
glUniform1f(data.m_uniformMap["thickness"], thickness);
glUniform1f(data.m_uniformMap["softness"], softness);
glUniform1i(data.m_uniformMap["outlineEnabled"], outlineThickness != 0.0f ? 1 : 0);
glUniform1f(data.m_uniformMap["outlineThickness"], outlineThickness);
glUniform4f(data.m_uniformMap["outlineColor"], mat->outlineColor.x, mat->outlineColor.y,mat->outlineColor.z, mat->outlineColor.w);
}
glActiveTexture(GL_TEXTURE0);
Expand All @@ -325,7 +346,7 @@ namespace LinaVG::Examples
Config.debugCurrentDrawCalls++;
Config.debugCurrentTriangleCount += int((float)buf->indexBuffer.m_size / 3.0f);
Config.debugCurrentVertexCount += buf->vertexBuffer.m_size;
}
}*/

void GLBackend::SetScissors(BackendHandle x, BackendHandle y, BackendHandle width, BackendHandle height)
{
Expand Down
24 changes: 9 additions & 15 deletions Example/src/DemoScreens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace LinaVG
m_screenDescriptions.push_back("You can use flat colors, alphas, vertical / horizontal gradients and rounded gradients. Also, textures w/ custom UV offset & tiling are supported.");
m_screenDescriptions.push_back("LinaVG supports inner as well as outer outlines. Outlines also support all previous coloring and texturing options, as well as both filled & non-filled objects.");
m_screenDescriptions.push_back("LinaVG supports single lines, multi-lines as well as bezier curves. Lines can have left/right or both caps, multi-lines can have 4 different types of joints. All lines also support outlines, coloring & texturing.");
m_screenDescriptions.push_back("Texts support drop shadows, flat colors & vertical/horizontal gradients. SDF texts also support outlines, individual thickness as well as softness factors. LinaVG also provides alignment, wrapping & spacing options.");
m_screenDescriptions.push_back("Texts support alignment, wrapping, spacing options, along with flat / gradient colors.");
m_screenDescriptions.push_back("You can suply varying draw order to DrawXXX commands in order to support z-ordering.");
m_screenDescriptions.push_back("You can use global clipping variables to create clipping rectangles for any shape you are drawing. Press C to toggle clipping.");
m_screenDescriptions.push_back("Here are some examples of animated shapes you can draw with LinaVG.");
Expand Down Expand Up @@ -127,7 +127,8 @@ namespace LinaVG
m_sdfMaterial5->softness = 0.5f;
m_sdfMaterial5->outlineThickness = 0.3f;
m_sdfMaterial5->outlineColor = Vec4(0,0,0,1);
m_sdfMaterial6->thickness = 0.5f;
m_sdfMaterial6->thickness = 0.6f;
m_sdfMaterial6->softness = 0.8f;
m_sdfMaterial6->outlineThickness = 0.0f;
}

Expand Down Expand Up @@ -709,25 +710,19 @@ namespace LinaVG
ExampleApp::Get()->GetLVGDrawer().DrawText("This is a normal text.", startPos, textOpts, m_rotateAngle, 1);

startPos.x += 350;
textOpts.dropShadowOffset = Vec2(2, 2);
ExampleApp::Get()->GetLVGDrawer().DrawText("Drop shadow.", startPos, textOpts, m_rotateAngle, 1);

startPos.x += 350;
textOpts.color.start = Vec4(1, 0, 0, 1);
textOpts.color.start = Vec4(0, 0, 1, 1);
ExampleApp::Get()->GetLVGDrawer().DrawText("Gradient color.", startPos, textOpts, m_rotateAngle, 1);

textOpts.color.start = Vec4(1, 0, 0, 1);
textOpts.color.start = Vec4(0, 0, 1, 1);
ExampleApp::Get()->GetLVGDrawer().DrawText("This is one with a gradient color.", startPos, textOpts, m_rotateAngle, 1);

startPos.x = screenSize.x * 0.05f;
startPos.y += 350;
textOpts.wrapWidth = 100;
textOpts.dropShadowColor = Vec4(1, 0, 0, 1);
textOpts.color = Vec4(1, 1, 1, 1);
ExampleApp::Get()->GetLVGDrawer().DrawText("This is a wrapped text with a colored drop shadow.", startPos, textOpts, m_rotateAngle, 1);
ExampleApp::Get()->GetLVGDrawer().DrawText("This is a wrapped text.", startPos, textOpts, m_rotateAngle, 1);

startPos.x += 365;
textOpts.wrapWidth = 100;
textOpts.alignment = TextAlignment::Center;
textOpts.dropShadowOffset = Vec2(0.0f, 0.0f);
textOpts.color.start = Vec4(0.6f, 0.6f, 0.6f, 1);
textOpts.color.end = Vec4(1, 1, 1, 1);
textOpts.color.gradientType = GradientType::Vertical;
Expand Down Expand Up @@ -782,9 +777,8 @@ namespace LinaVG
ExampleApp::Get()->GetLVGDrawer().DrawText("Thicker outline.", startPos, sdfTextOptions, m_rotateAngle, 1);

startPos.y += 50;
sdfTextOptions.dropShadowOffset = Vec2(5, 5);
sdfTextOptions.userData = m_sdfMaterial5;
ExampleApp::Get()->GetLVGDrawer().DrawText("Drop shadow.", startPos, sdfTextOptions, m_rotateAngle, 1);
ExampleApp::Get()->GetLVGDrawer().DrawText("Just lose it.", startPos, sdfTextOptions, m_rotateAngle, 1);

startPos.y = beforeSDFStartPos;
startPos.x += 930;
Expand Down
3 changes: 1 addition & 2 deletions Example/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ namespace LinaVG
// Init LinaVG
m_renderingBackend = new GLBackend();

m_lvgDrawer.GetCallbacks().drawDefault = std::bind(&GLBackend::DrawDefault, m_renderingBackend, std::placeholders::_1);
m_lvgDrawer.GetCallbacks().drawText = std::bind(&GLBackend::DrawText, m_renderingBackend, std::placeholders::_1);
m_lvgDrawer.GetCallbacks().draw = std::bind(&GLBackend::DrawDefault, m_renderingBackend, std::placeholders::_1);
m_lvgText.GetCallbacks().atlasNeedsUpdate = std::bind(&GLBackend::OnAtlasUpdate, m_renderingBackend, std::placeholders::_1);
m_demoScreens.Initialize();

Expand Down
5 changes: 1 addition & 4 deletions include/LinaVG/Core/BufferStore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ namespace LinaVG
struct BufferStoreData
{
Array<DrawBuffer> m_defaultBuffers;
Array<DrawBufferText> m_simpleTextBuffers;
Array<int> m_drawOrders;
LINAVG_MAP<uint32_t, TextCache> m_textCache;
int m_gcFrameCounter = 0;
Expand All @@ -84,15 +83,13 @@ namespace LinaVG
int GetBufferIndexInDefaultArray(DrawBuffer* buf);
int GetBufferIndexInCharArray(DrawBuffer* buf);
DrawBuffer& GetDefaultBuffer(void* userData, int drawOrder, DrawBufferShapeType shapeType, TextureHandle txtHandle, const Vec4& textureUV);
DrawBufferText& GetSimpleTextBuffer(void* userData, Font* font, int drawOrder, bool isDropShadow, bool isSDF);
void AddTextCache(uint32_t sid, const TextOptions& opts, DrawBuffer* buf, int vtxStart, int indexStart);
TextCache* CheckTextCache(uint32_t sid, const TextOptions& opts, DrawBuffer* buf);
};

struct BufferStoreCallbacks
{
std::function<void(DrawBuffer* buf)> drawDefault;
std::function<void(DrawBufferText* buf)> drawText;
std::function<void(DrawBuffer* buf)> draw;
};

class BufferStore
Expand Down
50 changes: 8 additions & 42 deletions include/LinaVG/Core/Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,6 @@ namespace LinaVG
font = opts.font;
color = opts.color;
textScale = opts.textScale;
dropShadowColor = opts.dropShadowColor;
dropShadowOffset = opts.dropShadowOffset;
alignment = opts.alignment;
spacing = opts.spacing;
newLineSpacing = opts.newLineSpacing;
Expand Down Expand Up @@ -464,12 +462,6 @@ namespace LinaVG
if (wordWrap != opts.wordWrap)
return false;

if (dropShadowOffset.x != 0.0f || dropShadowOffset.y != 0.0f)
{
if (!CheckColors(dropShadowColor, opts.dropShadowColor))
return false;
}

return alignment == opts.alignment && textScale == opts.textScale && spacing == opts.spacing && newLineSpacing == opts.newLineSpacing && wrapWidth == opts.wrapWidth;
}

Expand Down Expand Up @@ -514,16 +506,6 @@ namespace LinaVG
/// </summary>
bool wordWrap = true;

/// <summary>
/// Drop shadow m_color, lol.
/// </summary>
Vec4 dropShadowColor = Vec4(0.0f, 0.0f, 0.0f, 1.0f);

/// <summary>
/// Defines how far the drop shadow is rendered, in screen-units.
/// Set to 0.0f, 0.0f to disable drop-shadows.
/// </summary>
Vec2 dropShadowOffset = Vec2(0.0f, 0.0f);

/// <summary>
/// Defines custom clip rectangle for text vertices.
Expand Down Expand Up @@ -657,14 +639,9 @@ namespace LinaVG
int gcCollectInterval = 600;

/// <summary>
/// This amount of default buffers are reserved upon Renderer initialization. Saves time from allocating/deallocating buffers in runtime.
/// This amount of buffers are reserved upon Renderer initialization. Saves time from allocating/deallocating buffers in runtime.
/// </summary>
int defaultBufferReserve = 10;

/// <summary>
/// This amount of default buffers are reserved upon Renderer initialization. Saves time from allocating/deallocating buffers in runtime.
/// </summary>
int textBuffersReserve = 10;
int defaultBufferReserve = 50;

/// <summary>
/// Set this to your own function to receive error callbacks from LinaVG.
Expand Down Expand Up @@ -737,21 +714,23 @@ namespace LinaVG
enum class DrawBufferType
{
Default,
SimpleText,
Text,
SDFText,
};

enum class DrawBufferShapeType
{
DropShadow,
Shape,
Text,
SDFText,
AA,
};

struct DrawBuffer
{
DrawBuffer(){};
DrawBuffer(void* userData, int drawOrder, DrawBufferType type, DrawBufferShapeType shapeType, TextureHandle txtHandle, const Vec4& txtUV, BackendHandle clipPosX, BackendHandle clipPosY, BackendHandle clipSizeX, BackendHandle clipSizeY)
: drawOrder(drawOrder), drawBufferType(type), shapeType(shapeType), userData(userData), textureHandle(txtHandle), textureUV(txtUV)
DrawBuffer(void* userData, int drawOrder, DrawBufferShapeType shapeType, TextureHandle txtHandle, const Vec4& txtUV, BackendHandle clipPosX, BackendHandle clipPosY, BackendHandle clipSizeX, BackendHandle clipSizeY)
: drawOrder(drawOrder), shapeType(shapeType), userData(userData), textureHandle(txtHandle), textureUV(txtUV)
{
this->clipPosX = clipPosX;
this->clipPosY = clipPosY;
Expand All @@ -761,7 +740,6 @@ namespace LinaVG

Array<Vertex> vertexBuffer;
Array<Index> indexBuffer;
DrawBufferType drawBufferType = DrawBufferType::Default;
DrawBufferShapeType shapeType = DrawBufferShapeType::Shape;
TextureHandle textureHandle = NULL_TEXTURE;
Vec4 textureUV = Vec4(1.0f, 1.0f, 0.0f, 0.0f);
Expand Down Expand Up @@ -805,16 +783,4 @@ namespace LinaVG
}
};

struct DrawBufferText : public DrawBuffer
{
DrawBufferText(){};
DrawBufferText(void* userData, Font* font, int drawOrder, bool isDropShadow, bool sdf, BackendHandle clipPosX, BackendHandle clipPosY, BackendHandle clipSizeX, BackendHandle clipSizeY)
: DrawBuffer(userData, drawOrder, DrawBufferType::SimpleText, isDropShadow ? DrawBufferShapeType::DropShadow : DrawBufferShapeType::Shape, 0, Vec4(), clipPosX, clipPosY, clipSizeX, clipSizeY), font(font), isDropShadow(isDropShadow), isSDF(sdf){};

Font* font = nullptr;
bool isDropShadow = false;
bool isSDF = false;
};


} // namespace LinaVG
Loading

0 comments on commit f974db6

Please sign in to comment.