axmol/cocos/renderer/CCRenderState.h

262 lines
8.0 KiB
C
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2014 GamePlay3D team
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
http://www.cocos2d-x.org
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Ideas taken from:
- GamePlay3D: http://gameplay3d.org/
- OGRE3D: http://www.ogre3d.org/
- Qt3D: http://qt-project.org/
****************************************************************************/
#pragma once
#include <string>
#include <functional>
#include <cstdint>
#include "platform/CCPlatformMacros.h"
#include "base/CCRef.h"
#include "base/ccTypes.h"
#include "base/CCVector.h"
#include "renderer/CCPipelineDescriptor.h"
#include "renderer/backend/Types.h"
#include "renderer/CCMeshCommand.h"
NS_CC_BEGIN
class Texture2D;
class Pass;
class MeshCommand;
2021-12-25 10:04:45 +08:00
using CullFaceSide = backend::CullMode;
using FrontFace = backend::Winding;
2019-11-23 20:27:39 +08:00
using DepthFunction = backend::CompareFunction;
/**
* Defines the rendering state of the graphics device.
*/
class CC_DLL RenderState : public Ref
{
friend class Material;
friend class Technique;
friend class Pass;
public:
std::string getName() const;
/**
* Binds the render state for this RenderState and any of its parents, top-down,
* for the given pass.
*/
2021-12-25 10:04:45 +08:00
void bindPass(Pass* pass, MeshCommand*);
2019-11-23 20:27:39 +08:00
/**
* Defines a block of fixed-function render states that can be applied to a
* RenderState object.
*/
2021-12-25 10:04:45 +08:00
class CC_DLL StateBlock // : public Ref
2019-11-23 20:27:39 +08:00
{
friend class RenderState;
friend class Pass;
friend class RenderQueue;
friend class Renderer;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
public:
/**
* Creates a new StateBlock with default render state settings.
*/
2021-12-25 10:04:45 +08:00
// static StateBlock* create();
2019-11-23 20:27:39 +08:00
/** The recommended way to create StateBlocks is by calling `create`.
* Don't use `new` or `delete` on them.
2021-12-25 10:04:45 +08:00
*
2019-11-23 20:27:39 +08:00
*/
2021-12-25 10:04:45 +08:00
StateBlock() = default;
~StateBlock() = default;
StateBlock(const StateBlock&) = default;
2019-11-23 20:27:39 +08:00
/**
* Binds the state in this StateBlock to the renderer.
*
* This method handles both setting and restoring of render states to ensure that
* only the state explicitly defined by this StateBlock is applied to the renderer.
*/
2021-12-25 10:04:45 +08:00
void bind(PipelineDescriptor* programState);
2019-11-23 20:27:39 +08:00
/**
* Explicitly sets the source and destination used in the blend function for this render state.
*
* Note that the blend function is only applied when blending is enabled.
*
* @param blendFunc Specifies how the blending factors are computed.
*/
void setBlendFunc(const BlendFunc& blendFunc);
/**
* Toggles blending.
*
* @param enabled true to enable, false to disable.
*/
void setBlend(bool enabled);
/**
* Explicitly sets the source used in the blend function for this render state.
*
* Note that the blend function is only applied when blending is enabled.
*
* @param blend Specifies how the source blending factors are computed.
*/
void setBlendSrc(backend::BlendFactor blend);
/**
* Explicitly sets the source used in the blend function for this render state.
*
* Note that the blend function is only applied when blending is enabled.
*
* @param blend Specifies how the destination blending factors are computed.
*/
void setBlendDst(backend::BlendFactor blend);
/**
* Explicitly enables or disables backface culling.
*
* @param enabled true to enable, false to disable.
*/
void setCullFace(bool enabled);
/**
* Sets the side of the facets to cull.
*
* When not explicitly set, the default is to cull back-facing facets.
*
* @param side The side to cull.
*/
void setCullFaceSide(CullFaceSide side);
/**
* Sets the winding for front facing polygons.
*
* By default, counter-clockwise wound polygons are considered front facing.
*
* @param winding The winding for front facing polygons.
*/
void setFrontFace(FrontFace winding);
/**
* Toggles depth testing.
*
* By default, depth testing is disabled.
*
* @param enabled true to enable, false to disable.
*/
void setDepthTest(bool enabled);
/**
* Toggles depth writing.
*
* @param enabled true to enable, false to disable.
*/
void setDepthWrite(bool enabled);
/**
* Sets the depth function to use when depth testing is enabled.
*
* When not explicitly set and when depth testing is enabled, the default
* depth function is DEPTH_LESS.
*
* @param func The depth function.
*/
void setDepthFunction(DepthFunction func);
/**
* Sets a render state from the given name and value strings.
*
* This method attempts to interpret the passed in strings as render state
* name and value. This is normally used when loading render states from
* material files.
*
* @param name Name of the render state to set.
* @param value Value of the specified render state.
*/
void setState(std::string_view name, std::string_view value);
2019-11-23 20:27:39 +08:00
uint32_t getHash() const;
bool isDirty() const;
/** StateBlock bits to be used with invalidate */
enum
{
2021-12-25 10:04:45 +08:00
RS_BLEND = (1 << 0),
RS_BLEND_FUNC = (1 << 1),
RS_CULL_FACE = (1 << 2),
RS_DEPTH_TEST = (1 << 3),
RS_DEPTH_WRITE = (1 << 4),
RS_DEPTH_FUNC = (1 << 5),
2019-11-23 20:27:39 +08:00
RS_CULL_FACE_SIDE = (1 << 6),
2021-12-25 10:04:45 +08:00
// RS_STENCIL_TEST = (1 << 7),
// RS_STENCIL_WRITE = (1 << 8),
// RS_STENCIL_FUNC = (1 << 9),
// RS_STENCIL_OP = (1 << 10),
2019-11-23 20:27:39 +08:00
RS_FRONT_FACE = (1 << 11),
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
RS_ALL_ONES = 0xFFFFFFFF,
};
protected:
/**
2021-12-25 10:04:45 +08:00
* update internal states of ProgramState
*/
void apply(PipelineDescriptor* pipelineDescriptor);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
static void restoreUnmodifiedStates(int32_t flags, PipelineDescriptor* pipelineDescriptor);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
bool _cullFaceEnabled = false;
bool _depthTestEnabled = true;
bool _depthWriteEnabled = false;
DepthFunction _depthFunction = DepthFunction::LESS;
bool _blendEnabled = true;
2019-11-23 20:27:39 +08:00
backend::BlendFactor _blendSrc = backend::BlendFactor::ONE;
backend::BlendFactor _blendDst = backend::BlendFactor::ZERO;
2021-12-25 10:04:45 +08:00
CullFaceSide _cullFaceSide = CullFaceSide::BACK;
FrontFace _frontFace = FrontFace::COUNTER_CLOCK_WISE;
int32_t _modifiedBits = 0L;
2019-11-23 20:27:39 +08:00
mutable uint32_t _hash;
mutable bool _hashDirty;
};
StateBlock& getStateBlock() const;
protected:
RenderState() = default;
2021-12-25 10:04:45 +08:00
mutable uint32_t _hash = 0;
2019-11-23 20:27:39 +08:00
mutable bool _hashDirty = true;
/**
* The StateBlock of fixed-function render states that can be applied to the RenderState.
*/
mutable StateBlock _state;
// name, for filtering
std::string _name;
};
NS_CC_END