18#include <glm/gtc/type_ptr.hpp>
26#define g_device (ctx().tryGet<GfxDevice>())
27#define g_initialized (ctx().state().initialized)
28#define g_geometryManager (ctx().tryGet<GeometryManager>())
29#define g_resourceManager (ctx().tryGet<resource::ResourceManager>())
30#define g_immediateDraw (ctx().tryGet<ImmediateDraw>())
31#define g_immediateDrawActive (ctx().state().immediate_draw_active)
32#define g_currentViewProjection (ctx().state().current_view_projection)
33#define g_renderContext (ctx().tryGet<RenderContext>())
65 desc.
blend = imm->blendMode();
78 uintptr_t layoutPtr,
u32 layoutCount,
bool dynamic) {
84 static constexpr u32 MAX_ATTRS = 8;
85 if (layoutCount == 0 || layoutCount > MAX_ATTRS) {
86 ES_LOG_WARN(
"geometry_init: invalid layoutCount {}, max {}", layoutCount, MAX_ATTRS);
91 if (!layoutData)
return;
93 static constexpr const char* ATTR_NAMES[] = {
94 "a_attr0",
"a_attr1",
"a_attr2",
"a_attr3",
95 "a_attr4",
"a_attr5",
"a_attr6",
"a_attr7"
98 std::vector<VertexAttribute> attrs;
99 attrs.reserve(layoutCount);
100 for (
u32 i = 0; i < layoutCount; ++i) {
101 attrs.emplace_back(
static_cast<ShaderDataType>(layoutData[i]), ATTR_NAMES[i]);
106 verticesPtr,
static_cast<u64>(vertexCount) * (layout.
getStride() /
sizeof(
f32)),
107 "geometry_init.vertices");
108 if (!vertices)
return;
109 geom->init(
ctx().require<GfxDevice>(), vertices, vertexCount, std::move(layout), dynamic);
119 if (!indices)
return;
120 geom->setIndices(indices, indexCount);
130 if (!indices)
return;
131 geom->setIndices(indices, indexCount);
141 verticesPtr,
static_cast<u64>(vertexCount) * (geom->getVertexStride() /
sizeof(
f32)),
142 "geometry_updateVertices");
143 if (!vertices)
return;
144 geom->updateVertices(vertices, vertexCount, offset);
161 if (!geom || !geom->isValid())
return;
169 if (!transformData)
return;
170 glm::mat4 transform = glm::make_mat4(transformData);
183 if (!rc)
return false;
189 if (!rec || !rc->materials().layoutFor(rec->
shaderRef))
return false;
192 if (!geom || !geom->isValid())
return true;
215 rc->materials().bindForDraw(materialId);
222 uintptr_t uniformsPtr,
u32 uniformCount) {
226 if (!geom || !geom->isValid())
return;
233 const f32* transformData =
boundarySpan<f32>(transformPtr, 16,
"draw_meshWithUniforms.transform");
234 if (!transformData)
return;
235 glm::mat4 transform = glm::make_mat4(transformData);
241 static constexpr const char* UNIFORM_NAMES[] = {
242 "u_time",
"u_color",
"u_intensity",
"u_scale",
"u_offset",
243 "u_param0",
"u_param1",
"u_param2",
"u_param3",
"u_param4",
244 "u_vec0",
"u_vec1",
"u_vec2",
"u_vec3",
245 "u_texture0",
"u_texture1",
"u_texture2",
"u_texture3"
247 static constexpr u32 UNIFORM_NAME_COUNT =
sizeof(UNIFORM_NAMES) /
sizeof(UNIFORM_NAMES[0]);
249 const f32* uniforms =
boundarySpan<f32>(uniformsPtr, uniformCount,
"draw_meshWithUniforms.uniforms");
250 if (uniformCount != 0 && !uniforms)
return;
255 while (idx + 2 <= uniformCount) {
256 auto type =
static_cast<i32>(uniforms[idx++]);
257 auto nameId =
static_cast<i32>(uniforms[idx++]);
258 const u32 arity = (type == 10) ? 2u : (type >= 1 && type <= 4 ? static_cast<u32>(type) : 0u);
259 if (uniformCount - idx < arity)
break;
261 const char* name = (nameId >= 0 &&
static_cast<u32>(nameId) < UNIFORM_NAME_COUNT)
262 ? UNIFORM_NAMES[nameId] :
"u_unknown";
266 f32 value = uniforms[idx++];
271 glm::vec2 value(uniforms[idx], uniforms[idx + 1]);
277 glm::vec3 value(uniforms[idx], uniforms[idx + 1], uniforms[idx + 2]);
283 glm::vec4 value(uniforms[idx], uniforms[idx + 1],
284 uniforms[idx + 2], uniforms[idx + 3]);
290 i32 slot =
static_cast<i32>(uniforms[idx++]);
291 u32 textureId =
static_cast<u32>(uniforms[idx++]);
Always-on validation for JS-supplied pointer+length pairs at the WASM boundary.
GPU buffer abstractions for vertex and index data.
Custom geometry for user-defined mesh rendering.
#define g_geometryManager
#define g_immediateDrawActive
#define g_resourceManager
#define g_currentViewProjection
Abstract graphics device interface.
#define ES_LOG_WARN(...)
Logs a warning message.
Engine-side material registry — the resolved "how to draw" for a material handle.
Rendering context containing all renderer state.
Frame resources and the shared scene rendering contract.
Central resource management system.
Custom geometry for user-defined mesh rendering.
const IndexBuffer * indexBuffer() const
Gets the index buffer (null when non-indexed).
bool hasIndices() const
Checks if geometry uses indices.
u32 getVertexCount() const
Gets the vertex count.
void bind(GfxDevice &device) const
Binds the geometry's buffers for the next draw.
u32 getIndexCount() const
Gets the index count.
VertexLayoutHandle layoutHandle() const
The device vertex layout a pipeline drawing this geometry must reference.
Abstract graphics device interface.
bool programReady(ShaderHandle program)
Whether program can be drawn with now, without waiting: linked, or finished linking in the background...
void invalidatePipelineCache()
Forces the next setPipeline to re-apply, dropping the cached current pipeline.
bool skipPendingPrograms() const
virtual void drawElements(u32 indexCount, GfxDataType indexType, u32 byteOffset)=0
Draws indexed triangles.
void setPipeline(PipelineHandle handle)
Binds a pipeline: applies its program, blend, depth, stencil compare/op and culling.
virtual void drawArrays(u32 first, u32 vertexCount)=0
Draws non-indexed triangles.
PipelineHandle createPipeline(const PipelineDesc &desc)
Resolves a pipeline description to a cached handle (creating it on first use).
GPU shader program for rendering.
void commitParams()
Uploads the shadow (when dirty) and binds this shader's params UBO at DRAW_PARAMS_BINDING.
void setUniform(const std::string &name, i32 value) const
Sets an integer uniform.
ShaderHandle handle() const
Gets the device program handle.
2D texture for GPU rendering
TextureHandle handle() const
Gets the device texture handle.
Describes the layout of vertex data in a buffer.
u32 getStride() const
Gets the stride (total size) of one vertex.
Handle< esengine::Shader > ShaderHandle
Handle to a shader resource.
Handle< esengine::Texture > TextureHandle
Handle to a texture resource.
void geometry_updateVertices(u32 handle, uintptr_t verticesPtr, u32 vertexCount, u32 offset)
const T * boundarySpan(uintptr_t ptr, u64 count, const char *what)
Validates a JS-supplied pointer to count elements of T and returns it typed.
std::int32_t i32
Signed 32-bit integer.
bool geometry_isValid(u32 handle)
void geometry_release(u32 handle)
static void applyMeshPipeline(GfxDevice &device, const Shader &shader, const CustomGeometry &geom)
std::uint64_t u64
Unsigned 64-bit integer.
bool draw_meshWithMaterial(u32 geometryHandle, u32 materialId)
ShaderDataType
Vertex attribute data types.
static void flushImmediateDrawIfActive()
void draw_mesh(u32 geometryHandle, u32 shaderHandle, uintptr_t transformPtr)
void geometry_setIndices32(u32 handle, uintptr_t indicesPtr, u32 indexCount)
std::uint32_t u32
Unsigned 32-bit integer.
static void drawGeometry(GfxDevice &device, CustomGeometry &geom)
std::uint16_t u16
Unsigned 16-bit integer.
void geometry_init(u32 handle, uintptr_t verticesPtr, u32 vertexCount, uintptr_t layoutPtr, u32 layoutCount, bool dynamic)
void geometry_setIndices16(u32 handle, uintptr_t indicesPtr, u32 indexCount)
void draw_meshWithUniforms(u32 geometryHandle, u32 shaderHandle, uintptr_t transformPtr, uintptr_t uniformsPtr, u32 uniformCount)
static EstellaContext & ctx()
EstellaContext & activeCtx()
The engine context every binding routes through — single source of truth.
float f32
32-bit floating point
A material's resolved render state (P0) plus its packed std140 constants (P1).
resource::ShaderHandle shaderRef
u32 shader
Program id; 0 means "use the path's default batch shader".
Immutable description of a draw pipeline. Equality keys the pipeline cache, so two draws with the sam...
VertexLayoutHandle vertexLayout