Skip to content

Instantly share code, notes, and snippets.

@zeux
Created February 24, 2014 17:13
Show Gist options
  • Save zeux/9192509 to your computer and use it in GitHub Desktop.
Save zeux/9192509 to your computer and use it in GitHub Desktop.
class Renderbuffer: public Resource
{
public:
Texture::Format getFormat() const { return format; }
unsigned int getWidth() const { return width; }
unsigned int getHeight() const { return height; }
};
class Framebuffer: public Resource
{
public:
virtual void download(void* data, unsigned int size) = 0;
unsigned int getWidth() const { return width; }
unsigned int getHeight() const { return height; }
};
class VertexLayout: public Resource
{
public:
enum Semantic
{
Semantic_Position,
Semantic_Normal,
Semantic_Color,
Semantic_Texture,
Semantic_Count
};
enum Format
{
Format_Float1,
Format_Float2,
Format_Float3,
Format_Float4,
Format_Short2,
Format_Short4,
Format_UByte4,
Format_Color,
Format_Count
};
struct Element
{
unsigned int stream;
unsigned int offset;
Format format;
Semantic semantic;
unsigned int semanticIndex;
Element(unsigned int stream, unsigned int offset, Format format, Semantic semantic, unsigned int semanticIndex = 0);
};
const std::vector<Element>& getElements() const { return elements; }
};
class GeometryBuffer: public Resource
{
public:
enum Usage
{
Usage_Static,
Usage_Dynamic,
Usage_Count
};
enum LockMode
{
Lock_Normal,
Lock_Discard,
Lock_Count
};
virtual void* lock(LockMode mode = Lock_Normal) = 0;
virtual void unlock() = 0;
virtual void upload(unsigned int offset, const void* data, unsigned int size) = 0;
size_t getElementSize() const { return elementSize; }
size_t getElementCount() const { return elementCount; }
};
class VertexBuffer: public GeometryBuffer
{
};
class IndexBuffer: public GeometryBuffer
{
};
class Geometry: public Resource
{
public:
enum Primitive
{
Primitive_Triangles,
Primitive_Lines,
Primitive_Points,
Primitive_TriangleStrip,
Primitive_Count
};
};
class GeometryBatch
{
public:
GeometryBatch(const shared_ptr<Geometry>& geometry, Geometry::Primitive primitive, unsigned int count, unsigned int indexRangeSize);
GeometryBatch(const shared_ptr<Geometry>& geometry, Geometry::Primitive primitive, unsigned int offset, unsigned int count, unsigned int indexRangeBegin, unsigned int indexRangeEnd);
Geometry* getGeometry() const { return geometry.get(); }
Geometry::Primitive getPrimitive() const { return primitive; }
unsigned int getOffset() const { return offset; }
unsigned int getCount() const { return count; }
unsigned int getIndexRangeBegin() const { return indexRangeBegin; }
unsigned int getIndexRangeEnd() const { return indexRangeEnd; }
};
class VertexShader: public Resource
{
};
class FragmentShader: public Resource
{
};
class ShaderProgram: public Resource
{
public:
~ShaderProgram();
virtual int getConstantHandle(const char* name) const = 0;
virtual unsigned int getMaxWorldTransforms() const = 0;
virtual unsigned int getSamplerMask() const = 0;
};
struct ShaderGlobalConstant
{
const char* name;
unsigned int offset;
unsigned int size;
ShaderGlobalConstant(const char* name, unsigned int offset, unsigned int size);
};
class RasterizerState
{
public:
enum CullMode
{
Cull_None,
Cull_Back,
Cull_Front,
Cull_Count
};
RasterizerState(CullMode cullMode, int depthBias = 0);
CullMode getCullMode() const { return cullMode; }
int getDepthBias() const { return depthBias; }
bool operator==(const RasterizerState& other) const;
bool operator!=(const RasterizerState& other) const;
};
class BlendState
{
public:
enum Mode
{
Mode_None,
Mode_Additive,
Mode_Multiplicative,
Mode_AlphaBlend,
Mode_PremultipliedAlphaBlend,
Mode_AlphaOne,
Mode_Count
};
enum ColorMask
{
Color_None = 0,
Color_R = 1 << 0,
Color_G = 1 << 1,
Color_B = 1 << 2,
Color_A = 1 << 3,
Color_All = Color_R | Color_G | Color_B | Color_A
};
BlendState(Mode mode, unsigned int colorMask = Color_All);
Mode getMode() const { return mode; }
unsigned int getColorMask() const { return colorMask; }
bool operator==(const BlendState& other) const;
bool operator!=(const BlendState& other) const;
};
class DepthState
{
public:
enum Function
{
Function_Always,
Function_Less,
Function_LessEqual,
Function_Count
};
enum StencilMode
{
Stencil_None,
Stencil_IsNotZero,
Stencil_UpdateZFail,
Stencil_Count
};
DepthState(Function function, bool write, StencilMode stencilMode = Stencil_None);
Function getFunction() const { return function; }
bool getWrite() const { return write; }
StencilMode getStencilMode() const { return stencilMode; }
bool operator==(const DepthState& other) const;
bool operator!=(const DepthState& other) const;
};
class SamplerState
{
public:
enum Filter
{
Filter_Point,
Filter_Linear,
Filter_Anisotropic,
Filter_Count
};
enum Address
{
Address_Wrap,
Address_Clamp,
Address_Count
};
SamplerState(Filter filter, Address address = Address_Wrap, unsigned int anisotropy = 0);
Filter getFilter() const { return filter; }
Address getAddress() const { return address; }
unsigned int getAnisotropy() const { return anisotropy; }
bool operator==(const SamplerState& other) const;
bool operator!=(const SamplerState& other) const;
};
struct TextureRegion
{
unsigned int x;
unsigned int y;
unsigned int z;
unsigned int width;
unsigned int height;
unsigned int depth;
TextureRegion(unsigned int x, unsigned int y, unsigned int z, unsigned int width, unsigned int height, unsigned int depth);
TextureRegion(unsigned int x, unsigned int y, unsigned int width, unsigned int height);
};
class Texture: public Resource
{
public:
enum Type
{
Type_2D,
Type_3D,
Type_Cube,
Type_Count
};
enum Usage
{
Usage_Static,
Usage_Dynamic,
Usage_Renderbuffer,
Usage_Count
};
enum Format
{
Format_L8,
Format_LA8,
Format_RGB5A1,
Format_RGBA8,
Format_RG16,
Format_RGBA16F,
Format_BC1,
Format_BC2,
Format_BC3,
Format_PVRTC_RGB2,
Format_PVRTC_RGBA2,
Format_PVRTC_RGB4,
Format_PVRTC_RGBA4,
Format_D24S8,
Format_Count
};
struct LockResult
{
void* data;
unsigned int rowPitch;
unsigned int slicePitch;
};
virtual void upload(unsigned int index, unsigned int mip, const TextureRegion& region, const void* data, unsigned int size) = 0;
virtual bool supportsLocking() const = 0;
virtual LockResult lock(unsigned int index, unsigned int mip, const TextureRegion& region) = 0;
virtual void unlock(unsigned int index, unsigned int mip) = 0;
virtual shared_ptr<Renderbuffer> getRenderbuffer(unsigned int index, unsigned int mip) = 0;
Type getType() const { return type; }
Format getFormat() const { return format; }
unsigned int getWidth() const { return width; }
unsigned int getHeight() const { return height; }
unsigned int getDepth() const { return depth; }
unsigned int getMipLevels() const { return mipLevels; }
Usage getUsage() const { return usage; }
};
class DeviceContext
{
public:
enum ClearMask
{
Clear_Color = 1 << 0,
Clear_Depth = 1 << 1,
Clear_Stencil = 1 << 2
};
virtual ~DeviceContext();
virtual void setDefaultAnisotropy(unsigned int value) = 0;
virtual void updateGlobalConstants(const void* data, size_t dataSize) = 0;
virtual void bindFramebuffer(Framebuffer* buffer) = 0;
virtual void clearFramebuffer(unsigned int mask, const float color[4], float depth, unsigned int stencil) = 0;
virtual void copyFramebuffer(Framebuffer* buffer, Texture* texture) = 0;
virtual void bindProgram(ShaderProgram* program) = 0;
virtual void setWorldTransforms4x3(const float* data, size_t matrixCount) = 0;
virtual void setConstant(int handle, const float* data, size_t vectorCount) = 0;
virtual void bindTexture(unsigned int stage, Texture* texture, const SamplerState& state) = 0;
virtual void setRasterizerState(const RasterizerState& state) = 0;
virtual void setBlendState(const BlendState& state) = 0;
virtual void setDepthState(const DepthState& state) = 0;
virtual void draw(Geometry* geometry, Geometry::Primitive primitive, unsigned int offset, unsigned int count, unsigned int indexRangeBegin, unsigned int indexRangeEnd) = 0;
virtual void draw(const GeometryBatch& geometryBatch) = 0;
};
struct DeviceCaps
{
bool supportsFramebuffer;
bool supportsShaders;
bool supportsFFP;
bool supportsStencil;
bool supportsIndex32;
bool supportsTextureDXT;
bool supportsTexturePVR;
bool supportsTextureHalfFloat;
bool supportsTexture3D;
bool supportsTextureNPOT;
unsigned int maxDrawBuffers;
unsigned int maxTextureSize;
bool colorOrderBGR;
bool needsHalfPixelOffset;
bool requiresRenderTargetFlipping;
void dumpToFLog(int channel) const;
};
struct DeviceStats
{
float gpuFrameTime;
};
class Device
{
public:
enum API
{
API_OpenGL,
API_Direct3D9
};
static Device* create(API api, void* windowHandle);
virtual ~Device();
virtual DeviceContext* beginFrame() = 0;
virtual void endFrame() = 0;
virtual Framebuffer* getMainFramebuffer() = 0;
virtual void defineGlobalConstants(size_t dataSize, const std::vector<ShaderGlobalConstant>& constants) = 0;
virtual std::string getShadingLanguage() = 0;
virtual std::string createShaderSource(const std::string& path, const std::string& defines, boost::function<std::string (const std::string&)> fileCallback) = 0;
virtual std::vector<char> createShaderBytecode(const std::string& source, const std::string& target, const std::string& entrypoint) = 0;
virtual shared_ptr<VertexShader> createVertexShader(const std::vector<char>& bytecode) = 0;
virtual shared_ptr<FragmentShader> createFragmentShader(const std::vector<char>& bytecode) = 0;
virtual shared_ptr<ShaderProgram> createShaderProgram(const shared_ptr<VertexShader>& vertexShader, const shared_ptr<FragmentShader>& fragmentShader) = 0;
virtual shared_ptr<ShaderProgram> createShaderProgramFFP() = 0;
virtual shared_ptr<VertexBuffer> createVertexBuffer(size_t elementSize, size_t elementCount, GeometryBuffer::Usage usage) = 0;
virtual shared_ptr<IndexBuffer> createIndexBuffer(size_t elementSize, size_t elementCount, GeometryBuffer::Usage usage) = 0;
virtual shared_ptr<VertexLayout> createVertexLayout(const std::vector<VertexLayout::Element>& elements) = 0;
virtual shared_ptr<Geometry> createGeometry(const shared_ptr<VertexLayout>& layout, const shared_ptr<VertexBuffer>& vertexBuffer, const shared_ptr<IndexBuffer>& indexBuffer, unsigned int baseVertexIndex = 0) = 0;
virtual shared_ptr<Geometry> createGeometry(const shared_ptr<VertexLayout>& layout, const std::vector<shared_ptr<VertexBuffer> >& vertexBuffers, const shared_ptr<IndexBuffer>& indexBuffer, unsigned int baseVertexIndex = 0) = 0;
virtual shared_ptr<Texture> createTexture(Texture::Type type, Texture::Format format, unsigned int width, unsigned int height, unsigned int depth, unsigned int mipLevels, Texture::Usage usage) = 0;
virtual shared_ptr<Renderbuffer> createRenderbuffer(Texture::Format format, unsigned int width, unsigned int height) = 0;
virtual shared_ptr<Framebuffer> createFramebuffer(const shared_ptr<Renderbuffer>& color, const shared_ptr<Renderbuffer>& depth = shared_ptr<Renderbuffer>()) = 0;
virtual shared_ptr<Framebuffer> createFramebuffer(const std::vector<shared_ptr<Renderbuffer> >& color, const shared_ptr<Renderbuffer>& depth = shared_ptr<Renderbuffer>()) = 0;
virtual const DeviceCaps& getCaps() const = 0;
virtual DeviceStats getStatistics() const = 0;
virtual void setFrameDataCallback(const DeviceFrameDataCallback& callback) = 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment