gltf_buffer_view.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**************************************************************************/
  2. /* gltf_buffer_view.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "gltf_buffer_view.h"
  31. #include "gltf_buffer_view.compat.inc"
  32. #include "../gltf_state.h"
  33. void GLTFBufferView::_bind_methods() {
  34. ClassDB::bind_method(D_METHOD("load_buffer_view_data", "state"), &GLTFBufferView::load_buffer_view_data);
  35. ClassDB::bind_static_method("GLTFBufferView", D_METHOD("from_dictionary", "dictionary"), &GLTFBufferView::from_dictionary);
  36. ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFBufferView::to_dictionary);
  37. ClassDB::bind_method(D_METHOD("get_buffer"), &GLTFBufferView::get_buffer);
  38. ClassDB::bind_method(D_METHOD("set_buffer", "buffer"), &GLTFBufferView::set_buffer);
  39. ClassDB::bind_method(D_METHOD("get_byte_offset"), &GLTFBufferView::get_byte_offset);
  40. ClassDB::bind_method(D_METHOD("set_byte_offset", "byte_offset"), &GLTFBufferView::set_byte_offset);
  41. ClassDB::bind_method(D_METHOD("get_byte_length"), &GLTFBufferView::get_byte_length);
  42. ClassDB::bind_method(D_METHOD("set_byte_length", "byte_length"), &GLTFBufferView::set_byte_length);
  43. ClassDB::bind_method(D_METHOD("get_byte_stride"), &GLTFBufferView::get_byte_stride);
  44. ClassDB::bind_method(D_METHOD("set_byte_stride", "byte_stride"), &GLTFBufferView::set_byte_stride);
  45. ClassDB::bind_method(D_METHOD("get_indices"), &GLTFBufferView::get_indices);
  46. ClassDB::bind_method(D_METHOD("set_indices", "indices"), &GLTFBufferView::set_indices);
  47. ClassDB::bind_method(D_METHOD("get_vertex_attributes"), &GLTFBufferView::get_vertex_attributes);
  48. ClassDB::bind_method(D_METHOD("set_vertex_attributes", "is_attributes"), &GLTFBufferView::set_vertex_attributes);
  49. ADD_PROPERTY(PropertyInfo(Variant::INT, "buffer"), "set_buffer", "get_buffer"); // GLTFBufferIndex
  50. ADD_PROPERTY(PropertyInfo(Variant::INT, "byte_offset"), "set_byte_offset", "get_byte_offset"); // int
  51. ADD_PROPERTY(PropertyInfo(Variant::INT, "byte_length"), "set_byte_length", "get_byte_length"); // int
  52. ADD_PROPERTY(PropertyInfo(Variant::INT, "byte_stride"), "set_byte_stride", "get_byte_stride"); // int
  53. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "indices"), "set_indices", "get_indices"); // bool
  54. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vertex_attributes"), "set_vertex_attributes", "get_vertex_attributes"); // bool
  55. }
  56. GLTFBufferIndex GLTFBufferView::get_buffer() const {
  57. return buffer;
  58. }
  59. void GLTFBufferView::set_buffer(GLTFBufferIndex p_buffer) {
  60. buffer = p_buffer;
  61. }
  62. int64_t GLTFBufferView::get_byte_offset() const {
  63. return byte_offset;
  64. }
  65. void GLTFBufferView::set_byte_offset(int64_t p_byte_offset) {
  66. byte_offset = p_byte_offset;
  67. }
  68. int64_t GLTFBufferView::get_byte_length() const {
  69. return byte_length;
  70. }
  71. void GLTFBufferView::set_byte_length(int64_t p_byte_length) {
  72. byte_length = p_byte_length;
  73. }
  74. int64_t GLTFBufferView::get_byte_stride() const {
  75. return byte_stride;
  76. }
  77. void GLTFBufferView::set_byte_stride(int64_t p_byte_stride) {
  78. byte_stride = p_byte_stride;
  79. }
  80. bool GLTFBufferView::get_indices() const {
  81. return indices;
  82. }
  83. void GLTFBufferView::set_indices(bool p_indices) {
  84. indices = p_indices;
  85. }
  86. bool GLTFBufferView::get_vertex_attributes() const {
  87. return vertex_attributes;
  88. }
  89. void GLTFBufferView::set_vertex_attributes(bool p_attributes) {
  90. vertex_attributes = p_attributes;
  91. }
  92. Vector<uint8_t> GLTFBufferView::load_buffer_view_data(const Ref<GLTFState> p_gltf_state) const {
  93. ERR_FAIL_COND_V(p_gltf_state.is_null(), Vector<uint8_t>());
  94. const Vector<PackedByteArray> &buffers = p_gltf_state->get_buffers();
  95. ERR_FAIL_INDEX_V(buffer, buffers.size(), Vector<uint8_t>());
  96. const PackedByteArray &buffer_data = buffers[buffer];
  97. const int64_t byte_end = byte_offset + byte_length;
  98. // Note that for buffer views with a byte stride, the parts of this data which get used may
  99. // only be determined in combination with the accessors that reference this buffer view.
  100. return buffer_data.slice(byte_offset, byte_end);
  101. }
  102. GLTFBufferViewIndex GLTFBufferView::write_new_buffer_view_into_state(const Ref<GLTFState> &p_gltf_state, const PackedByteArray &p_input_data, const int64_t p_alignment, const ArrayBufferTarget p_target, const int64_t p_byte_stride, const GLTFBufferIndex p_buffer_index, const bool p_deduplicate) {
  103. ERR_FAIL_COND_V_MSG(p_buffer_index < 0, -1, "Buffer index must be greater than or equal to zero.");
  104. const bool target_is_indices = p_target == ArrayBufferTarget::TARGET_ELEMENT_ARRAY_BUFFER;
  105. const bool target_is_vertex_attributes = p_target == ArrayBufferTarget::TARGET_ARRAY_BUFFER;
  106. if (target_is_vertex_attributes) {
  107. ERR_FAIL_COND_V_MSG(p_byte_stride < 4 || p_byte_stride % 4 != 0, -1, "glTF export: Vertex attributes using TARGET_ARRAY_BUFFER must have a byte stride that is a multiple of 4 as required by section 3.6.2.4 of the glTF specification.");
  108. }
  109. // Check for duplicate buffer views before adding a new one.
  110. Vector<Ref<GLTFBufferView>> state_buffer_views = p_gltf_state->get_buffer_views();
  111. const int buffer_view_index = state_buffer_views.size();
  112. if (p_deduplicate) {
  113. for (int i = 0; i < buffer_view_index; i++) {
  114. const Ref<GLTFBufferView> &existing_buffer_view = state_buffer_views[i];
  115. if (existing_buffer_view->get_byte_offset() % p_alignment == 0 &&
  116. existing_buffer_view->get_byte_length() == p_input_data.size() &&
  117. existing_buffer_view->get_byte_stride() == p_byte_stride &&
  118. existing_buffer_view->get_indices() == target_is_indices &&
  119. existing_buffer_view->get_vertex_attributes() == target_is_vertex_attributes) {
  120. if (existing_buffer_view->load_buffer_view_data(p_gltf_state) == p_input_data) {
  121. // Duplicate found, return the index of the existing buffer view.
  122. return i;
  123. }
  124. }
  125. }
  126. }
  127. // Write the data into the buffer at the specified index.
  128. Vector<PackedByteArray> state_buffers = p_gltf_state->get_buffers();
  129. if (state_buffers.size() <= p_buffer_index) {
  130. state_buffers.resize(p_buffer_index + 1);
  131. }
  132. PackedByteArray state_buffer = state_buffers[p_buffer_index];
  133. const int64_t input_data_size = p_input_data.size();
  134. // This is used by accessors. The byte offset of an accessor MUST be a multiple of the accessor's component size.
  135. // https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#data-alignment
  136. int64_t byte_offset = state_buffer.size();
  137. if (byte_offset % p_alignment != 0) {
  138. byte_offset += p_alignment - (byte_offset % p_alignment);
  139. }
  140. state_buffer.resize(byte_offset + input_data_size);
  141. uint8_t *buffer_ptr = state_buffer.ptrw();
  142. memcpy(buffer_ptr + byte_offset, p_input_data.ptr(), input_data_size);
  143. state_buffers.set(p_buffer_index, state_buffer);
  144. p_gltf_state->set_buffers(state_buffers);
  145. // Create a new GLTFBufferView that references the new buffer.
  146. Ref<GLTFBufferView> buffer_view;
  147. buffer_view.instantiate();
  148. buffer_view->set_buffer(p_buffer_index);
  149. buffer_view->set_byte_offset(byte_offset);
  150. buffer_view->set_byte_length(input_data_size);
  151. buffer_view->set_byte_stride(p_byte_stride);
  152. buffer_view->set_indices(target_is_indices);
  153. buffer_view->set_vertex_attributes(target_is_vertex_attributes);
  154. // Add the new buffer view to the state.
  155. state_buffer_views.append(buffer_view);
  156. p_gltf_state->set_buffer_views(state_buffer_views);
  157. return buffer_view_index;
  158. }
  159. Ref<GLTFBufferView> GLTFBufferView::from_dictionary(const Dictionary &p_dict) {
  160. // See https://github.com/KhronosGroup/glTF/blob/main/specification/2.0/schema/bufferView.schema.json
  161. Ref<GLTFBufferView> buffer_view;
  162. buffer_view.instantiate();
  163. if (p_dict.has("buffer")) {
  164. buffer_view->set_buffer(p_dict["buffer"]);
  165. }
  166. if (p_dict.has("byteLength")) {
  167. buffer_view->set_byte_length(p_dict["byteLength"]);
  168. }
  169. if (p_dict.has("byteOffset")) {
  170. buffer_view->set_byte_offset(p_dict["byteOffset"]);
  171. }
  172. if (p_dict.has("byteStride")) {
  173. buffer_view->byte_stride = p_dict["byteStride"];
  174. if (buffer_view->byte_stride < 4 || buffer_view->byte_stride > 252 || buffer_view->byte_stride % 4 != 0) {
  175. ERR_PRINT("glTF import: Invalid byte stride " + itos(buffer_view->byte_stride) + " for buffer view. If defined, byte stride must be a multiple of 4 and between 4 and 252.");
  176. }
  177. }
  178. if (p_dict.has("target")) {
  179. const int target = p_dict["target"];
  180. buffer_view->indices = target == ArrayBufferTarget::TARGET_ELEMENT_ARRAY_BUFFER;
  181. buffer_view->vertex_attributes = target == ArrayBufferTarget::TARGET_ARRAY_BUFFER;
  182. }
  183. return buffer_view;
  184. }
  185. Dictionary GLTFBufferView::to_dictionary() const {
  186. Dictionary dict;
  187. ERR_FAIL_COND_V_MSG(buffer == -1, dict, "Buffer index must be set to a valid buffer before converting to Dictionary.");
  188. dict["buffer"] = buffer;
  189. dict["byteLength"] = byte_length;
  190. if (byte_offset != 0) {
  191. dict["byteOffset"] = byte_offset;
  192. }
  193. if (byte_stride != -1) {
  194. dict["byteStride"] = byte_stride;
  195. }
  196. if (indices) {
  197. dict["target"] = ArrayBufferTarget::TARGET_ELEMENT_ARRAY_BUFFER;
  198. } else if (vertex_attributes) {
  199. dict["target"] = ArrayBufferTarget::TARGET_ARRAY_BUFFER;
  200. }
  201. return dict;
  202. }