| 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 | |
| 31 | #include "gltf_buffer_view.h" |
| 32 | #include "gltf_buffer_view.compat.inc" |
| 33 | |
| 34 | #include "../gltf_state.h" |
| 35 | |
| 36 | void GLTFBufferView::_bind_methods() { |
| 37 | ClassDB::bind_method(D_METHOD("load_buffer_view_data", "state"), &GLTFBufferView::load_buffer_view_data); |
| 38 | |
| 39 | ClassDB::bind_static_method("GLTFBufferView", D_METHOD("from_dictionary", "dictionary"), &GLTFBufferView::from_dictionary); |
| 40 | ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFBufferView::to_dictionary); |
| 41 | |
| 42 | ClassDB::bind_method(D_METHOD("get_buffer"), &GLTFBufferView::get_buffer); |
| 43 | ClassDB::bind_method(D_METHOD("set_buffer", "buffer"), &GLTFBufferView::set_buffer); |
| 44 | ClassDB::bind_method(D_METHOD("get_byte_offset"), &GLTFBufferView::get_byte_offset); |
| 45 | ClassDB::bind_method(D_METHOD("set_byte_offset", "byte_offset"), &GLTFBufferView::set_byte_offset); |
| 46 | ClassDB::bind_method(D_METHOD("get_byte_length"), &GLTFBufferView::get_byte_length); |
| 47 | ClassDB::bind_method(D_METHOD("set_byte_length", "byte_length"), &GLTFBufferView::set_byte_length); |
| 48 | ClassDB::bind_method(D_METHOD("get_byte_stride"), &GLTFBufferView::get_byte_stride); |
| 49 | ClassDB::bind_method(D_METHOD("set_byte_stride", "byte_stride"), &GLTFBufferView::set_byte_stride); |
| 50 | ClassDB::bind_method(D_METHOD("get_indices"), &GLTFBufferView::get_indices); |
| 51 | ClassDB::bind_method(D_METHOD("set_indices", "indices"), &GLTFBufferView::set_indices); |
| 52 | ClassDB::bind_method(D_METHOD("get_vertex_attributes"), &GLTFBufferView::get_vertex_attributes); |
| 53 | ClassDB::bind_method(D_METHOD("set_vertex_attributes", "is_attributes"), &GLTFBufferView::set_vertex_attributes); |
| 54 | |
| 55 | ADD_PROPERTY(PropertyInfo(Variant::INT, "buffer"), "set_buffer", "get_buffer"); // GLTFBufferIndex |
| 56 | ADD_PROPERTY(PropertyInfo(Variant::INT, "byte_offset"), "set_byte_offset", "get_byte_offset"); // int |
| 57 | ADD_PROPERTY(PropertyInfo(Variant::INT, "byte_length"), "set_byte_length", "get_byte_length"); // int |
| 58 | ADD_PROPERTY(PropertyInfo(Variant::INT, "byte_stride"), "set_byte_stride", "get_byte_stride"); // int |
| 59 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "indices"), "set_indices", "get_indices"); // bool |
| 60 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vertex_attributes"), "set_vertex_attributes", "get_vertex_attributes"); // bool |
| 61 | } |
| 62 | |
| 63 | GLTFBufferIndex GLTFBufferView::get_buffer() const { |
| 64 | return buffer; |
| 65 | } |
| 66 | |
| 67 | void GLTFBufferView::set_buffer(GLTFBufferIndex p_buffer) { |
| 68 | buffer = p_buffer; |
| 69 | } |
| 70 | |
| 71 | int64_t GLTFBufferView::get_byte_offset() const { |
| 72 | return byte_offset; |
| 73 | } |
| 74 | |
| 75 | void GLTFBufferView::set_byte_offset(int64_t p_byte_offset) { |
| 76 | byte_offset = p_byte_offset; |
| 77 | } |
| 78 | |
| 79 | int64_t GLTFBufferView::get_byte_length() const { |
| 80 | return byte_length; |
| 81 | } |
| 82 | |
| 83 | void GLTFBufferView::set_byte_length(int64_t p_byte_length) { |
| 84 | byte_length = p_byte_length; |
| 85 | } |
| 86 | |
| 87 | int64_t GLTFBufferView::get_byte_stride() const { |
| 88 | return byte_stride; |
| 89 | } |
| 90 | |
| 91 | void GLTFBufferView::set_byte_stride(int64_t p_byte_stride) { |
| 92 | byte_stride = p_byte_stride; |
| 93 | } |
| 94 | |
| 95 | bool GLTFBufferView::get_indices() const { |
| 96 | return indices; |
| 97 | } |
| 98 | |
| 99 | void GLTFBufferView::set_indices(bool p_indices) { |
| 100 | indices = p_indices; |
| 101 | } |
| 102 | |
| 103 | bool GLTFBufferView::get_vertex_attributes() const { |
| 104 | return vertex_attributes; |
| 105 | } |
| 106 | |
| 107 | void GLTFBufferView::set_vertex_attributes(bool p_attributes) { |
| 108 | vertex_attributes = p_attributes; |
| 109 | } |
| 110 | |
| 111 | Vector<uint8_t> GLTFBufferView::load_buffer_view_data(const Ref<GLTFState> p_gltf_state) const { |
| 112 | ERR_FAIL_COND_V(p_gltf_state.is_null(), Vector<uint8_t>()); |
| 113 | const Vector<PackedByteArray> &buffers = p_gltf_state->get_buffers(); |
| 114 | ERR_FAIL_INDEX_V(buffer, buffers.size(), Vector<uint8_t>()); |
| 115 | const PackedByteArray &buffer_data = buffers[buffer]; |
| 116 | const int64_t byte_end = byte_offset + byte_length; |
| 117 | // Note that for buffer views with a byte stride, the parts of this data which get used may |
| 118 | // only be determined in combination with the accessors that reference this buffer view. |
| 119 | return buffer_data.slice(byte_offset, byte_end); |
| 120 | } |
| 121 | |
| 122 | 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) { |
| 123 | ERR_FAIL_COND_V_MSG(p_buffer_index < 0, -1, "Buffer index must be greater than or equal to zero."); |
| 124 | const bool target_is_indices = p_target == ArrayBufferTarget::TARGET_ELEMENT_ARRAY_BUFFER; |
| 125 | const bool target_is_vertex_attributes = p_target == ArrayBufferTarget::TARGET_ARRAY_BUFFER; |
| 126 | if (target_is_vertex_attributes) { |
| 127 | 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."); |
| 128 | } |
| 129 | // Check for duplicate buffer views before adding a new one. |
| 130 | Vector<Ref<GLTFBufferView>> state_buffer_views = p_gltf_state->get_buffer_views(); |
| 131 | const int buffer_view_index = state_buffer_views.size(); |
| 132 | if (p_deduplicate) { |
| 133 | for (int i = 0; i < buffer_view_index; i++) { |
| 134 | const Ref<GLTFBufferView> &existing_buffer_view = state_buffer_views[i]; |
| 135 | if (existing_buffer_view->get_byte_offset() % p_alignment == 0 && |
| 136 | existing_buffer_view->get_byte_length() == p_input_data.size() && |
| 137 | existing_buffer_view->get_byte_stride() == p_byte_stride && |
| 138 | existing_buffer_view->get_indices() == target_is_indices && |
| 139 | existing_buffer_view->get_vertex_attributes() == target_is_vertex_attributes) { |
| 140 | if (existing_buffer_view->load_buffer_view_data(p_gltf_state) == p_input_data) { |
| 141 | // Duplicate found, return the index of the existing buffer view. |
| 142 | return i; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | // Write the data into the buffer at the specified index. |
| 148 | Vector<PackedByteArray> state_buffers = p_gltf_state->get_buffers(); |
| 149 | if (state_buffers.size() <= p_buffer_index) { |
| 150 | state_buffers.resize(p_buffer_index + 1); |
| 151 | } |
| 152 | PackedByteArray state_buffer = state_buffers[p_buffer_index]; |
| 153 | const int64_t input_data_size = p_input_data.size(); |
| 154 | // This is used by accessors. The byte offset of an accessor MUST be a multiple of the accessor's component size. |
| 155 | // https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#data-alignment |
| 156 | int64_t byte_offset = state_buffer.size(); |
| 157 | if (byte_offset % p_alignment != 0) { |
| 158 | byte_offset += p_alignment - (byte_offset % p_alignment); |
| 159 | } |
| 160 | state_buffer.resize(byte_offset + input_data_size); |
| 161 | uint8_t *buffer_ptr = state_buffer.ptrw(); |
| 162 | memcpy(buffer_ptr + byte_offset, p_input_data.ptr(), input_data_size); |
| 163 | state_buffers.set(p_buffer_index, state_buffer); |
| 164 | p_gltf_state->set_buffers(state_buffers); |
| 165 | // Create a new GLTFBufferView that references the new buffer. |
| 166 | Ref<GLTFBufferView> buffer_view; |
| 167 | buffer_view.instantiate(); |
| 168 | buffer_view->set_buffer(p_buffer_index); |
| 169 | buffer_view->set_byte_offset(byte_offset); |
| 170 | buffer_view->set_byte_length(input_data_size); |
| 171 | buffer_view->set_byte_stride(p_byte_stride); |
| 172 | buffer_view->set_indices(target_is_indices); |
| 173 | buffer_view->set_vertex_attributes(target_is_vertex_attributes); |
| 174 | // Add the new buffer view to the state. |
| 175 | state_buffer_views.append(buffer_view); |
| 176 | p_gltf_state->set_buffer_views(state_buffer_views); |
| 177 | return buffer_view_index; |
| 178 | } |
| 179 | |
| 180 | Ref<GLTFBufferView> GLTFBufferView::from_dictionary(const Dictionary &p_dict) { |
| 181 | // See https://github.com/KhronosGroup/glTF/blob/main/specification/2.0/schema/bufferView.schema.json |
| 182 | Ref<GLTFBufferView> buffer_view; |
| 183 | buffer_view.instantiate(); |
| 184 | if (p_dict.has("buffer")) { |
| 185 | buffer_view->set_buffer(p_dict["buffer"]); |
| 186 | } |
| 187 | if (p_dict.has("byteLength")) { |
| 188 | buffer_view->set_byte_length(p_dict["byteLength"]); |
| 189 | } |
| 190 | if (p_dict.has("byteOffset")) { |
| 191 | buffer_view->set_byte_offset(p_dict["byteOffset"]); |
| 192 | } |
| 193 | if (p_dict.has("byteStride")) { |
| 194 | buffer_view->byte_stride = p_dict["byteStride"]; |
| 195 | if (buffer_view->byte_stride < 4 || buffer_view->byte_stride > 252 || buffer_view->byte_stride % 4 != 0) { |
| 196 | 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."); |
| 197 | } |
| 198 | } |
| 199 | if (p_dict.has("target")) { |
| 200 | const int target = p_dict["target"]; |
| 201 | buffer_view->indices = target == ArrayBufferTarget::TARGET_ELEMENT_ARRAY_BUFFER; |
| 202 | buffer_view->vertex_attributes = target == ArrayBufferTarget::TARGET_ARRAY_BUFFER; |
| 203 | } |
| 204 | return buffer_view; |
| 205 | } |
| 206 | |
| 207 | Dictionary GLTFBufferView::to_dictionary() const { |
| 208 | Dictionary dict; |
| 209 | ERR_FAIL_COND_V_MSG(buffer == -1, dict, "Buffer index must be set to a valid buffer before converting to Dictionary."); |
| 210 | dict["buffer"] = buffer; |
| 211 | dict["byteLength"] = byte_length; |
| 212 | if (byte_offset != 0) { |
| 213 | dict["byteOffset"] = byte_offset; |
| 214 | } |
| 215 | if (byte_stride != -1) { |
| 216 | dict["byteStride"] = byte_stride; |
| 217 | } |
| 218 | if (indices) { |
| 219 | dict["target"] = ArrayBufferTarget::TARGET_ELEMENT_ARRAY_BUFFER; |
| 220 | } else if (vertex_attributes) { |
| 221 | dict["target"] = ArrayBufferTarget::TARGET_ARRAY_BUFFER; |
| 222 | } |
| 223 | return dict; |
| 224 | } |