/* * Copyright 2011-2014 Branimir Karadzic. All rights reserved. * License: http://www.opensource.org/licenses/BSD-2-Clause */ #ifndef BGFX_H_HEADER_GUARD #define BGFX_H_HEADER_GUARD #include // uint32_t #include // size_t /// #define BGFX_STATE_RGB_WRITE UINT64_C(0x0000000000000001) #define BGFX_STATE_ALPHA_WRITE UINT64_C(0x0000000000000002) #define BGFX_STATE_DEPTH_WRITE UINT64_C(0x0000000000000004) #define BGFX_STATE_DEPTH_TEST_LESS UINT64_C(0x0000000000000010) #define BGFX_STATE_DEPTH_TEST_LEQUAL UINT64_C(0x0000000000000020) #define BGFX_STATE_DEPTH_TEST_EQUAL UINT64_C(0x0000000000000030) #define BGFX_STATE_DEPTH_TEST_GEQUAL UINT64_C(0x0000000000000040) #define BGFX_STATE_DEPTH_TEST_GREATER UINT64_C(0x0000000000000050) #define BGFX_STATE_DEPTH_TEST_NOTEQUAL UINT64_C(0x0000000000000060) #define BGFX_STATE_DEPTH_TEST_NEVER UINT64_C(0x0000000000000070) #define BGFX_STATE_DEPTH_TEST_ALWAYS UINT64_C(0x0000000000000080) #define BGFX_STATE_DEPTH_TEST_SHIFT 4 #define BGFX_STATE_DEPTH_TEST_MASK UINT64_C(0x00000000000000f0) #define BGFX_STATE_BLEND_ZERO UINT64_C(0x0000000000001000) #define BGFX_STATE_BLEND_ONE UINT64_C(0x0000000000002000) #define BGFX_STATE_BLEND_SRC_COLOR UINT64_C(0x0000000000003000) #define BGFX_STATE_BLEND_INV_SRC_COLOR UINT64_C(0x0000000000004000) #define BGFX_STATE_BLEND_SRC_ALPHA UINT64_C(0x0000000000005000) #define BGFX_STATE_BLEND_INV_SRC_ALPHA UINT64_C(0x0000000000006000) #define BGFX_STATE_BLEND_DST_ALPHA UINT64_C(0x0000000000007000) #define BGFX_STATE_BLEND_INV_DST_ALPHA UINT64_C(0x0000000000008000) #define BGFX_STATE_BLEND_DST_COLOR UINT64_C(0x0000000000009000) #define BGFX_STATE_BLEND_INV_DST_COLOR UINT64_C(0x000000000000a000) #define BGFX_STATE_BLEND_SRC_ALPHA_SAT UINT64_C(0x000000000000b000) #define BGFX_STATE_BLEND_FACTOR UINT64_C(0x000000000000c000) #define BGFX_STATE_BLEND_INV_FACTOR UINT64_C(0x000000000000d000) #define BGFX_STATE_BLEND_SHIFT 12 #define BGFX_STATE_BLEND_MASK UINT64_C(0x000000000ffff000) #define BGFX_STATE_BLEND_EQUATION_SUB UINT64_C(0x0000000010000000) #define BGFX_STATE_BLEND_EQUATION_REVSUB UINT64_C(0x0000000020000000) #define BGFX_STATE_BLEND_EQUATION_MIN UINT64_C(0x0000000030000000) #define BGFX_STATE_BLEND_EQUATION_MAX UINT64_C(0x0000000040000000) #define BGFX_STATE_BLEND_EQUATION_SHIFT 28 #define BGFX_STATE_BLEND_EQUATION_MASK UINT64_C(0x00000003f0000000) #define BGFX_STATE_BLEND_INDEPENDENT UINT64_C(0x0000000400000000) #define BGFX_STATE_CULL_CW UINT64_C(0x0000001000000000) #define BGFX_STATE_CULL_CCW UINT64_C(0x0000002000000000) #define BGFX_STATE_CULL_SHIFT 36 #define BGFX_STATE_CULL_MASK UINT64_C(0x0000003000000000) #define BGFX_STATE_ALPHA_REF_SHIFT 40 #define BGFX_STATE_ALPHA_REF_MASK UINT64_C(0x0000ff0000000000) #define BGFX_STATE_PT_TRISTRIP UINT64_C(0x0001000000000000) #define BGFX_STATE_PT_LINES UINT64_C(0x0002000000000000) #define BGFX_STATE_PT_POINTS UINT64_C(0x0003000000000000) #define BGFX_STATE_PT_SHIFT 48 #define BGFX_STATE_PT_MASK UINT64_C(0x0003000000000000) #define BGFX_STATE_POINT_SIZE_SHIFT 52 #define BGFX_STATE_POINT_SIZE_MASK UINT64_C(0x0ff0000000000000) #define BGFX_STATE_MSAA UINT64_C(0x1000000000000000) #define BGFX_STATE_RESERVED_MASK UINT64_C(0xe000000000000000) #define BGFX_STATE_NONE UINT64_C(0x0000000000000000) #define BGFX_STATE_MASK UINT64_C(0xffffffffffffffff) #define BGFX_STATE_DEFAULT (0 \ | BGFX_STATE_RGB_WRITE \ | BGFX_STATE_ALPHA_WRITE \ | BGFX_STATE_DEPTH_TEST_LESS \ | BGFX_STATE_DEPTH_WRITE \ | BGFX_STATE_CULL_CW \ | BGFX_STATE_MSAA \ ) #define BGFX_STATE_ALPHA_REF(_ref) ( (uint64_t(_ref)<>BGFX_STATE_BLEND_SHIFT) \ | ( uint32_t( (_dst)>>BGFX_STATE_BLEND_SHIFT)<<4) ) \ ) #define BGFX_STATE_BLEND_FUNC_RT_xE(_src, _dst, _equation) (0 \ | BGFX_STATE_BLEND_FUNC_RT_x(_src, _dst) \ | ( uint32_t( (_equation)>>BGFX_STATE_BLEND_EQUATION_SHIFT)<<8) \ ) #define BGFX_STATE_BLEND_FUNC_RT_1(_src, _dst) (BGFX_STATE_BLEND_FUNC_RT_x(_src, _dst)<< 0) #define BGFX_STATE_BLEND_FUNC_RT_2(_src, _dst) (BGFX_STATE_BLEND_FUNC_RT_x(_src, _dst)<<11) #define BGFX_STATE_BLEND_FUNC_RT_3(_src, _dst) (BGFX_STATE_BLEND_FUNC_RT_x(_src, _dst)<<22) #define BGFX_STATE_BLEND_FUNC_RT_1E(_src, _dst, _equation) (BGFX_STATE_BLEND_FUNC_RT_xE(_src, _dst, _equation)<< 0) #define BGFX_STATE_BLEND_FUNC_RT_2E(_src, _dst, _equation) (BGFX_STATE_BLEND_FUNC_RT_xE(_src, _dst, _equation)<<11) #define BGFX_STATE_BLEND_FUNC_RT_3E(_src, _dst, _equation) (BGFX_STATE_BLEND_FUNC_RT_xE(_src, _dst, _equation)<<22) /// #define BGFX_STENCIL_FUNC_REF_SHIFT 0 #define BGFX_STENCIL_FUNC_REF_MASK UINT32_C(0x000000ff) #define BGFX_STENCIL_FUNC_RMASK_SHIFT 8 #define BGFX_STENCIL_FUNC_RMASK_MASK UINT32_C(0x0000ff00) #define BGFX_STENCIL_TEST_LESS UINT32_C(0x00010000) #define BGFX_STENCIL_TEST_LEQUAL UINT32_C(0x00020000) #define BGFX_STENCIL_TEST_EQUAL UINT32_C(0x00030000) #define BGFX_STENCIL_TEST_GEQUAL UINT32_C(0x00040000) #define BGFX_STENCIL_TEST_GREATER UINT32_C(0x00050000) #define BGFX_STENCIL_TEST_NOTEQUAL UINT32_C(0x00060000) #define BGFX_STENCIL_TEST_NEVER UINT32_C(0x00070000) #define BGFX_STENCIL_TEST_ALWAYS UINT32_C(0x00080000) #define BGFX_STENCIL_TEST_SHIFT 16 #define BGFX_STENCIL_TEST_MASK UINT32_C(0x000f0000) #define BGFX_STENCIL_OP_FAIL_S_ZERO UINT32_C(0x00000000) #define BGFX_STENCIL_OP_FAIL_S_KEEP UINT32_C(0x00100000) #define BGFX_STENCIL_OP_FAIL_S_REPLACE UINT32_C(0x00200000) #define BGFX_STENCIL_OP_FAIL_S_INCR UINT32_C(0x00300000) #define BGFX_STENCIL_OP_FAIL_S_INCRSAT UINT32_C(0x00400000) #define BGFX_STENCIL_OP_FAIL_S_DECR UINT32_C(0x00500000) #define BGFX_STENCIL_OP_FAIL_S_DECRSAT UINT32_C(0x00600000) #define BGFX_STENCIL_OP_FAIL_S_INVERT UINT32_C(0x00700000) #define BGFX_STENCIL_OP_FAIL_S_SHIFT 20 #define BGFX_STENCIL_OP_FAIL_S_MASK UINT32_C(0x00f00000) #define BGFX_STENCIL_OP_FAIL_Z_ZERO UINT32_C(0x00000000) #define BGFX_STENCIL_OP_FAIL_Z_KEEP UINT32_C(0x01000000) #define BGFX_STENCIL_OP_FAIL_Z_REPLACE UINT32_C(0x02000000) #define BGFX_STENCIL_OP_FAIL_Z_INCR UINT32_C(0x03000000) #define BGFX_STENCIL_OP_FAIL_Z_INCRSAT UINT32_C(0x04000000) #define BGFX_STENCIL_OP_FAIL_Z_DECR UINT32_C(0x05000000) #define BGFX_STENCIL_OP_FAIL_Z_DECRSAT UINT32_C(0x06000000) #define BGFX_STENCIL_OP_FAIL_Z_INVERT UINT32_C(0x07000000) #define BGFX_STENCIL_OP_FAIL_Z_SHIFT 24 #define BGFX_STENCIL_OP_FAIL_Z_MASK UINT32_C(0x0f000000) #define BGFX_STENCIL_OP_PASS_Z_ZERO UINT32_C(0x00000000) #define BGFX_STENCIL_OP_PASS_Z_KEEP UINT32_C(0x10000000) #define BGFX_STENCIL_OP_PASS_Z_REPLACE UINT32_C(0x20000000) #define BGFX_STENCIL_OP_PASS_Z_INCR UINT32_C(0x30000000) #define BGFX_STENCIL_OP_PASS_Z_INCRSAT UINT32_C(0x40000000) #define BGFX_STENCIL_OP_PASS_Z_DECR UINT32_C(0x50000000) #define BGFX_STENCIL_OP_PASS_Z_DECRSAT UINT32_C(0x60000000) #define BGFX_STENCIL_OP_PASS_Z_INVERT UINT32_C(0x70000000) #define BGFX_STENCIL_OP_PASS_Z_SHIFT 28 #define BGFX_STENCIL_OP_PASS_Z_MASK UINT32_C(0xf0000000) #define BGFX_STENCIL_NONE UINT32_C(0x00000000) #define BGFX_STENCIL_MASK UINT32_C(0xffffffff) #define BGFX_STENCIL_DEFAULT UINT32_C(0x00000000) #define BGFX_STENCIL_FUNC_REF(_ref) ( (uint32_t(_ref)<+x | /// +----------+----------+----------+----------+ /// |+y 1|+y 4|+y 0|+y 5| /// | ^ -x | ^ +z | ^ +x | ^ -z | /// | | | | | | | | | /// | +---->+z | +---->+x | +---->-z | +---->-x | /// +----------+----------+----------+----------+ /// |+z 3| /// | ^ -y | /// | | | /// | +---->+x | /// +----------+ /// /// @param _mip /// @param _x /// @param _y /// @param _width /// @param _height /// @param _mem /// @param _pitch Pitch of input image (bytes). When _pitch is set to /// UINT16_MAX, it will be calculated internally based on _width. /// void updateTextureCube(TextureHandle _handle, uint8_t _side, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const Memory* _mem, uint16_t _pitch = UINT16_MAX); /// Destroy texture. void destroyTexture(TextureHandle _handle); /// Create frame buffer (simple). /// /// @param _width Texture width. /// @param _height Texture height. /// @param _format Texture format. /// @param _textureFlags Texture flags. /// FrameBufferHandle createFrameBuffer(uint16_t _width, uint16_t _height, TextureFormat::Enum _format, uint32_t _textureFlags = BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP); /// Create frame buffer. /// /// @param _num Number of texture attachments. /// @param _handles Texture attachments. /// @param _destroyTextures If true, textures will be destroyed when /// frame buffer is destroyed. /// FrameBufferHandle createFrameBuffer(uint8_t _num, TextureHandle* _handles, bool _destroyTextures = false); /// Destroy frame buffer. void destroyFrameBuffer(FrameBufferHandle _handle); /// Create shader uniform parameter. /// /// @param _name Uniform name in shader. /// @param _type Type of uniform (See: UniformType). /// @param _num Number of elements in array. /// /// Predefined uniforms: /// /// u_viewRect vec4(x, y, width, height) - view rectangle for current /// view. /// /// u_viewTexel vec4(1.0/width, 1.0/height, undef, undef) - inverse /// width and height /// /// u_view mat4 - view matrix /// /// u_viewProj mat4 - concatenated view projection matrix /// /// u_model mat4[BGFX_CONFIG_MAX_BONES] - array of model matrices. /// /// u_modelView mat4 - concatenated model view matrix, only first /// model matrix from array is used. /// /// u_modelViewProj mat4 - concatenated model view projection matrix. /// /// u_alphaRef float - alpha reference value for alpha test. /// UniformHandle createUniform(const char* _name, UniformType::Enum _type, uint16_t _num = 1); /// Destroy shader uniform parameter. void destroyUniform(UniformHandle _handle); /// Set view name. /// /// @param _id View id. /// @param _name View name. /// /// NOTE: /// This is debug only feature. /// void setViewName(uint8_t _id, const char* _name); /// Set view rectangle. Draw primitive outside view will be clipped. /// /// @param _id View id. /// @param _x Position x from the left corner of the window. /// @param _y Position y from the top corner of the window. /// @param _width Width of view port region. /// @param _height Height of view port region. /// void setViewRect(uint8_t _id, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height); /// Set view rectangle for multiple views. /// /// @param _viewMask Bit mask representing affected views. /// @param _x Position x from the left corner of the window. /// @param _y Position y from the top corner of the window. /// @param _width Width of view port region. /// @param _height Height of view port region. /// void setViewRectMask(uint32_t _viewMask, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height); /// Set view scissor. Draw primitive outside view will be clipped. When /// _x, _y, _width and _height are set to 0, scissor will be disabled. /// /// @param _x Position x from the left corner of the window. /// @param _y Position y from the top corner of the window. /// @param _width Width of scissor region. /// @param _height Height of scissor region. /// void setViewScissor(uint8_t _id, uint16_t _x = 0, uint16_t _y = 0, uint16_t _width = 0, uint16_t _height = 0); /// Set view scissor for multiple views. When _x, _y, _width and _height /// are set to 0, scissor will be disabled. /// /// @param _id View id. /// @param _viewMask Bit mask representing affected views. /// @param _x Position x from the left corner of the window. /// @param _y Position y from the top corner of the window. /// @param _width Width of scissor region. /// @param _height Height of scissor region. /// void setViewScissorMask(uint32_t _viewMask, uint16_t _x = 0, uint16_t _y = 0, uint16_t _width = 0, uint16_t _height = 0); /// Set view clear flags. /// /// @param _id View id. /// @param _flags Clear flags. Use BGFX_CLEAR_NONE to remove any clear /// operation. See: BGFX_CLEAR_*. /// @param _rgba Color clear value. /// @param _depth Depth clear value. /// @param _stencil Stencil clear value. /// void setViewClear(uint8_t _id, uint8_t _flags, uint32_t _rgba = 0x000000ff, float _depth = 1.0f, uint8_t _stencil = 0); /// Set view clear flags for multiple views. void setViewClearMask(uint32_t _viewMask, uint8_t _flags, uint32_t _rgba = 0x000000ff, float _depth = 1.0f, uint8_t _stencil = 0); /// Set view into sequential mode. Draw calls will be sorted in the same /// order in which submit calls were called. void setViewSeq(uint8_t _id, bool _enabled); /// Set multiple views into sequential mode. void setViewSeqMask(uint32_t _viewMask, bool _enabled); /// Set view frame buffer. /// /// @param _id View id. /// @param _handle Frame buffer handle. Passing BGFX_INVALID_HANDLE as /// frame buffer handle will draw primitives from this view into /// default back buffer. /// void setViewFrameBuffer(uint8_t _id, FrameBufferHandle _handle); /// Set view frame buffer for multiple views. /// /// @param _viewMask View mask. /// @param _handle Frame buffer handle. Passing BGFX_INVALID_HANDLE as /// frame buffer handle will draw primitives from this view into /// default back buffer. /// void setViewFrameBufferMask(uint32_t _viewMask, FrameBufferHandle _handle); /// Set view view and projection matrices, all draw primitives in this /// view will use these matrices. void setViewTransform(uint8_t _id, const void* _view, const void* _proj, uint8_t _other = 0xff); /// Set view view and projection matrices for multiple views. void setViewTransformMask(uint32_t _viewMask, const void* _view, const void* _proj, uint8_t _other = 0xff); /// Sets debug marker. void setMarker(const char* _marker); /// Set render states for draw primitive. /// /// @param _state State flags. Default state for primitive type is /// triangles. See: BGFX_STATE_DEFAULT. /// /// BGFX_STATE_ALPHA_WRITE - Enable alpha write. /// BGFX_STATE_DEPTH_WRITE - Enable depth write. /// BGFX_STATE_DEPTH_TEST_* - Depth test function. /// BGFX_STATE_BLEND_* - See NOTE 1: BGFX_STATE_BLEND_FUNC. /// BGFX_STATE_BLEND_EQUATION_* - See NOTE 2. /// BGFX_STATE_CULL_* - Backface culling mode. /// BGFX_STATE_RGB_WRITE - Enable RGB write. /// BGFX_STATE_MSAA - Enable MSAA. /// BGFX_STATE_PT_[LINES/POINTS] - Primitive type. /// /// @param _rgba Sets blend factor used by BGFX_STATE_BLEND_FACTOR and /// BGFX_STATE_BLEND_INV_FACTOR blend modes. /// /// NOTE: /// 1. Use BGFX_STATE_ALPHA_REF, BGFX_STATE_POINT_SIZE and /// BGFX_STATE_BLEND_FUNC macros to setup more complex states. /// 2. BGFX_STATE_BLEND_EQUATION_ADD is set when no other blend /// equation is specified. /// void setState(uint64_t _state, uint32_t _rgba = 0); /// Set stencil test state. /// /// @param _fstencil Front stencil state. /// @param _bstencil Back stencil state. If back is set to BGFX_STENCIL_NONE /// _fstencil is applied to both front and back facing primitives. /// void setStencil(uint32_t _fstencil, uint32_t _bstencil = BGFX_STENCIL_NONE); /// Set scissor for draw primitive. For scissor for all primitives in /// view see setViewScissor. /// /// @param _x Position x from the left corner of the window. /// @param _y Position y from the top corner of the window. /// @param _width Width of scissor region. /// @param _height Height of scissor region. /// @returns Scissor cache index. /// uint16_t setScissor(uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height); /// Set scissor from cache for draw primitive. /// /// @param _cache Index in scissor cache. Passing UINT16_MAX unset primitive /// scissor and primitive will use view scissor instead. /// void setScissor(uint16_t _cache = UINT16_MAX); /// Set model matrix for draw primitive. If it is not called model will /// be rendered with identity model matrix. /// /// @param _mtx Pointer to first matrix in array. /// @param _num Number of matrices in array. /// @returns index into matrix cache in case the same model matrix has /// to be used for other draw primitive call. /// uint32_t setTransform(const void* _mtx, uint16_t _num = 1); /// Set model matrix from matrix cache for draw primitive. /// /// @param _cache Index in matrix cache. /// @param _num Number of matrices from cache. /// void setTransform(uint32_t _cache, uint16_t _num = 1); /// Set shader uniform parameter for draw primitive. void setUniform(UniformHandle _handle, const void* _value, uint16_t _num = 1); /// Set index buffer for draw primitive. void setIndexBuffer(IndexBufferHandle _handle, uint32_t _firstIndex = 0, uint32_t _numIndices = UINT32_MAX); /// Set index buffer for draw primitive. void setIndexBuffer(DynamicIndexBufferHandle _handle, uint32_t _firstIndex = 0, uint32_t _numIndices = UINT32_MAX); /// Set index buffer for draw primitive. void setIndexBuffer(const TransientIndexBuffer* _tib); /// Set index buffer for draw primitive. void setIndexBuffer(const TransientIndexBuffer* _tib, uint32_t _firstIndex, uint32_t _numIndices); /// Set vertex buffer for draw primitive. void setVertexBuffer(VertexBufferHandle _handle); /// Set vertex buffer for draw primitive. void setVertexBuffer(VertexBufferHandle _handle, uint32_t _startVertex, uint32_t _numVertices); /// Set vertex buffer for draw primitive. void setVertexBuffer(DynamicVertexBufferHandle _handle, uint32_t _numVertices = UINT32_MAX); /// Set vertex buffer for draw primitive. void setVertexBuffer(const TransientVertexBuffer* _tvb); /// Set vertex buffer for draw primitive. void setVertexBuffer(const TransientVertexBuffer* _tvb, uint32_t _startVertex, uint32_t _numVertices); /// Set instance data buffer for draw primitive. void setInstanceDataBuffer(const InstanceDataBuffer* _idb, uint16_t _num = UINT16_MAX); /// Set program for draw primitive. void setProgram(ProgramHandle _handle); /// Set texture stage for draw primitive. /// /// @param _stage Texture unit. /// @param _sampler Program sampler. /// @param _handle Texture handle. /// @param _flags Texture sampling mode. Default value UINT32_MAX uses /// texture sampling settings from the texture. /// /// BGFX_TEXTURE_[U/V/W]_[MIRROR/CLAMP] - Mirror or clamp to edge wrap /// mode. /// /// BGFX_TEXTURE_[MIN/MAG/MIP]_[POINT/ANISOTROPIC] - Point or anisotropic /// sampling. /// /// @param _flags Texture sampler filtering flags. UINT32_MAX use the /// sampler filtering mode set by texture. /// void setTexture(uint8_t _stage, UniformHandle _sampler, TextureHandle _handle, uint32_t _flags = UINT32_MAX); /// Set texture stage for draw primitive. /// /// @param _stage Texture unit. /// @param _sampler Program sampler. /// @param _handle Frame buffer handle. /// @param _attachment Attachment index. /// @param _flags Texture sampling mode. Default value UINT32_MAX uses /// texture sampling settings from the texture. /// /// BGFX_TEXTURE_[U/V/W]_[MIRROR/CLAMP] - Mirror or clamp to edge wrap /// mode. /// /// BGFX_TEXTURE_[MIN/MAG/MIP]_[POINT/ANISOTROPIC] - Point or anisotropic /// sampling. /// void setTexture(uint8_t _stage, UniformHandle _sampler, FrameBufferHandle _handle, uint8_t _attachment = 0, uint32_t _flags = UINT32_MAX); /// Submit primitive for rendering into single view. /// /// @param _id View id. /// @param _depth Depth for sorting. /// @returns Number of draw calls. /// uint32_t submit(uint8_t _id, int32_t _depth = 0); /// Submit primitive for rendering into multiple views. /// /// @param _viewMask Mask to which views to submit draw primitive calls. /// @param _depth Depth for sorting. /// @returns Number of draw calls. /// uint32_t submitMask(uint32_t _viewMask, int32_t _depth = 0); /// Discard all previously set state for draw call. void discard(); /// Request screen shot. /// /// @param _filePath Will be passed to CallbackI::screenShot callback. /// /// NOTE: /// CallbackI::screenShot must be implemented. /// void saveScreenShot(const char* _filePath); } // namespace bgfx #endif // BGFX_H_HEADER_GUARD