diff options
author | Jeff McGlynn <jwmcglynn@google.com> | 2018-06-20 11:34:20 -0700 |
---|---|---|
committer | Jeff McGlynn <jwmcglynn@google.com> | 2018-06-20 11:50:22 -0700 |
commit | b56a50064caf2a590ba43699e0074690fcd431bf (patch) | |
tree | d81c98f2d097f9b32f23cffef7afa6725a182e66 /src/decoder/intermediate_astc_block.h | |
parent | ce724a5f1c94f539a6bf956fc6431c37e97961a9 (diff) |
Initial version of astc-codec for open source release
Contains an implementation of an ASTC decoder that is able to pass the
dEQP ASTC LDR tests.
astc-codec has no external dependencies for the main library, only for
test code, and is licensed under the Apache license.
Components:
include/ - Public API that can decode ASTC LDR data into a RGBA UNORM8
buffer.
src/base/ - Base library with common functionality not directly related
to ASTC decoding. Contains a uint128 implementation, BitStream for
reading/writing bits with a primitive (or uint128 type), Optional
implementation (to not take a dependency on C++17), and more.
src/decoder/ - Internal implementation of the ASTC decoder.
src/base/test/, src/decoder/test/ - Unit tests (and a fuzzing test) for
the astc decoder.
src/decoder/testdata/ - Sample ASTC images and golden image results for
testing.
src/decoder/tools/ - A tool to inspect contents of an ASTC file.
third_party/ - Third party libraries, only used for tests.
Change-Id: Ia98e5a7dc847daa3d3a48c5e62d94b8fb1cb98bd
Diffstat (limited to 'src/decoder/intermediate_astc_block.h')
-rw-r--r-- | src/decoder/intermediate_astc_block.h | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/src/decoder/intermediate_astc_block.h b/src/decoder/intermediate_astc_block.h new file mode 100644 index 0000000..ec6eb3e --- /dev/null +++ b/src/decoder/intermediate_astc_block.h @@ -0,0 +1,128 @@ +// Copyright 2018 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef ASTC_CODEC_DECODER_INTERMEDIATE_ASTC_BLOCK_H_ +#define ASTC_CODEC_DECODER_INTERMEDIATE_ASTC_BLOCK_H_ + +#include "src/base/optional.h" +#include "src/base/uint128.h" +#include "src/decoder/physical_astc_block.h" + +#include <array> +#include <vector> + +namespace astc_codec { + +// From Table C.2.7 -- These are the only valid ranges that weight +// values can take. +constexpr std::array<int, 12> kValidWeightRanges = +{{ 1, 2, 3, 4, 5, 7, 9, 11, 15, 19, 23, 31 }}; + +// Void extent data are all the ASTC blocks that are labeled for having a +// constant color. In the ASTC spec, some of these blocks may optionally +// have "void extent coordinates" that describe how far in texture space +// the constant color should span. If these coordinates are not valid, +// then the coordinates are all set to a fully saturated bit mask +// ((1 << 13) - 1) and the block is treated as a singular constant color. +// We call both types of these blocks "void extent" to remove confusion +// in our code. +struct VoidExtentData { + uint16_t r; + uint16_t g; + uint16_t b; + uint16_t a; + std::array<uint16_t, 4> coords; +}; + +// Intermediate endpoint data. Really this is just an endpoint mode +// and a couple of values that represent the data used to decode the +// RGB values from the color endpoint mode. +struct IntermediateEndpointData { + ColorEndpointMode mode; + std::vector<int> colors; +}; + +// This is an unpacked physical ASTC block, but it does not have enough +// information to be worked with logically. It is simply a container of +// all of the unpacked ASTC information. It is used as a staging area +// for the information that is later Pack()'d into a PhysicalASTCBlock. +struct IntermediateBlockData { + int weight_grid_dim_x; + int weight_grid_dim_y; + int weight_range; + + // Quantized, non-interpolated weights + std::vector<int> weights; + + // The 10-bit partition ID if we need one + base::Optional<int> partition_id; + + // The dual-plane channel in [0, 3] if it exists. + base::Optional<int> dual_plane_channel; + + // The quantized/encoded endpoint values for this block. The range of each + // endpoint value is specified by |endpoint_range|, if it exists. If not, the + // range can be queried by calling EndpointRangeForBlock + std::vector<IntermediateEndpointData> endpoints; + + // The range [0, endpoint_range] that any one endpoint value can take. Users + // should not write to this value themselves. If it is empty at the time + // someone calls Pack(), it will be automatically inferred. Otherwise, it is + // set by Unpack() based on what the underlying encoding specified. + base::Optional<int> endpoint_range; +}; + +// Returns the maximum value that a given endpoint value can take according to +// the other settings in the block. Ignores the |endpoint_range| member +// variable. Returns negative values on error: +// -1 : Too many bits required to store weight grid +// -2 : There are too few bits allocated for color endpoint data according to +// C.2.24 in the ASTC spec +int EndpointRangeForBlock(const IntermediateBlockData& data); +inline int EndpointRangeForBlock(const VoidExtentData& data); + +// Unpacks the physical ASTC block into the intermediate block. Returns false +// if the physical block is an error encoded block, or if the physical block +// is a void extent block. On error the contents of ib are undefined. +base::Optional<IntermediateBlockData> UnpackIntermediateBlock( + const PhysicalASTCBlock& pb); + +// Unpacks the physical ASTC block into a void extent block. Returns false +// if the physical block is an error encoded block, or if the physical block +// is an intermediate block. On error the contents of ib are undefined. +base::Optional<VoidExtentData> UnpackVoidExtent(const PhysicalASTCBlock& pb); + +// Packs the given intermediate block into a physical block. Returns an error +// string if the provided values in the intermediate block emit an illegal ASTC +// encoding. In this case the results in the physical block are undefined. +base::Optional<std::string> Pack(const IntermediateBlockData& data, + base::UInt128* pb); + +// Packs the given void extent block into a physical block. Returns an error +// string if the provided values in the void extent block emit an illegal ASTC +// encoding. In this case the results in the physical block are undefined. +base::Optional<std::string> Pack(const VoidExtentData& data, base::UInt128* pb); + +//////////////////////////////////////////////////////////////////////////////// +// +// Impl + +inline int EndpointRangeForBlock(const VoidExtentData& data) { + // Void extent blocks use 16-bit ARGB definitions + return (1 << 16) - 1; +} + +} // namespace astc_codec + +#endif // ASTC_CODEC_DECODER_INTERMEDIATE_ASTC_BLOCK_H_ |