Skip to content

Commit fc9b0bb

Browse files
committed
Merge branch 'master' of github.com:Devsh-Graphics-Programming/Nabla-Examples-and-Tests into autoexposure_ex
2 parents 734fea9 + 8d778a8 commit fc9b0bb

File tree

6 files changed

+118
-104
lines changed

6 files changed

+118
-104
lines changed

62_CAD/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ set(EXAMPLE_SOURCES
1111
"${CMAKE_CURRENT_SOURCE_DIR}/Polyline.h"
1212
"${CMAKE_CURRENT_SOURCE_DIR}/DrawResourcesFiller.cpp"
1313
"${CMAKE_CURRENT_SOURCE_DIR}/DrawResourcesFiller.h"
14+
"${CMAKE_CURRENT_SOURCE_DIR}/SingleLineText.cpp"
15+
"${CMAKE_CURRENT_SOURCE_DIR}/SingleLineText.h"
1416
"../../src/nbl/ext/TextRendering/TextRendering.cpp" # TODO: this one will be a part of dedicated Nabla ext called "TextRendering" later on which uses MSDF + Freetype
1517
)
1618
set(EXAMPLE_INCLUDES

62_CAD/DrawResourcesFiller.cpp

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -902,77 +902,4 @@ uint32_t DrawResourcesFiller::addMSDFTexture(core::smart_refctd_ptr<ICPUBuffer>
902902
hash,
903903
intendedNextSubmit
904904
);
905-
}
906-
907-
SingleLineText::SingleLineText(core::smart_refctd_ptr<nbl::ext::TextRendering::FontFace>&& face, const std::string& text)
908-
{
909-
m_face = std::move(face);
910-
glyphBoxes.reserve(text.length());
911-
912-
// Position transform
913-
float64_t2 currentPos = float32_t2(0.0, 0.0);
914-
for (uint32_t i = 0; i < text.length(); i++)
915-
{
916-
const auto glyphIndex = m_face->getGlyphIndex(wchar_t(text.at(i)));
917-
const auto glyphMetrics = m_face->getGlyphMetricss(glyphIndex);
918-
const bool skipGenerateGlyph = (glyphIndex == 0 || (glyphMetrics.size.x == 0.0 && glyphMetrics.size.y == 0.0));
919-
920-
if (!skipGenerateGlyph)
921-
{
922-
#ifdef VERIFY_DEBUG
923-
msdfgen::Shape shape = m_face->generateGlyphShape(glyphIndex);
924-
_NBL_BREAK_IF(shape.contours.empty());
925-
#endif
926-
GlyphBox glyphBbox =
927-
{
928-
.topLeft = currentPos + glyphMetrics.horizontalBearing,
929-
.size = glyphMetrics.size,
930-
.glyphIdx = glyphIndex,
931-
};
932-
glyphBoxes.push_back(glyphBbox);
933-
}
934-
currentPos += glyphMetrics.advance;
935-
}
936-
}
937-
938-
void SingleLineText::Draw(
939-
DrawResourcesFiller& drawResourcesFiller,
940-
SIntendedSubmitInfo& intendedNextSubmit,
941-
const float64_t2& baselineStart,
942-
const float32_t2& scale,
943-
const float32_t& rotateAngle)
944-
{
945-
float32_t2 vec(cos(rotateAngle), sin(rotateAngle));
946-
float64_t3x3 rotationMulScaleMat =
947-
{
948-
vec.x * scale.x, vec.y * scale.y, 0.0,
949-
-vec.y * scale.x, vec.x * scale.y, 0.0,
950-
0.0, 0.0, 1.0,
951-
};
952-
float64_t3x3 translationMat =
953-
{
954-
1.0, 0.0, baselineStart.x,
955-
0.0, 1.0, baselineStart.y,
956-
0.0, 0.0, 1.0,
957-
};
958-
float64_t3x3 transformation = mul(translationMat, rotationMulScaleMat);
959-
960-
LineStyleInfo lineStyle = {};
961-
lineStyle.color = float32_t4(1.0, 1.0, 1.0, 1.0);
962-
const uint32_t styleIdx = drawResourcesFiller.addLineStyle_SubmitIfNeeded(lineStyle, intendedNextSubmit);
963-
auto glyphObjectIdx = drawResourcesFiller.addMainObject_SubmitIfNeeded(styleIdx, intendedNextSubmit);
964-
965-
for (const auto& glyphBox : glyphBoxes)
966-
{
967-
const float64_t2 topLeft = mul(transformation, float64_t3(glyphBox.topLeft, 1.0)).xy;
968-
const float64_t2 dirU = mul(transformation, float64_t3(glyphBox.size.x, 0.0, 0.0)).xy;
969-
const float64_t2 dirV = mul(transformation, float64_t3(0.0, -glyphBox.size.y, 0.0)).xy;
970-
971-
// float32_t3 xx = float64_t3(0.0, -glyphBox.size.y, 0.0);
972-
const float32_t aspectRatio = static_cast<float32_t>(glm::length(dirV) / glm::length(dirU)); // check if you can just do: (glyphBox.size.y * scale.y) / glyphBox.size.x * scale.x)
973-
const float32_t2 minUV = m_face->getUV(float32_t2(0.0f,0.0f), glyphBox.size, drawResourcesFiller.getMSDFResolution(), MSDFPixelRange);
974-
drawResourcesFiller.drawFontGlyph(m_face.get(), glyphBox.glyphIdx, topLeft, dirU, aspectRatio, minUV, glyphObjectIdx, intendedNextSubmit);
975-
}
976-
977-
}
978-
905+
}

62_CAD/DrawResourcesFiller.h

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
#pragma once
12
#include "Polyline.h"
23
#include "Hatch.h"
34
#include "IndexAllocator.h"
45
#include <nbl/video/utilities/SIntendedSubmitInfo.h>
56
#include <nbl/core/containers/LRUCache.h>
6-
#include "nbl/ext/TextRendering/TextRendering.h"
7+
#include <nbl/ext/TextRendering/TextRendering.h>
78

89
using namespace nbl;
910
using namespace nbl::video;
@@ -386,32 +387,3 @@ struct DrawResourcesFiller
386387
bool m_hasInitializedMSDFTextureArrays = false;
387388
};
388389

389-
class SingleLineText
390-
{
391-
public:
392-
// constructs and fills the `glyphBoxes`
393-
SingleLineText(core::smart_refctd_ptr<nbl::ext::TextRendering::FontFace>&& face, const std::string& text);
394-
395-
// iterates over `glyphBoxes` generates textures msdfs if failed to add to cache (through that lambda you put)
396-
// void Draw(DrawResourcesFiller& drawResourcesFiller, SIntendedSubmitInfo& intendedNextSubmit);
397-
void Draw(
398-
DrawResourcesFiller& drawResourcesFiller,
399-
SIntendedSubmitInfo& intendedNextSubmit,
400-
const float64_t2& baselineStart = float64_t2(0.0,0.0),
401-
const float32_t2& scale = float64_t2(1.0, 1.0),
402-
const float32_t& rotateAngle = 0);
403-
404-
protected:
405-
406-
struct GlyphBox
407-
{
408-
float64_t2 topLeft;
409-
float32_t2 size;
410-
uint32_t glyphIdx;
411-
uint32_t pad;
412-
};
413-
414-
std::vector<GlyphBox> glyphBoxes;
415-
core::smart_refctd_ptr<nbl::ext::TextRendering::FontFace> m_face;
416-
};
417-

62_CAD/SingleLineText.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "SingleLineText.h"
2+
3+
SingleLineText::SingleLineText(core::smart_refctd_ptr<nbl::ext::TextRendering::FontFace>&& face, const std::string& text)
4+
{
5+
m_face = std::move(face);
6+
glyphBoxes.reserve(text.length());
7+
8+
// Position transform
9+
float64_t2 currentPos = float32_t2(0.0, 0.0);
10+
for (uint32_t i = 0; i < text.length(); i++)
11+
{
12+
const auto glyphIndex = m_face->getGlyphIndex(wchar_t(text.at(i)));
13+
const auto glyphMetrics = m_face->getGlyphMetricss(glyphIndex);
14+
const bool skipGenerateGlyph = (glyphIndex == 0 || (glyphMetrics.size.x == 0.0 && glyphMetrics.size.y == 0.0));
15+
16+
if (!skipGenerateGlyph)
17+
{
18+
#ifdef VERIFY_DEBUG
19+
msdfgen::Shape shape = m_face->generateGlyphShape(glyphIndex);
20+
_NBL_BREAK_IF(shape.contours.empty());
21+
#endif
22+
GlyphBox glyphBbox =
23+
{
24+
.topLeft = currentPos + glyphMetrics.horizontalBearing,
25+
.size = glyphMetrics.size,
26+
.glyphIdx = glyphIndex,
27+
};
28+
glyphBoxes.push_back(glyphBbox);
29+
}
30+
currentPos += glyphMetrics.advance;
31+
}
32+
}
33+
34+
void SingleLineText::Draw(
35+
DrawResourcesFiller& drawResourcesFiller,
36+
SIntendedSubmitInfo& intendedNextSubmit,
37+
const float64_t2& baselineStart,
38+
const float32_t2& scale,
39+
const float32_t& rotateAngle)
40+
{
41+
float32_t2 vec(cos(rotateAngle), sin(rotateAngle));
42+
float64_t3x3 rotationMulScaleMat =
43+
{
44+
vec.x * scale.x, vec.y * scale.y, 0.0,
45+
-vec.y * scale.x, vec.x * scale.y, 0.0,
46+
0.0, 0.0, 1.0,
47+
};
48+
float64_t3x3 translationMat =
49+
{
50+
1.0, 0.0, baselineStart.x,
51+
0.0, 1.0, baselineStart.y,
52+
0.0, 0.0, 1.0,
53+
};
54+
float64_t3x3 transformation = mul(translationMat, rotationMulScaleMat);
55+
56+
LineStyleInfo lineStyle = {};
57+
lineStyle.color = float32_t4(1.0, 1.0, 1.0, 1.0);
58+
const uint32_t styleIdx = drawResourcesFiller.addLineStyle_SubmitIfNeeded(lineStyle, intendedNextSubmit);
59+
auto glyphObjectIdx = drawResourcesFiller.addMainObject_SubmitIfNeeded(styleIdx, intendedNextSubmit);
60+
61+
for (const auto& glyphBox : glyphBoxes)
62+
{
63+
const float64_t2 topLeft = mul(transformation, float64_t3(glyphBox.topLeft, 1.0)).xy;
64+
const float64_t2 dirU = mul(transformation, float64_t3(glyphBox.size.x, 0.0, 0.0)).xy;
65+
const float64_t2 dirV = mul(transformation, float64_t3(0.0, -glyphBox.size.y, 0.0)).xy;
66+
67+
// float32_t3 xx = float64_t3(0.0, -glyphBox.size.y, 0.0);
68+
const float32_t aspectRatio = static_cast<float32_t>(glm::length(dirV) / glm::length(dirU)); // check if you can just do: (glyphBox.size.y * scale.y) / glyphBox.size.x * scale.x)
69+
const float32_t2 minUV = m_face->getUV(float32_t2(0.0f,0.0f), glyphBox.size, drawResourcesFiller.getMSDFResolution(), MSDFPixelRange);
70+
drawResourcesFiller.drawFontGlyph(m_face.get(), glyphBox.glyphIdx, topLeft, dirU, aspectRatio, minUV, glyphObjectIdx, intendedNextSubmit);
71+
}
72+
73+
}
74+

62_CAD/SingleLineText.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#pragma once
2+
#include "DrawResourcesFiller.h"
3+
4+
using namespace nbl;
5+
using namespace nbl::video;
6+
using namespace nbl::core;
7+
using namespace nbl::asset;
8+
using namespace nbl::ext::TextRendering;
9+
10+
class SingleLineText
11+
{
12+
public:
13+
// constructs and fills the `glyphBoxes`
14+
SingleLineText(core::smart_refctd_ptr<nbl::ext::TextRendering::FontFace>&& face, const std::string& text);
15+
16+
// iterates over `glyphBoxes` generates textures msdfs if failed to add to cache (through that lambda you put)
17+
// void Draw(DrawResourcesFiller& drawResourcesFiller, SIntendedSubmitInfo& intendedNextSubmit);
18+
void Draw(
19+
DrawResourcesFiller& drawResourcesFiller,
20+
SIntendedSubmitInfo& intendedNextSubmit,
21+
const float64_t2& baselineStart = float64_t2(0.0,0.0),
22+
const float32_t2& scale = float64_t2(1.0, 1.0),
23+
const float32_t& rotateAngle = 0);
24+
25+
protected:
26+
27+
struct GlyphBox
28+
{
29+
float64_t2 topLeft;
30+
float32_t2 size;
31+
uint32_t glyphIdx;
32+
uint32_t pad;
33+
};
34+
35+
std::vector<GlyphBox> glyphBoxes;
36+
core::smart_refctd_ptr<nbl::ext::TextRendering::FontFace> m_face;
37+
};
38+

62_CAD/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ using namespace video;
2323
#include "Hatch.h"
2424
#include "Polyline.h"
2525
#include "DrawResourcesFiller.h"
26+
#include "SingleLineText.h"
2627

2728
#include "nbl/video/surface/CSurfaceVulkan.h"
2829
#include "nbl/ext/FullScreenTriangle/FullScreenTriangle.h"

0 commit comments

Comments
 (0)