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

Function WriteVector

engine/graphics/src/graphics_vertices.cpp:281–311  ·  view source on GitHub ↗

Supports writing a vector to a scalar, matrix, or vector. Regardless of destination type, if we have fewer elements in the source vector than in the destination vector, the rest of the vector is filled with zeroes.

Source from the content-addressed store, hash-verified

279 // Regardless of destination type, if we have fewer elements in the source vector than in the destination vector,
280 // the rest of the vector is filled with zeroes.
281 static void WriteVector(uint8_t* write_ptr, const UnpackAttributeData& src_data, const UnpackAttributeData& dst_data)
282 {
283 const uint32_t num_src_elements_to_write = dmMath::Min(src_data.m_ElementCount, dst_data.m_ElementCount);
284 const uint32_t num_zero_elements_to_write = dst_data.m_ElementCount - num_src_elements_to_write;
285 const uint32_t dst_element_byte_width = DataTypeToByteWidth(dst_data.m_DataType);
286
287 // Case 1: When src and dst data types match, just copy directly
288 if (src_data.m_DataType == dst_data.m_DataType)
289 {
290 const uint32_t copy_size = num_src_elements_to_write * dst_element_byte_width;
291 memcpy(write_ptr, src_data.m_ValuePtr, copy_size);
292 write_ptr += copy_size; // Advance pointer
293 }
294 // Case 2: Convert source data type to float and then write to destination type
295 else
296 {
297 const uint32_t src_element_byte_width = DataTypeToByteWidth(src_data.m_DataType);
298 for (uint32_t i = 0; i < num_src_elements_to_write; ++i)
299 {
300 float float_value = VertexAttributeDataTypeToFloat(src_data.m_DataType, src_data.m_ValuePtr + i * src_element_byte_width);
301
302 write_ptr = WriteVertexAttributeFromFloat(write_ptr, float_value, dst_data.m_DataType);
303 }
304 }
305
306 // Zero out any remaining elements in the destination
307 if (num_zero_elements_to_write > 0)
308 {
309 memset(write_ptr, 0, num_zero_elements_to_write * dst_element_byte_width);
310 }
311 }
312
313 // Supports writing a single scalar as a scalar, matrix, or vector.
314 // If the destination is a matrix, the value is written on the diagonal.

Callers 1

WriteAttributesFunction · 0.85

Calls 4

MinFunction · 0.85
DataTypeToByteWidthFunction · 0.85

Tested by

no test coverage detected