boo/include/boo/graphicsdev/IGraphicsDataFactory.hpp

291 lines
11 KiB
C++
Raw Normal View History

2018-10-06 20:36:44 -07:00
#pragma once
#include <cstddef>
2017-12-28 23:54:26 -08:00
#include <cstdint>
#include <functional>
#include <initializer_list>
2018-10-15 20:13:57 -07:00
#include <vector>
#include "boo/BooObject.hpp"
#include "boo/System.hpp"
2015-12-18 13:59:54 -08:00
2018-10-06 19:49:22 -07:00
#ifdef __SWITCH__
#include <ctype.h>
#endif
2018-12-07 21:17:51 -08:00
namespace boo {
struct IGraphicsCommandQueue;
/** Supported buffer uses */
2018-12-07 21:17:51 -08:00
enum class BufferUse { Null, Vertex, Index, Uniform };
/** Typeless graphics buffer */
2018-12-07 21:17:51 -08:00
struct IGraphicsBuffer : IObj {
bool dynamic() const { return m_dynamic; }
protected:
2018-12-07 21:17:51 -08:00
bool m_dynamic;
explicit IGraphicsBuffer(bool dynamic) : m_dynamic(dynamic) {}
};
/** Static resource buffer for verts, indices, uniform constants */
2018-12-07 21:17:51 -08:00
struct IGraphicsBufferS : IGraphicsBuffer {
protected:
2018-12-07 21:17:51 -08:00
IGraphicsBufferS() : IGraphicsBuffer(false) {}
};
/** Dynamic resource buffer for verts, indices, uniform constants */
2018-12-07 21:17:51 -08:00
struct IGraphicsBufferD : IGraphicsBuffer {
virtual void load(const void* data, size_t sz) = 0;
virtual void* map(size_t sz) = 0;
virtual void unmap() = 0;
protected:
2018-12-07 21:17:51 -08:00
IGraphicsBufferD() : IGraphicsBuffer(true) {}
2015-10-29 23:26:02 -07:00
};
/** Texture access types */
enum class TextureType { Static, StaticArray, Dynamic, Render, CubeRender };
2015-11-03 16:27:32 -08:00
/** Supported texture formats */
enum class TextureFormat { RGBA8, I8, I16, DXT1, DXT3, DXT5, BPTC };
/** Supported texture clamp modes */
2018-12-07 21:17:51 -08:00
enum class TextureClampMode { Invalid = -1, Repeat, ClampToWhite, ClampToBlack, ClampToEdge, ClampToEdgeNearest };
/** Typeless texture */
2018-12-07 21:17:51 -08:00
struct ITexture : IObj {
TextureType type() const { return m_type; }
2018-12-07 21:17:51 -08:00
/* Only applies on GL and Vulkan. Use shader semantics on other platforms */
virtual void setClampMode(TextureClampMode mode) {}
protected:
2018-12-07 21:17:51 -08:00
TextureType m_type;
explicit ITexture(TextureType type) : m_type(type) {}
};
/** Static resource buffer for textures */
2018-12-07 21:17:51 -08:00
struct ITextureS : ITexture {
protected:
2018-12-07 21:17:51 -08:00
ITextureS() : ITexture(TextureType::Static) {}
};
2015-11-26 15:03:01 -08:00
/** Static-array resource buffer for array textures */
2018-12-07 21:17:51 -08:00
struct ITextureSA : ITexture {
2015-11-26 15:03:01 -08:00
protected:
2018-12-07 21:17:51 -08:00
ITextureSA() : ITexture(TextureType::StaticArray) {}
2015-11-26 15:03:01 -08:00
};
/** Dynamic resource buffer for textures */
2018-12-07 21:17:51 -08:00
struct ITextureD : ITexture {
virtual void load(const void* data, size_t sz) = 0;
virtual void* map(size_t sz) = 0;
virtual void unmap() = 0;
protected:
2018-12-07 21:17:51 -08:00
ITextureD() : ITexture(TextureType::Dynamic) {}
2015-11-03 16:27:32 -08:00
};
/** Resource buffer for render-target textures */
2018-12-07 21:17:51 -08:00
struct ITextureR : ITexture {
2015-11-03 16:27:32 -08:00
protected:
2018-12-07 21:17:51 -08:00
ITextureR() : ITexture(TextureType::Render) {}
2017-09-30 21:23:28 -07:00
};
/** Resource buffer for cube render-target textures */
struct ITextureCubeR : ITexture {
protected:
ITextureCubeR() : ITexture(TextureType::CubeRender) {}
};
2015-10-29 23:26:02 -07:00
/** Types of vertex attributes */
2018-12-07 21:17:51 -08:00
enum class VertexSemantic {
None = 0,
Position3,
Position4,
Normal3,
Normal4,
Color,
ColorUNorm,
UV2,
UV4,
Weight,
ModelView,
Position2,
2018-12-07 21:17:51 -08:00
SemanticMask = 0xf,
Instanced = 0x10
2015-10-29 23:26:02 -07:00
};
ENABLE_BITWISE_ENUM(VertexSemantic)
2015-10-29 23:26:02 -07:00
/** Used to create IVertexFormat */
2018-12-07 21:17:51 -08:00
struct VertexElementDescriptor {
VertexSemantic semantic{};
2018-12-07 21:17:51 -08:00
int semanticIdx = 0;
VertexElementDescriptor() = default;
VertexElementDescriptor(VertexSemantic s, int idx = 0) : semantic(s), semanticIdx(idx) {}
2015-10-29 23:26:02 -07:00
};
2018-10-06 19:49:22 -07:00
/** Structure for passing vertex format info for pipeline construction */
2018-12-07 21:17:51 -08:00
struct VertexFormatInfo {
size_t elementCount = 0;
const VertexElementDescriptor* elements = nullptr;
2018-10-06 19:49:22 -07:00
2018-12-07 21:17:51 -08:00
VertexFormatInfo() = default;
2018-10-06 19:49:22 -07:00
2018-12-07 21:17:51 -08:00
VertexFormatInfo(size_t sz, const VertexElementDescriptor* elem) : elementCount(sz), elements(elem) {}
2018-10-06 19:49:22 -07:00
2018-12-07 21:17:51 -08:00
template <typename T>
VertexFormatInfo(const T& tp) : elementCount(std::extent_v<T>), elements(tp) {}
2018-10-06 19:49:22 -07:00
2019-01-02 19:46:48 -08:00
VertexFormatInfo(std::initializer_list<VertexElementDescriptor> l)
2019-06-20 15:43:27 -07:00
: elementCount(l.size()), elements(std::move(l.begin())) {}
2018-10-06 19:49:22 -07:00
};
/** Opaque token for referencing a shader stage usable in a graphics pipeline */
struct IShaderStage : IObj {};
/** Opaque token for referencing a complete graphics pipeline state necessary
* to rasterize geometry (shaders and blending modes mainly) */
struct IShaderPipeline : IObj {
virtual bool isReady() const = 0;
};
/** Opaque token serving as indirection table for shader resources
* and IShaderPipeline reference. Each renderable surface-material holds one
* as a reference */
struct IShaderDataBinding : IObj {};
2016-04-03 23:13:11 -07:00
/** Used wherever distinction of pipeline stages is needed */
2018-12-07 21:17:51 -08:00
enum class PipelineStage { Null, Vertex, Fragment, Geometry, Control, Evaluation };
2016-04-03 23:13:11 -07:00
/** Used by platform shader pipeline constructors */
2018-12-07 21:17:51 -08:00
enum class Primitive { Triangles, TriStrips, Patches };
2017-03-10 12:38:00 -08:00
/** Used by platform shader pipeline constructors */
2018-12-07 21:17:51 -08:00
enum class CullMode { None, Backface, Frontface };
2017-03-10 12:38:00 -08:00
/** Used by platform shader pipeline constructors */
2018-12-07 21:17:51 -08:00
enum class ZTest {
None,
LEqual, /* Flipped on Vulkan, D3D, Metal */
Greater,
GEqual,
Equal
};
2015-10-29 23:26:02 -07:00
/** Used by platform shader pipeline constructors */
2018-12-07 21:17:51 -08:00
enum class BlendFactor {
Zero,
One,
SrcColor,
InvSrcColor,
DstColor,
InvDstColor,
SrcAlpha,
InvSrcAlpha,
DstAlpha,
InvDstAlpha,
SrcColor1,
InvSrcColor1,
/* Special value that activates DstColor - SrcColor blending */
Subtract
2015-10-29 23:26:02 -07:00
};
2018-10-06 19:49:22 -07:00
/** Structure for passing additional pipeline construction information */
2018-12-07 21:17:51 -08:00
struct AdditionalPipelineInfo {
BlendFactor srcFac = BlendFactor::One;
BlendFactor dstFac = BlendFactor::Zero;
Primitive prim = Primitive::TriStrips;
ZTest depthTest = ZTest::LEqual;
bool depthWrite = true;
bool colorWrite = true;
bool alphaWrite = false;
CullMode culling = CullMode::Backface;
uint32_t patchSize = 0;
2019-02-26 20:35:21 -08:00
bool overwriteAlpha = false;
2018-12-07 21:17:51 -08:00
bool depthAttachment = true;
2018-10-06 19:49:22 -07:00
};
/** Factory object for creating batches of resources as an IGraphicsData token */
2018-12-07 21:17:51 -08:00
struct IGraphicsDataFactory {
virtual ~IGraphicsDataFactory() = default;
enum class Platform { Null, OpenGL, D3D11, Metal, Vulkan, GX, NX };
virtual Platform platform() const = 0;
virtual const char* platformName() const = 0;
2018-12-07 21:17:51 -08:00
struct Context {
virtual Platform platform() const = 0;
virtual const char* platformName() const = 0;
2018-12-07 21:17:51 -08:00
virtual ObjToken<IGraphicsBufferS> newStaticBuffer(BufferUse use, const void* data, size_t stride,
size_t count) = 0;
virtual ObjToken<IGraphicsBufferD> newDynamicBuffer(BufferUse use, size_t stride, size_t count) = 0;
virtual ObjToken<ITextureS> newStaticTexture(size_t width, size_t height, size_t mips, TextureFormat fmt,
TextureClampMode clampMode, const void* data, size_t sz) = 0;
virtual ObjToken<ITextureSA> newStaticArrayTexture(size_t width, size_t height, size_t layers, size_t mips,
TextureFormat fmt, TextureClampMode clampMode, const void* data,
size_t sz) = 0;
virtual ObjToken<ITextureD> newDynamicTexture(size_t width, size_t height, TextureFormat fmt,
TextureClampMode clampMode) = 0;
virtual ObjToken<ITextureR> newRenderTexture(size_t width, size_t height, TextureClampMode clampMode,
size_t colorBindingCount, size_t depthBindingCount) = 0;
virtual ObjToken<ITextureCubeR> newCubeRenderTexture(size_t width, size_t mips) = 0;
2018-12-07 21:17:51 -08:00
virtual ObjToken<IShaderStage> newShaderStage(const uint8_t* data, size_t size, PipelineStage stage) = 0;
ObjToken<IShaderStage> newShaderStage(const std::vector<uint8_t>& data, PipelineStage stage) {
return newShaderStage(data.data(), data.size(), stage);
}
virtual ObjToken<IShaderPipeline> newShaderPipeline(ObjToken<IShaderStage> vertex, ObjToken<IShaderStage> fragment,
ObjToken<IShaderStage> geometry, ObjToken<IShaderStage> control,
ObjToken<IShaderStage> evaluation,
const VertexFormatInfo& vtxFmt,
const AdditionalPipelineInfo& additionalInfo,
bool asynchronous = true) = 0;
2018-12-07 21:17:51 -08:00
ObjToken<IShaderPipeline> newShaderPipeline(ObjToken<IShaderStage> vertex, ObjToken<IShaderStage> fragment,
const VertexFormatInfo& vtxFmt,
const AdditionalPipelineInfo& additionalInfo,
bool asynchronous = true) {
return newShaderPipeline(std::move(vertex), std::move(fragment), {}, {}, {}, vtxFmt, additionalInfo,
asynchronous);
2018-12-07 21:17:51 -08:00
}
virtual ObjToken<IShaderDataBinding> newShaderDataBinding(
const ObjToken<IShaderPipeline>& pipeline, const ObjToken<IGraphicsBuffer>& vbo,
const ObjToken<IGraphicsBuffer>& instVbo, const ObjToken<IGraphicsBuffer>& ibo, size_t ubufCount,
const ObjToken<IGraphicsBuffer>* ubufs, const PipelineStage* ubufStages, const size_t* ubufOffs,
const size_t* ubufSizes, size_t texCount, const ObjToken<ITexture>* texs, const int* texBindIdx,
const bool* depthBind, size_t baseVert = 0, size_t baseInst = 0) = 0;
ObjToken<IShaderDataBinding> newShaderDataBinding(const ObjToken<IShaderPipeline>& pipeline,
const ObjToken<IGraphicsBuffer>& vbo,
const ObjToken<IGraphicsBuffer>& instVbo,
const ObjToken<IGraphicsBuffer>& ibo, size_t ubufCount,
const ObjToken<IGraphicsBuffer>* ubufs,
const PipelineStage* ubufStages, size_t texCount,
const ObjToken<ITexture>* texs, const int* texBindIdx,
const bool* depthBind, size_t baseVert = 0, size_t baseInst = 0) {
return newShaderDataBinding(pipeline, vbo, instVbo, ibo, ubufCount, ubufs, ubufStages, nullptr, nullptr, texCount,
texs, texBindIdx, depthBind, baseVert, baseInst);
}
};
virtual void commitTransaction(const std::function<bool(Context& ctx)>& __BooTraceArgs) = 0;
virtual ObjToken<IGraphicsBufferD> newPoolBuffer(BufferUse use, size_t stride, size_t count __BooTraceArgs) = 0;
virtual void setDisplayGamma(float gamma) = 0;
virtual bool isTessellationSupported(uint32_t& maxPatchSizeOut) = 0;
virtual void waitUntilShadersReady() = 0;
2019-06-20 23:01:27 -07:00
virtual bool areShadersReady() = 0;
};
using GraphicsDataFactoryContext = IGraphicsDataFactory::Context;
using FactoryCommitFunc = std::function<bool(GraphicsDataFactoryContext& ctx)>;
2016-12-09 18:31:50 -08:00
2018-12-07 21:17:51 -08:00
} // namespace boo