Program Listing for File metadata-types.h

Return to documentation for file (include\playback\metadata-extraction\metadata-types.h)

/******************************************************************************
© Intel Corporation.

This software and the related documents are Intel copyrighted materials,
and your use of them is governed by the express license under which they
were provided to you ("License"). Unless the License provides otherwise,
you may not use, modify, copy, publish, distribute, disclose or transmit
this software or the related documents without Intel's prior written
permission.


 This software and the related documents are provided as is, with no express
or implied warranties, other than those that are expressly stated in the
License.

******************************************************************************/

#pragma once

#include "igpa-config.h"
#include "introspection/shader-interface.h"
#include "reflection/struct-data-manager.h"
#include "api-types/api-types.h"

#include <cstdint>
#include <string>
#include <unordered_map>
#include <vector>
#include <map>
#include <math.h>
#include <limits>

namespace gpa {
namespace playback {

typedef uint64_t ResourceID;

uint32_t const kWholeSize = 0;

enum ResourceType {
    kResourceTypeInvalid = 0,
    kBuffer,
    kTexture,
    kShader,
    kSampler,
};

enum BufferTypeBits {
    kBufferTypeUnknown = 0,
    kDynamicBuffer = 1,
    kIndexBuffer = 1 << 1,
    kIndirectBuffer = 1 << 2,
    kStorageBuffer = 1 << 3,
    kTexelBuffer = 1 << 4,
    kUniformBuffer = 1 << 5,
    kVertexBuffer = 1 << 6,
};
typedef uint32_t BufferTypeFlags;

struct BufferMetadata
{
    BufferTypeFlags type{kBufferTypeUnknown};
    // TODO : We probably want to change this to uint64_t...
    uint32_t length{0};
};

enum IndexType {
    kUnknown,
    kUInt16,
    kUInt32,
};

enum Aspect {
    kColor,
    kDepth,
    kStencil,
    kMetadata,
};

enum TextureType {
    kTexture1D,
    kTexture1DArray,
    kTexture2D,
    kTexture2DArray,
    kTexture3D,
    kTextureCube,
    kTextureCubeArray,
    kTextureTypeInvalid,
};

struct TextureMetadata
{
    TextureType type{kTextureTypeInvalid};
    uint32_t width{0};
    uint32_t height{0};
    uint32_t depth{0};
    uint32_t slices{0};
    uint32_t mips{0};
    uint64_t format{0};
    uint32_t sampleCount{0};
    std::vector<Aspect> aspects;
};

enum ShaderStage {
    kVertex,
    kHull,
    kDomain,
    kGeometry,
    kAmplification,
    kMesh,
    kFragment,
    kCompute,
    kLibrary,
    kShaderStageInvalid,
};

struct ShaderSource
{
    api_types::ShaderLanguage language{
        api_types::GPA_SHADER_LANGUAGE_UNKNOWN};
    std::string contents;
};

struct ShaderMetadata
{
    enum InputRate {
        PerVertex,
        PerInstance
    };

    struct VtxInputDesc
    {
        uint32_t binding;
        uint32_t stride;
        uint32_t inputRate;
    };

    struct VtxAttribDesc
    {
        uint32_t location;
        uint32_t binding;
        uint32_t format;
        uint32_t offset;
    };

    ShaderStage stage{kShaderStageInvalid};
    std::vector<api_types::ShaderLanguage> availableLanguages;
    introspection::ShaderInterface reflectionData;
    std::vector<VtxInputDesc> vertexBufferInformation;
    std::vector<VtxAttribDesc> vertexAttribInformation;
    IndexType indexBufferType;
};

enum Filter {
    kUnknownFilter,
    kFilterNearest,
    kFilterLinear,
    kFilterPoint,
    kFilterAnisotropic,
    kFilterCubicImg,
};

enum SamplerAddressMode {
    kUnknownAddressMode,
    kTextureAddressWrap,
    kTextureAddressMirror,
    kTextureAddressClamp,
    kTextureAddressBorder,
    kTextureAddressMirrorOnce,
};

enum CompareOp {
    kUnknownCompareOp,
    kCompareOpNever,
    kCompareOpLess,
    kCompareOpEqual,
    kCompareOpLessOrEqual,
    kCompareOpGreater,
    kCompareOpNotEqual,
    kCompareOpGreaterOrEqual,
    kCompareOpAlways,
};

enum SamplerFlagBits {
    kSubsampledBit = 0x00000001,
    kSubsampledCoarseReconstructionBit = 0x00000002,
};
typedef int SamplerFlags;

struct BorderColor
{
    enum BorderColorComponentType {
        kFloat,
        kInt,
    };
    union
    {
        float f[4];
        int i[4];
    } values;
    BorderColorComponentType type;

    bool operator==(BorderColor other)
    {
        if (type == kFloat) {
            float error = std::numeric_limits<float>::epsilon();

            return fabs(values.f[0] - other.values.f[0]) < error &&
                   fabs(values.f[1] - other.values.f[1]) < error &&
                   fabs(values.f[2] - other.values.f[2]) < error &&
                   fabs(values.f[3] - other.values.f[3]) < error;
        } else {
            return values.i[0] == other.values.i[0] && values.i[1] == other.values.i[1] && values.i[2] == other.values.i[2] && values.i[3] == other.values.i[3];
        }
    }
    bool operator!=(BorderColor other)
    {
        return !operator==(other);
    }
};

struct SamplerMetadata
{
    SamplerFlags flags;
    Filter magFilter;
    Filter minFilter;
    Filter mipmapMode;
    SamplerAddressMode addressModeU;
    SamplerAddressMode addressModeV;
    SamplerAddressMode addressModeW;
    float mipLodBias;
    float maxAnisotropy;
    bool compareEnable;
    CompareOp compareOp;
    float minLod;
    float maxLod;
    BorderColor borderColor;
    bool unnormalizedCoordinates;
};

struct ResourceMetadata
{
    ResourceType type{kResourceTypeInvalid};
    std::string debugName;
    BufferMetadata buffer{};
    TextureMetadata texture{};
    ShaderMetadata shader{};
    SamplerMetadata sampler{};
};

struct SubresourceRange
{
    std::vector<Aspect> aspects;
    uint32_t baseMipLevel;
    uint32_t levelCount;
    uint32_t baseArrayLayer;
    uint32_t layerCount;
};

struct TextureRange
{
    TextureType type{kTextureTypeInvalid};
    SubresourceRange subresourceRange;
};
using TextureBinding = TextureRange;

struct BufferRange
{
    uint64_t offset;
    uint64_t range;
};
using UniformBufferBinding = BufferRange;
using StorageBufferBinding = BufferRange;
struct BufferDynamicRange
{
    uint32_t dynamicOffset;
    uint64_t offset;
    uint64_t range;
};
using UniformBufferDynamicBinding = BufferDynamicRange;
using StorageBufferDynamicBinding = BufferDynamicRange;
using Format = uint64_t;
struct TexelBufferRange
{
    BufferRange range;
    Format format;
};

struct BufferBinding
{
    BufferTypeFlags type = kBufferTypeUnknown;
    UniformBufferBinding uniform{};
    UniformBufferDynamicBinding uniformDynamic{};
    StorageBufferBinding storage{};
    StorageBufferDynamicBinding storageDynamic{};
    TexelBufferRange uniformTexel{};
    TexelBufferRange storageTexel{};
    // ... to be expanded with additional buffer types in the future
};

struct SamplerBinding
{
    bool immutable;
};

enum PipelineStateObjectType : uint32_t {
    kInvalid = 0,
    kGraphics_FullPipelineStateObject,
    kGraphics_InputLayoutObject,
    kGraphics_RasterizerStateObject,
    kGraphics_DepthStencilStateObject,
    kGraphics_BlendStateObject,
    kGraphics_SamplerStateObject,
    kCompute_FullPipelineStateObject,
    kRaytracing_FullPipelineStateObject
};

enum StageVisibilityBits {
    kStageVisibilityNone = 0,
    kShaderStageVertex = 1,
    kShaderStageTessellationControl = 1 << 1,
    kShaderStageTessellationEvaluation = 1 << 2,
    kShaderStageGeometry = 1 << 3,
    kShaderStageFragment = 1 << 4,
    kShaderStageCompute = 1 << 5,
    kShaderStageAllGraphics = kShaderStageVertex | kShaderStageTessellationControl | kShaderStageTessellationEvaluation | kShaderStageGeometry | kShaderStageFragment,
    kShaderStageAll = 0x7FFFFFFF,
};
typedef uint32_t StageVisibilityFlags;

struct BindingMetadata
{
    ResourceType type;
    TextureBinding texture;
    BufferBinding buffer;
    SamplerBinding sampler;
};

struct ResourceBindings
{
    struct ArgumentResource
    {
        enum ArgumentType {
            kArgType_Unknown = 0,
            kArgType_IndirectBuffer,
            kArgType_IndirectCountBuffer,
            kArgType_SourceResource,
            kArgType_DestinationResource,
            kArgType_BarrierTransitionTarget,
            kArgType_LoadedResource,
            kArgType_Count,
        };

        ResourceID id;
        ArgumentType argumentType;
        uint32_t argumentIndex;
        ResourceType resourceType;
        TextureRange textureRange;
        BufferRange bufferRange;
    };

    struct ResouceBinding
    {
        ResourceID id;
        BindingMetadata metadata;
    };

    struct InputResource
    {
        ResourceID id;
        uint32_t bindIndex;
        uint32_t descriptorSetIndex;
        uint32_t arrayIndex;
        StageVisibilityFlags visibility;
        BindingMetadata metadata;
    };

    struct OutputResource
    {
        ResourceID id;
        BindingMetadata metadata;
    };

    std::vector<InputResource> inputs;
    std::vector<OutputResource> outputs;

    std::vector<ResourceID> indexBuffers;
    std::vector<BufferRange> indexBuffersRanges;
    std::vector<ResourceID> vertexBuffers;
    std::vector<BufferRange> vertexBuffersRanges;

    std::vector<ArgumentResource> argumentResources;

    std::vector<ResourceID> shaders;
};

struct TextureBox
{
    uint32_t x;
    uint32_t y;
    uint32_t z;
    uint32_t width;
    uint32_t height;
    uint32_t depth;
};

struct TextureSlice
{
    Aspect aspect;
    uint32_t mipIndex;
    uint32_t arrayIndex;
};

#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnested-anon-types"
#endif

struct TextureSubresourceDesc
{
    bool isSwapchainImage;
    TextureType type;
    uint32_t rowStride;
    uint32_t sliceStride;
    uint64_t format;
    TextureSlice slice;
    TextureBox box;
};

struct ShaderRequest
{
    ResourceID id;
    api_types::ShaderLanguage language;
};

struct ShaderMetrics
{
    ResourceID shaderId;
    ShaderStage shaderStage;
    std::string shaderName;
    bool isPrimary;

    uint32_t simdWidth;
    uint64_t totalCycles;
    uint64_t invocationsCount;
    uint64_t simdActiveChannels;
    double simdOccupancy;
};

struct ShaderInstruction
{
    uint64_t invocationCount;
    double totalCycles;
    int32_t theoreticalLatency;
    uint32_t sourceLineIndex;
    uint16_t sourceFileIndex;
    std::string disassemblyLine;
};

using ShaderAssembly = std::vector<ShaderInstruction>;

struct ProfiledShader : ShaderMetrics
{
    uint32_t kernelVersion;
    std::string errorMessage;

    ShaderAssembly shaderAssembly;
    std::vector<std::string> sourceFileNames;
    api_types::ShaderLanguage mappingTarget;
};

struct ProfileShaderRequest
{
    std::vector<uint64_t> profiledCalls;
};

using ProfileShaderResult = std::vector<ProfiledShader>;
using ShaderMetricsResult = std::vector<ShaderMetrics>;

struct RequestedResource
{
    ResourceID id;
    ResourceType type;
    union
    {
        TextureSubresourceDesc texture;
        BufferRange buffer;
    };
};

#ifdef __clang__
#pragma clang diagnostic pop
#endif

struct ResourceDataRequest
{
    static const int64_t kRangeBegin = -1;
    int64_t index = 0;
    std::vector<RequestedResource> requestedResources;
};

struct ResultResourceMetadata
{
    ResourceType type{kResourceTypeInvalid};
    std::string debugName;
    BufferMetadata buffer;
    TextureSubresourceDesc texture;
};

struct ResourceDataResult
{
    struct ResourceData
    {
        ResourceID resource;
        std::vector<uint8_t> dataEx;
        ResultResourceMetadata metadataEx;
    };
    std::vector<ResourceData> resourceDataList;
};

struct ResourceBindingsEnumData_ex
{
    std::unordered_map<gpa::playback::ResourceID, gpa::playback::ResourceMetadata>
        metadataPerResource;
    std::vector<gpa::playback::ResourceBindings> bindingsPerIndex;
};

}  // namespace playback
}  // namespace gpa