Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DecodeFromTextBuffer #1021

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/draco/attributes/geometry_attribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ namespace draco {
class GeometryAttribute {
public:
// Supported attribute types.
enum Type {
INVALID = -1,
enum Type :uint8_t{
INVALID = 0xFF,
// Named attributes start here. The difference between named and generic
// attributes is that for named attributes we know their purpose and we
// can apply some special methods when dealing with them (e.g. during
Expand Down
8 changes: 7 additions & 1 deletion src/draco/compression/entropy/symbol_encoding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ static void ComputeBitLengths(const uint32_t *symbols, int num_values,
for (int j = 1; j < num_components; ++j) {
if (max_component_value < symbols[i + j]) {
max_component_value = symbols[i + j];
if (max_component_value == 0xFFFFFFFF) {
throw std::runtime_error("Failure in ComputeBitLengths.");
}
}
}
int value_msb_pos = 0;
Expand Down Expand Up @@ -128,7 +131,10 @@ bool EncodeSymbols(const uint32_t *symbols, int num_values, int num_components,
uint32_t max_value;
ComputeBitLengths(symbols, num_values, num_components, &bit_lengths,
&max_value);

if(max_value==0xFFFFFFFF)
{
throw std::runtime_error("Failure in ComputeBitLengths.");
}
// Approximate number of bits needed for storing the symbols using the tagged
// scheme.
const int64_t tagged_scheme_total_bits =
Expand Down
6 changes: 5 additions & 1 deletion src/draco/core/bit_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
// limitations under the License.
//
#include "draco/core/bit_utils.h"

#include <stdexcept>

namespace draco {

void ConvertSignedIntsToSymbols(const int32_t *in, int in_values,
Expand All @@ -23,6 +24,9 @@ void ConvertSignedIntsToSymbols(const int32_t *in, int in_values,
// Put the sign bit into LSB pos and shift the rest one bit left.
for (int i = 0; i < in_values; ++i) {
out[i] = ConvertSignedIntToSymbol(in[i]);
if (out[i] == 0xFFFFFFFF) {
throw std::runtime_error("Failure in ConvertSignedIntsToSymbols.");
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/draco/core/bit_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ inline int MostSignificantBit(uint32_t n) {
// symbols that can be encoded using an entropy encoder.
void ConvertSignedIntsToSymbols(const int32_t *in, int in_values,
uint32_t *out);

#pragma optimize("",off)
// Converts unsigned integer symbols encoded with an entropy encoder back to
// signed values.
void ConvertSymbolsToSignedInts(const uint32_t *in, int in_values,
Expand Down
30 changes: 30 additions & 0 deletions src/draco/io/gltf_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,12 @@ StatusOr<std::unique_ptr<Mesh>> GltfDecoder::DecodeFromBuffer(
return BuildMesh();
}

StatusOr<std::unique_ptr<Mesh>> GltfDecoder::DecodeFromTextBuffer(
DecoderBuffer *buffer) {
DRACO_RETURN_IF_ERROR(LoadTextBuffer(*buffer));
return BuildMesh();
}

StatusOr<std::unique_ptr<Scene>> GltfDecoder::DecodeFromFileToScene(
const std::string &file_name) {
return DecodeFromFileToScene(file_name, nullptr);
Expand All @@ -485,6 +491,14 @@ StatusOr<std::unique_ptr<Scene>> GltfDecoder::DecodeFromBufferToScene(
return std::move(scene_);
}

StatusOr<std::unique_ptr<Scene>> GltfDecoder::DecodeFromTextBufferToScene(
DecoderBuffer *buffer) {
DRACO_RETURN_IF_ERROR(LoadTextBuffer(*buffer));
scene_ = std::unique_ptr<Scene>(new Scene());
DRACO_RETURN_IF_ERROR(DecodeGltfToScene());
return std::move(scene_);
}

Status GltfDecoder::LoadFile(const std::string &file_name,
std::vector<std::string> *input_files) {
const std::string extension = LowercaseFileExtension(file_name);
Expand Down Expand Up @@ -536,6 +550,22 @@ Status GltfDecoder::LoadBuffer(const DecoderBuffer &buffer) {
return OkStatus();
}

Status GltfDecoder::LoadTextBuffer(const DecoderBuffer &buffer) {
tinygltf::TinyGLTF loader;
std::string err;
std::string warn;
if (!loader.LoadASCIIFromString(
&gltf_model_, &err, &warn,
reinterpret_cast<const char *>(buffer.data_head()),
buffer.remaining_size(),"")) {
return Status(Status::DRACO_ERROR,
"TinyGLTF failed to load gltf buffer: " + err);
}
DRACO_RETURN_IF_ERROR(CheckUnsupportedFeatures());
input_file_name_.clear();
return OkStatus();
}

StatusOr<std::unique_ptr<Mesh>> GltfDecoder::BuildMesh() {
DRACO_RETURN_IF_ERROR(GatherAttributeAndMaterialStats());
if (total_face_indices_count_ > 0 && total_point_indices_count_ > 0) {
Expand Down
6 changes: 6 additions & 0 deletions src/draco/io/gltf_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class GltfDecoder {
StatusOr<std::unique_ptr<Mesh>> DecodeFromFile(
const std::string &file_name, std::vector<std::string> *mesh_files);
StatusOr<std::unique_ptr<Mesh>> DecodeFromBuffer(DecoderBuffer *buffer);
StatusOr<std::unique_ptr<Mesh>> DecodeFromTextBuffer(DecoderBuffer *buffer);

// Decodes a glTF file stored in the input |file_name| or |buffer| to a Scene.
// The second form returns a vector of files used as input to the scene during
Expand All @@ -59,6 +60,8 @@ class GltfDecoder {
const std::string &file_name, std::vector<std::string> *scene_files);
StatusOr<std::unique_ptr<Scene>> DecodeFromBufferToScene(
DecoderBuffer *buffer);
StatusOr<std::unique_ptr<Scene>> DecodeFromTextBufferToScene(
DecoderBuffer *buffer);

// Scene graph can be loaded either as a tree or a general directed acyclic
// graph (DAG) that allows multiple parent nodes. By default. we decode the
Expand Down Expand Up @@ -92,6 +95,9 @@ class GltfDecoder {
// Loads |gltf_model_| from |buffer| in GLB format.
Status LoadBuffer(const DecoderBuffer &buffer);

// Loads |gltf_model_| from |buffer| in GLTF format.
Status LoadTextBuffer(const DecoderBuffer &buffer);

// Builds mesh from |gltf_model_|.
StatusOr<std::unique_ptr<Mesh>> BuildMesh();

Expand Down
2 changes: 1 addition & 1 deletion third_party/tinygltf
Submodule tinygltf updated 46 files
+13 −0 .github/FUNDING.yml
+1 −0 .github/ISSUE_TEMPLATE/config.yml
+30 −0 .github/ISSUE_TEMPLATE/issue-report.md
+167 −0 .github/workflows/c-cpp.yml
+72 −0 .github/workflows/codeql-analysis.yml
+7 −0 .gitignore
+57 −16 CMakeLists.txt
+63 −6 README.md
+0 −15 cmake/TinyGLTFConfig.cmake
+3 −0 cmake/TinyGLTFConfig.cmake.in
+1 −0 examples/basic/.gitignore
+4 −0 examples/basic/README.md
+5 −5 examples/basic/basic/basic.vcxproj
+86 −56 examples/basic/main.cpp
+2 −0 examples/build-gltf/Makefile
+121 −0 examples/build-gltf/create_triangle_gltf.cpp
+36 −0 examples/dxview/CMakeLists.txt
+37 −0 examples/dxview/README.md
+3 −0 examples/dxview/res/gray.hlsl
+52 −0 examples/dxview/res/lighting.hlsl
+46 −0 examples/dxview/res/primitive.hlsl
+1,292 −0 examples/dxview/src/Viewer.cc
+130 −0 examples/dxview/src/Viewer.h
+90 −0 examples/dxview/src/dxview.cc
+3 −3 examples/glview/CMakeLists.txt
+36 −8 examples/glview/glview.cc
+17,300 −10,953 json.hpp
+1 −1 loader_example.cc
+ models/CubeImageUriSpaces/ 2x2 image has multiple spaces.png
+ models/CubeImageUriSpaces/2x2 image has spaces.png
+171 −0 models/CubeImageUriSpaces/CubeImageUriMultipleSpaces.gltf
+ models/CubeImageUriSpaces/CubeImageUriSpaces.bin
+171 −0 models/CubeImageUriSpaces/CubeImageUriSpaces.gltf
+ models/Extensions-overwrite-issue261/issue-261.bin
+376 −0 models/Extensions-overwrite-issue261/issue-261.gltf
+ models/SparseMorphTargets-issue280/singleBlendshapeCube_sparse.glb
+267 −0 models/regression/unassigned-skeleton.gltf
+0 −2 test_runner.py
+46 −0 tests/fuzzer/README.md
+33 −0 tests/fuzzer/fuzz_gltf.cc
+9 −0 tests/fuzzer/meson.build
+385 −0 tests/tester.cc
+4 −0 tiny_gltf.cc
+998 −374 tiny_gltf.h
+11 −0 wasm/Makefile
+31 −0 wasm/README.md