Skip to content

Commit

Permalink
Canvas: Prepare text rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
kullingk committed Oct 17, 2024
1 parent 4c578ff commit 037efdd
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 38 deletions.
2 changes: 1 addition & 1 deletion contrib/cppcore
Submodule cppcore updated 2 files
+51 −4 doc/Container.md
+1 −0 doc/index.md
51 changes: 46 additions & 5 deletions src/Engine/RenderBackend/2D/CanvasRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ CanvasRenderer::CanvasRenderer(i32 numLayers, i32 x, i32 y, i32 w, i32 h) :
mNumLayers(numLayers),
mFont(nullptr),
mMesh(nullptr),
mTexts(nullptr) {
mFont2MeshMap() {
setResolution(x, y, w, h);
}

Expand All @@ -141,6 +141,28 @@ void CanvasRenderer::preRender(RenderBackendService *rbSrv) {
rbSrv->setMatrix(MatrixType::Projection, m);
}

static void renumberIndices(const DrawCmd &dc, ui16 offset) {
if (offset > 0) {
for (size_t j = 0; j < dc.NumIndices; ++j) {
dc.Indices[j] += static_cast<ui16>(offset);
}
}
}

static bool hasTexts(const DrawCmdArray &drawCmdArray) {
if (drawCmdArray.isEmpty()) {
return true;
}

for (size_t i = 0; i < drawCmdArray.size(); ++i) {
if (drawCmdArray[i]->UseFont != nullptr) {
return true;
}
}

return false;
}

void CanvasRenderer::render(RenderBackendService *rbSrv) {
if (rbSrv == nullptr) {
return;
Expand All @@ -150,6 +172,7 @@ void CanvasRenderer::render(RenderBackendService *rbSrv) {
return;
}

// Create not textured geometry
if (mMesh == nullptr) {
mMesh = new Mesh("2d", VertexType::RenderVertex, IndexType::UnsignedShort);
Material *mat2D = MaterialBuilder::create2DMaterial();
Expand All @@ -160,6 +183,19 @@ void CanvasRenderer::render(RenderBackendService *rbSrv) {
mMesh->setMaterial(mat2D);
}

// Load all font-meshes
if (mFont2MeshMap.isEmpty()) {
if (hasTexts(mDrawCmdArray)) {
for (size_t i = 0; i < mDrawCmdArray.size(); ++i) {
const auto &dc = mDrawCmdArray[i];
const String &keyName = dc->UseFont->Name;
const String meshName = "text." + keyName;
Material *matFont = MaterialBuilder::createTextMaterial(keyName);

}
}
}

PrimitiveType prim = PrimitiveType::TriangleList;
size_t numVertices = 0l, numIndices = 0l;
for (size_t i=0; i<mDrawCmdArray.size(); ++i) {
Expand All @@ -169,13 +205,18 @@ void CanvasRenderer::render(RenderBackendService *rbSrv) {
continue;
}

const ui32 lastIndex = mMesh->getLastIndex();
if (numVertices > 0) {
for (size_t j = 0; j < dc.NumIndices; ++j) {
dc.Indices[j] += static_cast<ui16>(numVertices);
if (dc.UseFont != nullptr) {
const String &fontKey = dc.UseFont->Name;
Mesh *text = nullptr;
if (mFont2MeshMap.hasKey(fontKey)) {
mFont2MeshMap.getValue(fontKey, text);
}
} else {
}

const ui32 lastIndex = mMesh->getLastIndex();
renumberIndices(dc, numVertices);

//Debugging::MeshDiagnostic::dumpVertices(dc.Vertices, dc.NumVertices);
mMesh->attachVertices(dc.Vertices, dc.NumVertices * sizeof(RenderVert));
mMesh->attachIndices(dc.Indices, dc.NumIndices * sizeof(ui16));
Expand Down
10 changes: 6 additions & 4 deletions src/Engine/RenderBackend/2D/CanvasRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#include "Common/osre_common.h"
#include "RenderBackend/RenderCommon.h"
#include <cppcore/Container/THashMap.h>

namespace OSRE {
namespace RenderBackend {
Expand All @@ -44,15 +45,15 @@ struct Point2Df {
f32 X, Y; /// Coordinate components
};

using DrawCmdArray = cppcore::TArray<DrawCmd *>;

//-------------------------------------------------------------------------------------------------
/// @ingroup Engine
///
/// @brief This class implements a canvas renderer.
//-------------------------------------------------------------------------------------------------
class OSRE_EXPORT CanvasRenderer : IRenderPath {
public:
using DrawCmdArray = cppcore::TArray<DrawCmd*>;

public:
/// @brief The class constructor.
/// @param numLayers The number of layers.
/// @param x The x position.
Expand Down Expand Up @@ -170,7 +171,8 @@ class OSRE_EXPORT CanvasRenderer : IRenderPath {
i32 mNumLayers;
Font *mFont;
Mesh *mMesh;
Mesh *mTexts;
using Font2MeshMap = cppcore::THashMap<String, Mesh*>;
Font2MeshMap mFont2MeshMap;
};

inline void CanvasRenderer::setDirty() {
Expand Down
94 changes: 67 additions & 27 deletions src/Engine/RenderBackend/MaterialBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,33 +51,6 @@ static void addMaterialParameter(Material *mat) {
shader->addUniformBuffer("Projection");
}

// see https://jasonliang.js.org/batch-renderer.html
const String vertex_2d =
getGLSLVersionString_400() +
getGLSLRenderVertexLayout() +
"out vec3 v_color0;\n"
"out vec2 v_texindex;\n"
"uniform mat4 Model;\n"
"uniform mat4 View;\n"
"uniform mat4 Projection;\n"
"void main() {\n"
" v_color0 = color0;\n"
" mat4 u_mvp = Projection * View * Model;\n"
" gl_Position = u_mvp * vec4(position, 1.0);\n"
" v_texindex = texcoord0;\n"
"}\n";

const String fragment_2d = "#version 330 core\n"
"in vec2 v_texindex;\n"
"in vec3 v_color0;\n"
"out vec4 f_color;\n"
"uniform sampler2D u_texture;\n"
"void main() {\n"
" f_color = texture(u_texture, v_texindex);\n"
" if (f_color.r==0.0 || f_color.g ==0.0 || f_color.b==0.0)\n"
" f_color = vec4(v_color0,1);\n"
"}\n";

static constexpr c8 Render2DMat[] = "2d_mat";

Material *MaterialBuilder::create2DMaterial() {
Expand All @@ -89,6 +62,31 @@ Material *MaterialBuilder::create2DMaterial() {

mat = materialCache->create(Render2DMat);

const String vertex_2d =
getGLSLVersionString_400() +
getGLSLRenderVertexLayout() +
"out vec3 v_color0;\n"
"out vec2 v_texindex;\n"
"uniform mat4 Model;\n"
"uniform mat4 View;\n"
"uniform mat4 Projection;\n"
"void main() {\n"
" v_color0 = color0;\n"
" mat4 u_mvp = Projection * View * Model;\n"
" gl_Position = u_mvp * vec4(position, 1.0);\n"
" v_texindex = texcoord0;\n"
"}\n";
const String fragment_2d = "#version 330 core\n"
"in vec2 v_texindex;\n"
"in vec3 v_color0;\n"
"out vec4 f_color;\n"
"uniform sampler2D u_texture;\n"
"void main() {\n"
" f_color = texture(u_texture, v_texindex);\n"
" if (f_color.r==0.0 || f_color.g ==0.0 || f_color.b==0.0)\n"
" f_color = vec4(v_color0,1);\n"
"}\n";

ShaderSourceArray shArray;
shArray[static_cast<ui32>(ShaderType::SH_VertexShaderType)] = vertex_2d;
shArray[static_cast<ui32>(ShaderType::SH_FragmentShaderType)] = fragment_2d;
Expand All @@ -103,6 +101,48 @@ Material *MaterialBuilder::create2DMaterial() {
return mat;
}

Material *MaterialBuilder::createTextMaterial(const String &fontName) {
if (fontName.empty()) {
return nullptr;
}

MaterialBuilder::MaterialCache *materialCache = sData->mMaterialCache;
Material *mat = materialCache->find(fontName);
if (nullptr != mat) {
return mat;
}

mat = materialCache->create(Render2DMat);
const String vertex_2d =
getGLSLVersionString_400() +
getGLSLRenderVertexLayout() +
"out vec3 v_color0;\n"
"out vec2 v_texindex;\n"
"uniform mat4 Model;\n"
"uniform mat4 View;\n"
"uniform mat4 Projection;\n"
"void main() {\n"
" v_color0 = color0;\n"
" mat4 u_mvp = Projection * View * Model;\n"
" gl_Position = u_mvp * vec4(position, 1.0);\n"
" v_texindex = texcoord0;\n"
"}\n";

const String fragment_2d =
"#version 330 core\n"
"in vec2 v_texindex;\n"
"in vec3 v_color0;\n"
"out vec4 f_color;\n"
"uniform sampler2D u_texture;\n"
"void main() {\n"
" f_color = texture(u_texture, v_texindex);\n"
" if (f_color.r==0.0 || f_color.g ==0.0 || f_color.b==0.0)\n"
" f_color = vec4(v_color0,1);\n"
"}\n";

return nullptr;
}

static const String GLSLVsSrc =
getGLSLVersionString_400() +
getNewLine() +
Expand Down
1 change: 1 addition & 0 deletions src/Engine/RenderBackend/MaterialBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class OSRE_EXPORT MaterialBuilder {
static RenderBackend::Material *createDebugRenderTextMaterial();

static Material *create2DMaterial();
static Material *createTextMaterial(const String &fontName);

private:
/// @brief The default class constructor.
Expand Down

0 comments on commit 037efdd

Please sign in to comment.