2015-05-06 04:07:32 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2015-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2015-05-06 04:07:32 +08:00
|
|
|
Copyright (c) 2014 GamePlay3D team
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
https://axys1.github.io/
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
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/
|
|
|
|
|
|
|
|
****************************************************************************/
|
2019-01-30 09:35:17 +08:00
|
|
|
#pragma once
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <functional>
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
#include "platform/CCPlatformMacros.h"
|
|
|
|
#include "base/CCRef.h"
|
|
|
|
#include "base/ccTypes.h"
|
|
|
|
#include "base/CCVector.h"
|
|
|
|
|
2019-02-15 14:30:10 +08:00
|
|
|
#include "renderer/CCPipelineDescriptor.h"
|
|
|
|
#include "renderer/backend/Types.h"
|
2019-03-14 16:22:54 +08:00
|
|
|
#include "renderer/CCMeshCommand.h"
|
2019-02-15 14:30:10 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
class Texture2D;
|
|
|
|
class Pass;
|
2019-03-15 14:26:27 +08:00
|
|
|
class MeshCommand;
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
using CullFaceSide = backend::CullMode;
|
|
|
|
using FrontFace = backend::Winding;
|
2019-01-30 09:35:17 +08:00
|
|
|
using DepthFunction = backend::CompareFunction;
|
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
/**
|
|
|
|
* Defines the rendering state of the graphics device.
|
|
|
|
*/
|
2022-07-15 19:17:01 +08:00
|
|
|
class AX_DLL RenderState : public Ref
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
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*);
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines a block of fixed-function render states that can be applied to a
|
|
|
|
* RenderState object.
|
|
|
|
*/
|
2022-07-15 19:17:01 +08:00
|
|
|
class AX_DLL StateBlock // : public Ref
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
friend class RenderState;
|
|
|
|
friend class Pass;
|
|
|
|
friend class RenderQueue;
|
|
|
|
friend class Renderer;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Creates a new StateBlock with default render state settings.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
// static StateBlock* create();
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2015-06-16 08:43:37 +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
|
|
|
*
|
2015-06-16 08:43:37 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
StateBlock() = default;
|
|
|
|
~StateBlock() = default;
|
|
|
|
StateBlock(const StateBlock&) = default;
|
2015-05-06 04:07:32 +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);
|
2015-05-06 04:07:32 +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.
|
|
|
|
*/
|
2019-02-15 14:30:10 +08:00
|
|
|
void setBlendSrc(backend::BlendFactor blend);
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-02-15 14:30:10 +08:00
|
|
|
void setBlendDst(backend::BlendFactor blend);
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
void setState(std::string_view name, std::string_view value);
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
uint32_t getHash() const;
|
|
|
|
bool isDirty() const;
|
|
|
|
|
2015-06-13 08:14:09 +08:00
|
|
|
/** 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),
|
2015-06-13 08:14:09 +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),
|
2015-06-13 08:14:09 +08:00
|
|
|
RS_FRONT_FACE = (1 << 11),
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-06-13 08:14:09 +08:00
|
|
|
RS_ALL_ONES = 0xFFFFFFFF,
|
|
|
|
};
|
|
|
|
|
2019-02-15 14:30:10 +08:00
|
|
|
protected:
|
2015-06-16 05:12:44 +08:00
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* update internal states of ProgramState
|
|
|
|
*/
|
|
|
|
void apply(PipelineDescriptor* pipelineDescriptor);
|
2015-06-16 05:12:44 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
static void restoreUnmodifiedStates(int32_t flags, PipelineDescriptor* pipelineDescriptor);
|
2015-05-14 08:39: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-02-15 14:30:10 +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;
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
mutable uint32_t _hash;
|
|
|
|
mutable bool _hashDirty;
|
|
|
|
};
|
|
|
|
|
2019-02-15 14:30:10 +08:00
|
|
|
StateBlock& getStateBlock() const;
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
protected:
|
2019-02-15 14:30:10 +08:00
|
|
|
RenderState() = default;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
mutable uint32_t _hash = 0;
|
2019-01-30 09:35:17 +08:00
|
|
|
mutable bool _hashDirty = true;
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The StateBlock of fixed-function render states that can be applied to the RenderState.
|
|
|
|
*/
|
2019-02-15 14:30:10 +08:00
|
|
|
mutable StateBlock _state;
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
// name, for filtering
|
|
|
|
std::string _name;
|
|
|
|
};
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|