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

Function WriteScalar

engine/graphics/src/graphics_vertices.cpp:316–362  ·  view source on GitHub ↗

Supports writing a single scalar as a scalar, matrix, or vector. If the destination is a matrix, the value is written on the diagonal. If the destination is a scalar or a vector, the value is written to each component of the destination.

Source from the content-addressed store, hash-verified

314 // If the destination is a matrix, the value is written on the diagonal.
315 // If the destination is a scalar or a vector, the value is written to each component of the destination.
316 static void WriteScalar(uint8_t* write_ptr, const UnpackAttributeData& src_data, const UnpackAttributeData& dst_data)
317 {
318 uint8_t src_value_buffer[sizeof(float)];
319 const uint8_t* src_value_read_ptr = 0x0; // Use const for read-only
320 const uint32_t dst_element_byte_width = DataTypeToByteWidth(dst_data.m_DataType);
321
322 // Case 1: If source and destination data types are identical, skip conversion
323 if (src_data.m_DataType == dst_data.m_DataType)
324 {
325 src_value_read_ptr = src_data.m_ValuePtr;
326 }
327 // Case 2: Convert source data type to float and then write to intermediate buffer
328 else
329 {
330 float float_value = VertexAttributeDataTypeToFloat(src_data.m_DataType, src_data.m_ValuePtr);
331
332 WriteVertexAttributeFromFloat(src_value_buffer, float_value, dst_data.m_DataType);
333 src_value_read_ptr = src_value_buffer;
334 }
335
336 if (dst_data.m_IsMatrix)
337 {
338 const uint32_t dst_row_col_count = VectorTypeToMatrixRowColCount(dst_data.m_VectorType);
339 const uint32_t total_elements = dst_row_col_count * dst_row_col_count;
340
341 // Optimize matrix writing, write the diagonal elements, zero-fill non-diagonal
342 for (uint32_t i = 0; i < total_elements; ++i)
343 {
344 if (i % (dst_row_col_count + 1) == 0) // Diagonal check
345 {
346 memcpy(write_ptr, src_value_read_ptr, dst_element_byte_width);
347 }
348 else
349 {
350 memset(write_ptr, 0, dst_element_byte_width);
351 }
352 write_ptr += dst_element_byte_width;
353 }
354 }
355 else // Handle scalar or vector case
356 {
357 for (uint32_t i = 0; i < dst_data.m_ElementCount; ++i)
358 {
359 memcpy(write_ptr + i * dst_element_byte_width, src_value_read_ptr, dst_element_byte_width);
360 }
361 }
362 }
363
364 static void UnpackWriteAttributeBySemanticType(
365 UnpackAttributeDataState& unpack_state,

Callers 1

WriteAttributesFunction · 0.85

Tested by

no test coverage detected