Path: blob/master/modules/gltf/structures/gltf_buffer_view.cpp
20849 views
/**************************************************************************/1/* gltf_buffer_view.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "gltf_buffer_view.h"31#include "gltf_buffer_view.compat.inc"3233#include "../gltf_state.h"3435void GLTFBufferView::_bind_methods() {36ClassDB::bind_method(D_METHOD("load_buffer_view_data", "state"), &GLTFBufferView::load_buffer_view_data);3738ClassDB::bind_static_method("GLTFBufferView", D_METHOD("from_dictionary", "dictionary"), &GLTFBufferView::from_dictionary);39ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFBufferView::to_dictionary);4041ClassDB::bind_method(D_METHOD("get_buffer"), &GLTFBufferView::get_buffer);42ClassDB::bind_method(D_METHOD("set_buffer", "buffer"), &GLTFBufferView::set_buffer);43ClassDB::bind_method(D_METHOD("get_byte_offset"), &GLTFBufferView::get_byte_offset);44ClassDB::bind_method(D_METHOD("set_byte_offset", "byte_offset"), &GLTFBufferView::set_byte_offset);45ClassDB::bind_method(D_METHOD("get_byte_length"), &GLTFBufferView::get_byte_length);46ClassDB::bind_method(D_METHOD("set_byte_length", "byte_length"), &GLTFBufferView::set_byte_length);47ClassDB::bind_method(D_METHOD("get_byte_stride"), &GLTFBufferView::get_byte_stride);48ClassDB::bind_method(D_METHOD("set_byte_stride", "byte_stride"), &GLTFBufferView::set_byte_stride);49ClassDB::bind_method(D_METHOD("get_indices"), &GLTFBufferView::get_indices);50ClassDB::bind_method(D_METHOD("set_indices", "indices"), &GLTFBufferView::set_indices);51ClassDB::bind_method(D_METHOD("get_vertex_attributes"), &GLTFBufferView::get_vertex_attributes);52ClassDB::bind_method(D_METHOD("set_vertex_attributes", "is_attributes"), &GLTFBufferView::set_vertex_attributes);5354ADD_PROPERTY(PropertyInfo(Variant::INT, "buffer"), "set_buffer", "get_buffer"); // GLTFBufferIndex55ADD_PROPERTY(PropertyInfo(Variant::INT, "byte_offset"), "set_byte_offset", "get_byte_offset"); // int56ADD_PROPERTY(PropertyInfo(Variant::INT, "byte_length"), "set_byte_length", "get_byte_length"); // int57ADD_PROPERTY(PropertyInfo(Variant::INT, "byte_stride"), "set_byte_stride", "get_byte_stride"); // int58ADD_PROPERTY(PropertyInfo(Variant::BOOL, "indices"), "set_indices", "get_indices"); // bool59ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vertex_attributes"), "set_vertex_attributes", "get_vertex_attributes"); // bool60}6162GLTFBufferIndex GLTFBufferView::get_buffer() const {63return buffer;64}6566void GLTFBufferView::set_buffer(GLTFBufferIndex p_buffer) {67buffer = p_buffer;68}6970int64_t GLTFBufferView::get_byte_offset() const {71return byte_offset;72}7374void GLTFBufferView::set_byte_offset(int64_t p_byte_offset) {75byte_offset = p_byte_offset;76}7778int64_t GLTFBufferView::get_byte_length() const {79return byte_length;80}8182void GLTFBufferView::set_byte_length(int64_t p_byte_length) {83byte_length = p_byte_length;84}8586int64_t GLTFBufferView::get_byte_stride() const {87return byte_stride;88}8990void GLTFBufferView::set_byte_stride(int64_t p_byte_stride) {91byte_stride = p_byte_stride;92}9394bool GLTFBufferView::get_indices() const {95return indices;96}9798void GLTFBufferView::set_indices(bool p_indices) {99indices = p_indices;100}101102bool GLTFBufferView::get_vertex_attributes() const {103return vertex_attributes;104}105106void GLTFBufferView::set_vertex_attributes(bool p_attributes) {107vertex_attributes = p_attributes;108}109110Vector<uint8_t> GLTFBufferView::load_buffer_view_data(const Ref<GLTFState> p_gltf_state) const {111ERR_FAIL_COND_V(p_gltf_state.is_null(), Vector<uint8_t>());112const Vector<PackedByteArray> &buffers = p_gltf_state->get_buffers();113ERR_FAIL_INDEX_V(buffer, buffers.size(), Vector<uint8_t>());114const PackedByteArray &buffer_data = buffers[buffer];115const int64_t byte_end = byte_offset + byte_length;116// Note that for buffer views with a byte stride, the parts of this data which get used may117// only be determined in combination with the accessors that reference this buffer view.118return buffer_data.slice(byte_offset, byte_end);119}120121GLTFBufferViewIndex 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) {122ERR_FAIL_COND_V_MSG(p_buffer_index < 0, -1, "Buffer index must be greater than or equal to zero.");123const bool target_is_indices = p_target == ArrayBufferTarget::TARGET_ELEMENT_ARRAY_BUFFER;124const bool target_is_vertex_attributes = p_target == ArrayBufferTarget::TARGET_ARRAY_BUFFER;125if (target_is_vertex_attributes) {126ERR_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.");127}128// Check for duplicate buffer views before adding a new one.129Vector<Ref<GLTFBufferView>> state_buffer_views = p_gltf_state->get_buffer_views();130const int buffer_view_index = state_buffer_views.size();131if (p_deduplicate) {132for (int i = 0; i < buffer_view_index; i++) {133const Ref<GLTFBufferView> &existing_buffer_view = state_buffer_views[i];134if (existing_buffer_view->get_byte_offset() % p_alignment == 0 &&135existing_buffer_view->get_byte_length() == p_input_data.size() &&136existing_buffer_view->get_byte_stride() == p_byte_stride &&137existing_buffer_view->get_indices() == target_is_indices &&138existing_buffer_view->get_vertex_attributes() == target_is_vertex_attributes) {139if (existing_buffer_view->load_buffer_view_data(p_gltf_state) == p_input_data) {140// Duplicate found, return the index of the existing buffer view.141return i;142}143}144}145}146// Write the data into the buffer at the specified index.147Vector<PackedByteArray> state_buffers = p_gltf_state->get_buffers();148if (state_buffers.size() <= p_buffer_index) {149state_buffers.resize(p_buffer_index + 1);150}151PackedByteArray state_buffer = state_buffers[p_buffer_index];152const int64_t input_data_size = p_input_data.size();153// This is used by accessors. The byte offset of an accessor MUST be a multiple of the accessor's component size.154// https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#data-alignment155int64_t byte_offset = state_buffer.size();156int64_t padding_bytes = 0;157if (byte_offset % p_alignment != 0) {158padding_bytes = p_alignment - (byte_offset % p_alignment);159byte_offset += padding_bytes;160}161state_buffer.resize(byte_offset + input_data_size);162if (padding_bytes > 0) {163memset(state_buffer.ptrw() + (byte_offset - padding_bytes), 0, padding_bytes);164}165uint8_t *buffer_ptr = state_buffer.ptrw();166memcpy(buffer_ptr + byte_offset, p_input_data.ptr(), input_data_size);167state_buffers.set(p_buffer_index, state_buffer);168p_gltf_state->set_buffers(state_buffers);169// Create a new GLTFBufferView that references the new buffer.170Ref<GLTFBufferView> buffer_view;171buffer_view.instantiate();172buffer_view->set_buffer(p_buffer_index);173buffer_view->set_byte_offset(byte_offset);174buffer_view->set_byte_length(input_data_size);175buffer_view->set_byte_stride(p_byte_stride);176buffer_view->set_indices(target_is_indices);177buffer_view->set_vertex_attributes(target_is_vertex_attributes);178// Add the new buffer view to the state.179state_buffer_views.append(buffer_view);180p_gltf_state->set_buffer_views(state_buffer_views);181return buffer_view_index;182}183184Ref<GLTFBufferView> GLTFBufferView::from_dictionary(const Dictionary &p_dict) {185// See https://github.com/KhronosGroup/glTF/blob/main/specification/2.0/schema/bufferView.schema.json186Ref<GLTFBufferView> buffer_view;187buffer_view.instantiate();188if (p_dict.has("buffer")) {189buffer_view->set_buffer(p_dict["buffer"]);190}191if (p_dict.has("byteLength")) {192buffer_view->set_byte_length(p_dict["byteLength"]);193}194if (p_dict.has("byteOffset")) {195buffer_view->set_byte_offset(p_dict["byteOffset"]);196}197if (p_dict.has("byteStride")) {198buffer_view->byte_stride = p_dict["byteStride"];199if (buffer_view->byte_stride < 4 || buffer_view->byte_stride > 252 || buffer_view->byte_stride % 4 != 0) {200ERR_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.");201}202}203if (p_dict.has("target")) {204const int target = p_dict["target"];205buffer_view->indices = target == ArrayBufferTarget::TARGET_ELEMENT_ARRAY_BUFFER;206buffer_view->vertex_attributes = target == ArrayBufferTarget::TARGET_ARRAY_BUFFER;207}208return buffer_view;209}210211Dictionary GLTFBufferView::to_dictionary() const {212Dictionary dict;213ERR_FAIL_COND_V_MSG(buffer == -1, dict, "Buffer index must be set to a valid buffer before converting to Dictionary.");214dict["buffer"] = buffer;215dict["byteLength"] = byte_length;216if (byte_offset != 0) {217dict["byteOffset"] = byte_offset;218}219if (byte_stride != -1) {220dict["byteStride"] = byte_stride;221}222if (indices) {223dict["target"] = ArrayBufferTarget::TARGET_ELEMENT_ARRAY_BUFFER;224} else if (vertex_attributes) {225dict["target"] = ArrayBufferTarget::TARGET_ARRAY_BUFFER;226}227return dict;228}229230231