MCPcopy Create free account
hub / github.com/defold/defold / InitializeTextContext

Function InitializeTextContext

engine/render/src/render/font/font_renderer.cpp:61–111  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

59 }
60
61 void InitializeTextContext(HRenderContext render_context, uint32_t max_characters, uint32_t max_batches)
62 {
63 // TODO: Why does the vertex need to be 16-byte aligned?
64 //DM_STATIC_ASSERT(GetFontVertexSize() % 16 == 0, Invalid_Struct_Size);
65
66 DM_STATIC_ASSERT( MAX_FONT_RENDER_CONSTANTS == MAX_TEXT_RENDER_CONSTANTS, Constant_Arrays_Must_Have_Same_Size );
67
68 TextContext& text_context = render_context->m_TextContext;
69
70 text_context.m_FontRenderBackend = CreateFontRenderBackend();
71
72 text_context.m_MaxVertexCount = max_characters * 6; // 6 vertices per character
73 uint32_t buffer_size = GetFontVertexSize(text_context.m_FontRenderBackend) * text_context.m_MaxVertexCount;
74 text_context.m_ClientBuffer = 0x0;
75 text_context.m_VertexIndex = 0;
76 text_context.m_VerticesFlushed = 0;
77 text_context.m_Frame = 0;
78 text_context.m_PreviousFrame = ~0;
79 text_context.m_TextEntriesFlushed = 0;
80
81 dmMemory::Result r = dmMemory::AlignedMalloc((void**)&text_context.m_ClientBuffer, 16, buffer_size);
82 if (r != dmMemory::RESULT_OK) {
83 dmLogError("Could not allocate text vertex buffer of size %u (%d).", buffer_size, r);
84 return;
85 }
86
87 text_context.m_VertexDecl = CreateVertexDeclaration(text_context.m_FontRenderBackend, render_context->m_GraphicsContext);
88 text_context.m_VertexBuffer = dmGraphics::NewVertexBuffer(render_context->m_GraphicsContext, buffer_size, 0x0, dmGraphics::BUFFER_USAGE_STREAM_DRAW);
89
90 text_context.m_ConstantBuffers.SetCapacity(max_batches); // 1:1 index mapping with render object
91 text_context.m_RenderObjects.SetCapacity(max_batches);
92 text_context.m_RenderObjectIndex = 0;
93
94 // Approximately as we store terminating '\0'
95 text_context.m_TextBuffer.SetCapacity(max_characters);
96 // NOTE: 8 is "arbitrary" heuristic
97 text_context.m_TextEntries.SetCapacity(max_characters / 8);
98
99 for (uint32_t i = 0; i < text_context.m_RenderObjects.Capacity(); ++i)
100 {
101 RenderObject ro;
102 ro.m_SourceBlendFactor = dmGraphics::BLEND_FACTOR_SRC_ALPHA;
103 ro.m_DestinationBlendFactor = dmGraphics::BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
104 ro.m_SetBlendFactors = 1;
105 ro.m_VertexBuffer = text_context.m_VertexBuffer;
106 ro.m_VertexDeclaration = text_context.m_VertexDecl;
107 ro.m_PrimitiveType = dmGraphics::PRIMITIVE_TRIANGLES;
108 text_context.m_RenderObjects.Push(ro);
109 text_context.m_ConstantBuffers.Push(dmRender::NewNamedConstantBuffer());
110 }
111 }
112
113 void FinalizeTextContext(HRenderContext render_context)
114 {

Callers 1

NewRenderContextFunction · 0.85

Calls 9

CreateFontRenderBackendFunction · 0.85
GetFontVertexSizeFunction · 0.85
AlignedMallocFunction · 0.85
CreateVertexDeclarationFunction · 0.85
NewVertexBufferFunction · 0.85
NewNamedConstantBufferFunction · 0.85
SetCapacityMethod · 0.45
CapacityMethod · 0.45
PushMethod · 0.45

Tested by

no test coverage detected