#include "ASW.h" #ifndef ASW_GRAPHICS_H #define ASW_GRAPHICS_H #define CG_CALL(statement) do {statement; CGerror error_enum; const char * error_string = cgGetLastErrorString(&error_enum); \ if (error_enum != CG_NO_ERROR) throw ASW::GraphicsApi::ExceptionCG(__FILE__, __LINE__, __FUNCTION__, NULL, error_string);} while (0) #ifdef ASW_DEBUG #define CG_RUN(statement) CG_CALL(statement) #else #define CG_RUN(statement) statement #endif class GraphicsApi : public Script::Unit { public: enum PropertyType { PROPERTY_UNKNOWN = 0, PROPERTY_TEXTURE_1D = 1, PROPERTY_TEXTURE_2D = 2, PROPERTY_TEXTURING = 3, PROPERTY_LIGHTING = 4, PROPERTY_BLENDING = 5, PROPERTY_TEST_DEPTH = 6, PROPERTY_TEST_STENCIL = 7, PROPERTY_CULL = 8, PROPERTY_TWO_SIDED_STENCIL = 9, PROPERTY_SCISSOR_TEST = 10, PROPERTY_COUNT = 10, }; enum Buffer { BUFFER_DEPTH = 0x01, BUFFER_STENCIL = 0x02, BUFFER_COLOR = 0x04, }; enum BlendType { BLEND_UNKNOWN = 0, BLEND_ZERO = 1, BLEND_ONE = 2, BLEND_SRC_COLOR = 3, BLEND_SRC_COLOR_INV = 4, BLEND_SRC_ALPHA = 5, BLEND_SRC_ALPHA_INV = 6, BLEND_DST_COLOR = 7, BLEND_DST_COLOR_INV = 8, BLEND_DST_ALPHA = 9, BLEND_DST_ALPHA_INV = 10, BLEND_SRC_ALPHA_SAT = 11, BLEND_COUNT = 11 }; enum StencilOp { STENCILOP_UNKNOWN = 0, STENCILOP_KEEP = 1, STENCILOP_ZERO = 2, STENCILOP_REPLACE = 3, STENCILOP_INVERT = 4, STENCILOP_INCREMENT_WRAP = 5, STENCILOP_DECREMENT_WRAP = 6, STENCILOP_INCREMENT_CLAMP = 7, STENCILOP_DECREMENT_CLAMP = 8, STENCILOP_COUNT = 8 }; enum Test { TEST_UNKNOWN = 0, TEST_NEVER = 1, TEST_ALWAYS = 2, TEST_EQUAL = 3, TEST_NOT_EQUAL = 4, TEST_LESSER = 5, TEST_GREATER = 6, TEST_LESSER_EQUAL = 7, TEST_GREATER_EQUAL = 8, TEST_COUNT = 8, }; enum CullType { CULL_UNKNOWN = 0, CULL_NONE = 1, CULL_CW = 2, CULL_CCW = 3, CULL_BOTH = 4, CULL_COUNT = 4, }; enum FillType { FILL_UNKNOWN = 0, FILL_POINT = 1, FILL_WIREFRAME = 2, FILL_SOLID = 3, FILL_COUNT = 3 }; struct Settings { bool m_blnHardwareVertexProcessing; bool m_blnHardwareRasterizer; bool m_blnVsync; }; struct DrawTextInfo { UTIL::DN::Box m_stuBox; ASW::Colorf m_stuColor; ASW::Point2f m_stuSize; ASW::Font * m_ptrFont; char * m_ptrText; ASW::Type::Align m_enmHorizontal; ASW::Type::Align m_enmVertical; }; protected: bool m_blnProperties[ASW::GraphicsApi::PROPERTY_COUNT]; char * m_ptrPrintfBuffer; unsigned int m_uintMaxLights; unsigned int m_uintMaxTextures; unsigned int m_uintMaxIndexValue; unsigned char m_uchrMaxBlendingMatrices; unsigned int m_uintPrintfBufferSize; const ASW::BufferVertex * m_ptrActiveBufferVertex; const ASW::BufferIndex * m_ptrActiveBufferIndex; const ASW::Shader * m_ptrActiveShaderPixel; const ASW::Shader * m_ptrActiveShaderVertex; CGcontext m_stuCgContext; CGprofile m_enmCgProfileVertex; CGprofile m_enmCgProfilePixel; UTIL::Log * m_ptrLogStd; UTIL::Log * m_ptrLogErr; virtual void PrivateDestroy() = 0; private: void Cleanup(); public: struct ExceptionCG : public ASW::Exception { char * m_ptrError; ExceptionCG(const char * File, const unsigned int Line, const char * Function, const char * Detail, const char * Error); ~ExceptionCG(); virtual void Print(UTIL::Output & p_clsOutput); virtual const char * Type(); }; GraphicsApi(); virtual ~GraphicsApi() {Cleanup();} // // Public member functions // const ASW::BufferIndex * BoundBufferIndex() {return m_ptrActiveBufferIndex;} const ASW::BufferVertex * BoundBufferVertex() {return m_ptrActiveBufferVertex;} const ASW::Shader * BoundShaderVertex() {return m_ptrActiveShaderVertex;} const ASW::Shader * BoundShaderPixel() {return m_ptrActiveShaderPixel;} void EnableLight(const unsigned int IndexFirst, const unsigned int IndexLast, const bool Enable = true); void Destroy(); void Unload(ASW::Shader & Shader); // // Pure virtual functions // virtual void Bind(const ASW::Material & Material) = 0; virtual void Bind(const ASW::Texture * Texture, const unsigned int Stage = 0) = 0; virtual void Bind(const ASW::BufferVertex * VertexBuffer) = 0; virtual void Bind(const ASW::BufferIndex * IndexBuffer) = 0; virtual void Bind(const ASW::Shader * Shader, const ASW::Type::Shader Type = ASW::Type::SHADER_UNKNOWN) = 0; virtual void Blend(const BlendType Source, const BlendType Destination) = 0; virtual void BufferClear(const unsigned int Mask) = 0; virtual void BufferEnable(const unsigned int Mask, const bool Enable) = 0; virtual void BufferInfo(const ASW::BufferVertex & Buffer) = 0; virtual bool CheckEnable(const ASW::GraphicsApi::PropertyType Property) = 0; virtual void ClearColor(const unsigned char Red, const unsigned char Green, const unsigned char Blue, const unsigned char Alpha) = 0; virtual void ClearDepth(const float Depth) = 0; virtual void CullMode(const CullType Type) = 0; virtual void Draw(const ASW::Type::Primitive Type, const ASW::BufferVertex::Index & Index) = 0; virtual void Draw(const ASW::Type::Primitive Type, const ASW::BufferIndex::Index & Index) = 0; virtual void DrawIndices(const ASW::Type::Primitive Type, const ASW::CompositeIndex ** Indices, const unsigned int Amount) = 0; virtual void DrawVertices(const ASW::Type::Primitive Type, const ASW::CompositeIndex ** Indices, const unsigned int Amount) = 0; virtual void DrawEnd() = 0; virtual void DrawStart() = 0; virtual void Enable(const PropertyType Property, const bool Enable = true) = 0; virtual void EnableLight(const unsigned int Index, const bool Enable = true) = 0; virtual void FillMode(const FillType Type) = 0; virtual ASW::Colorf GetAmbience() = 0; virtual void GetDeviceInfo(std::string & Device, std::string & Api) = 0; virtual ASW::Point4i GetViewport(ASW::Point2f * DepthRange = NULL) = 0; virtual bool Load(ASW::Font & Font) = 0; virtual bool Load(ASW::Texture & Texture) = 0; virtual void Load(ASW::Buffer & Buffer) = 0; virtual void * Lock(ASW::Buffer & Buffer, const unsigned int Offset = ~0, const unsigned int Size = ~0, const ASW::Type::HintAccess Hint = ASW::Type::ACCESS_READ_WRITE) = 0; virtual bool Load(ASW::Shader & Shader, const char * FileName, const char * EntryPoint); virtual UTIL::D3::Matrix MatrixBuild(const unsigned int Width, const unsigned int Height, const float Near, const float Far, const float Fov) = 0; virtual UTIL::D3::Matrix MatrixGet(const ASW::Type::Matrix Type) = 0; virtual void MatrixSet(const ASW::Type::Matrix Type, const UTIL::D3::Matrix & Matrix) = 0; //virtual void MatrixSet(const unsigned int Width, const unsigned int Height) = 0; //virtual void MatrixSet(const unsigned int Width, const unsigned int Height, const float PlaneNear, const float PlaneFar) = 0; virtual void Print(const DrawTextInfo & DrawInfo) = 0; virtual void Resize(const unsigned int Width, const unsigned int Height) = 0; virtual void Set(ASW::Buffer & Buffer, const void * Data, const unsigned int Offset, const unsigned int Size) = 0; virtual void SetAmbience(const Colorf & AmbientLight) = 0; virtual void SetDepthTest(const ASW::GraphicsApi::Test Test) = 0; virtual void SetLight(const unsigned int Index, const ASW::Instance::Light & Light) = 0; virtual void SetScissorRectangle(const int Left, const int Bottom, const unsigned int Width, const unsigned int Height) = 0; virtual void SetStencilWriteMask(const unsigned int Mask, const bool Front = true) = 0; virtual void SetStencilOp(const ASW::GraphicsApi::StencilOp TestFail, const ASW::GraphicsApi::StencilOp TestPassDepthFail, const ASW::GraphicsApi::StencilOp TestPassDepthPass, const bool Front = true) = 0; virtual void SetStencilTest(const ASW::GraphicsApi::Test Test, const int Reference = 0, const unsigned int Mask = ~0, const bool Front = true) = 0; virtual void SetStencilParams(const unsigned int WriteMask, const ASW::GraphicsApi::Test Test, const ASW::GraphicsApi::StencilOp TestFail, const ASW::GraphicsApi::StencilOp TestPassDepthFail, const ASW::GraphicsApi::StencilOp TestPassDepthPass, const int Reference = 0, const unsigned int Mask = ~0, const bool Front = true) = 0; virtual void SetViewport(const ASW::Point4i & Dimensions, const ASW::Point2f & DepthRange) = 0; virtual ASW::Point2f Size(const ASW::Font & Font, const char * String) = 0; virtual void Unload(ASW::Buffer & Buffer, const bool RetainData = false) = 0; virtual void Unload(ASW::Font & FontRef) = 0; virtual void Unload(ASW::Texture & Texture) = 0; virtual void Unlock(ASW::Buffer & Buffer) = 0; // // Scripting functions // virtual Script::Unit * GetChild(const char * Name) {return NULL;} virtual const char * GetName() const {return "renderer";} virtual void SetProperty(const char * Name, const Script::Variant & Value) {throw ASW::Script::SCRIPT_ERROR_UNKNOWN_PROPERTY;} virtual Script::Variant GetProperty(const char * Name) const {throw ASW::Script::SCRIPT_ERROR_UNKNOWN_PROPERTY; return Script::Variant();} }; #endif