Shadow culling for v3.3.1 (#3810)

This commit is contained in:
troublemaker52025
2021-09-02 19:34:51 +08:00
committed by GitHub
parent 5c8713cbc6
commit 46976eff2d
21 changed files with 606 additions and 258 deletions
+6 -1
View File
@@ -99,7 +99,7 @@ void Mat4::createPerspective(float fieldOfView, float aspectRatio,
float zNearPlane, float zFarPlane, Mat4 *dst) {
GP_ASSERT(dst);
GP_ASSERT(zFarPlane != zNearPlane);
GP_ASSERT(fieldOfView != 0.0f);
GP_ASSERT(fieldOfView != 0.0F);
const float minClipZ = -1.0F;
const float projectionSignY = 1.0F;
@@ -722,6 +722,11 @@ void Mat4::getBackVector(Vec3 *dst) const {
dst->z = m[10];
}
Mat4 Mat4::clone() const {
Mat4 mat(*this);
return mat;
}
Mat4 Mat4::getInversed() const {
Mat4 mat(*this);
mat.inverse();
+7
View File
@@ -500,6 +500,13 @@ public:
*/
bool inverse();
/**
* Clone this matrix.
*
* @return a new clone matrix.
*/
Mat4 clone() const;
/**
* Get the inversed matrix.
*/
+70 -50
View File
@@ -28,12 +28,6 @@
NS_CC_MATH_BEGIN
Vec3::Vec3()
: x(0.0f),
y(0.0f),
z(0.0f) {
}
Vec3::Vec3(float xx, float yy, float zz)
: x(xx),
y(yy),
@@ -56,22 +50,19 @@ Vec3 Vec3::fromColor(unsigned int color) {
float components[3];
int componentIndex = 0;
for (int i = 2; i >= 0; --i) {
int component = (color >> i * 8) & 0x0000ff;
const unsigned int component = (color >> i * 8) & 0x0000ff;
components[componentIndex++] = static_cast<float>(component) / 255.0f;
components[componentIndex++] = static_cast<float>(component) / 255.0F;
}
Vec3 value(components);
return value;
}
Vec3::~Vec3() {
}
float Vec3::angle(const Vec3 &v1, const Vec3 &v2) {
float dx = v1.y * v2.z - v1.z * v2.y;
float dy = v1.z * v2.x - v1.x * v2.z;
float dz = v1.x * v2.y - v1.y * v2.x;
const float dx = v1.y * v2.z - v1.z * v2.y;
const float dy = v1.z * v2.x - v1.x * v2.z;
const float dz = v1.x * v2.y - v1.y * v2.x;
return std::atan2(std::sqrt(dx * dx + dy * dy + dz * dz) + MATH_FLOAT_SMALL, dot(v1, v2));
}
@@ -88,22 +79,28 @@ void Vec3::clamp(const Vec3 &min, const Vec3 &max) {
GP_ASSERT(!(min.x > max.x || min.y > max.y || min.z > max.z));
// Clamp the x value.
if (x < min.x)
if (x < min.x) {
x = min.x;
if (x > max.x)
}
if (x > max.x) {
x = max.x;
}
// Clamp the y value.
if (y < min.y)
if (y < min.y) {
y = min.y;
if (y > max.y)
}
if (y > max.y) {
y = max.y;
}
// Clamp the z value.
if (z < min.z)
if (z < min.z) {
z = min.z;
if (z > max.z)
}
if (z > max.z) {
z = max.z;
}
}
void Vec3::clamp(const Vec3 &v, const Vec3 &min, const Vec3 &max, Vec3 *dst) {
@@ -112,24 +109,30 @@ void Vec3::clamp(const Vec3 &v, const Vec3 &min, const Vec3 &max, Vec3 *dst) {
// Clamp the x value.
dst->x = v.x;
if (dst->x < min.x)
if (dst->x < min.x) {
dst->x = min.x;
if (dst->x > max.x)
}
if (dst->x > max.x) {
dst->x = max.x;
}
// Clamp the y value.
dst->y = v.y;
if (dst->y < min.y)
if (dst->y < min.y) {
dst->y = min.y;
if (dst->y > max.y)
}
if (dst->y > max.y) {
dst->y = max.y;
}
// Clamp the z value.
dst->z = v.z;
if (dst->z < min.z)
if (dst->z < min.z) {
dst->z = min.z;
if (dst->z > max.z)
}
if (dst->z > max.z) {
dst->z = max.z;
}
}
void Vec3::cross(const Vec3 &v) {
@@ -145,6 +148,13 @@ void Vec3::cross(const Vec3 &v1, const Vec3 &v2, Vec3 *dst) {
MathUtil::crossVec3(&v1.x, &v2.x, &dst->x);
}
void Vec3::crossProduct(const Vec3 &v1, const Vec3 &v2, Vec3 *dst) {
dst->set(
v1.y * v2.z - v1.z * v2.y,
v1.z * v2.x - v1.x * v2.z,
v1.x * v2.y - v1.y * v2.x);
}
void Vec3::multiply(const Vec3 &v) {
x *= v.x;
y *= v.y;
@@ -158,16 +168,20 @@ void Vec3::multiply(const Vec3 &v1, const Vec3 &v2, Vec3 *dst) {
}
void Vec3::transformMat3(const Vec3 &v, const Mat3 &m) {
float ix = v.x, iy = v.y, iz = v.z;
const float ix = v.x;
const float iy = v.y;
const float iz = v.z;
x = ix * m.m[0] + iy * m.m[3] + iz * m.m[6];
y = ix * m.m[1] + iy * m.m[4] + iz * m.m[7];
z = ix * m.m[2] + iy * m.m[5] + iz * m.m[8];
}
void Vec3::transformMat4(const Vec3 &v, const Mat4 &m) {
float ix = v.x, iy = v.y, iz = v.z;
const float ix = v.x;
const float iy = v.y;
const float iz = v.z;
float rhw = m.m[3] * ix + m.m[7] * iy + m.m[11] * iz + m.m[15];
rhw = rhw ? 1 / rhw : 1;
rhw = static_cast<bool>(rhw) ? 1.0F / rhw : 1.0F;
x = (m.m[0] * ix + m.m[4] * iy + m.m[8] * iz + m.m[12]) * rhw;
y = (m.m[1] * ix + m.m[5] * iy + m.m[9] * iz + m.m[13]) * rhw;
@@ -175,13 +189,16 @@ void Vec3::transformMat4(const Vec3 &v, const Mat4 &m) {
}
void Vec3::transformQuat(const Quaternion &q) {
float qx = q.x, qy = q.y, qz = q.z, qw = q.w;
const float qx = q.x;
const float qy = q.y;
const float qz = q.z;
const float qw = q.w;
// calculate quat * vec
float ix = qw * x + qy * z - qz * y;
float iy = qw * y + qz * x - qx * z;
float iz = qw * z + qx * y - qy * x;
float iw = -qx * x - qy * y - qz * z;
const float ix = qw * x + qy * z - qz * y;
const float iy = qw * y + qz * x - qx * z;
const float iz = qw * z + qx * y - qy * x;
const float iw = -qx * x - qy * y - qz * z;
// calculate result * inverse quat
x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
@@ -190,17 +207,17 @@ void Vec3::transformQuat(const Quaternion &q) {
}
float Vec3::distance(const Vec3 &v) const {
float dx = v.x - x;
float dy = v.y - y;
float dz = v.z - z;
const float dx = v.x - x;
const float dy = v.y - y;
const float dz = v.z - z;
return std::sqrt(dx * dx + dy * dy + dz * dz);
}
float Vec3::distanceSquared(const Vec3 &v) const {
float dx = v.x - x;
float dy = v.y - y;
float dz = v.z - z;
const float dx = v.x - x;
const float dy = v.y - y;
const float dz = v.z - z;
return (dx * dx + dy * dy + dz * dz);
}
@@ -216,15 +233,17 @@ float Vec3::dot(const Vec3 &v1, const Vec3 &v2) {
void Vec3::normalize() {
float n = x * x + y * y + z * z;
// Already normalized.
if (n == 1.0f)
if (n == 1.0F) {
return;
}
n = std::sqrt(n);
// Too close to zero.
if (n < MATH_TOLERANCE)
if (n < MATH_TOLERANCE) {
return;
}
n = 1.0f / n;
n = 1.0F / n;
x *= n;
y *= n;
z *= n;
@@ -263,15 +282,16 @@ void Vec3::min(const Vec3 &v1, const Vec3 &v2, Vec3 *dst) {
}
void Vec3::smooth(const Vec3 &target, float elapsedTime, float responseTime) {
if (elapsedTime > 0) {
if (elapsedTime > 0.0F) {
*this += (target - *this) * (elapsedTime / (elapsedTime + responseTime));
}
}
const Vec3 Vec3::ZERO(0.0f, 0.0f, 0.0f);
const Vec3 Vec3::ONE(1.0f, 1.0f, 1.0f);
const Vec3 Vec3::UNIT_X(1.0f, 0.0f, 0.0f);
const Vec3 Vec3::UNIT_Y(0.0f, 1.0f, 0.0f);
const Vec3 Vec3::UNIT_Z(0.0f, 0.0f, 1.0f);
const Vec3 Vec3::ZERO(0.0F, 0.0F, 0.0F);
const Vec3 Vec3::ONE(1.0F, 1.0F, 1.0F);
const Vec3 Vec3::UNIT_X(1.0F, 0.0F, 0.0F);
const Vec3 Vec3::UNIT_Y(0.0F, 1.0F, 0.0F);
const Vec3 Vec3::UNIT_Z(0.0F, 0.0F, 1.0F);
const Vec3 Vec3::FORWARD(0.0F, 0.0F, -1.0F);
NS_CC_MATH_END
+27 -13
View File
@@ -23,7 +23,6 @@
#ifndef MATH_VEC3_H
#define MATH_VEC3_H
#include <cmath>
#include "math/MathBase.h"
/**
@@ -51,22 +50,22 @@ public:
/**
* The x-coordinate.
*/
float x;
float x{};
/**
* The y-coordinate.
*/
float y;
float y{};
/**
* The z-coordinate.
*/
float z;
float z{};
/**
* Constructs a new vector initialized to all zeros.
*/
Vec3();
Vec3() = default;
/**
* Constructs a new vector initialized to the specified values.
@@ -82,7 +81,7 @@ public:
*
* @param array An array containing the elements of the vector in the order x, y, z.
*/
Vec3(const float *array);
explicit Vec3(const float *array);
/**
* Constructs a vector that describes the direction between the specified points.
@@ -112,7 +111,7 @@ public:
/**
* Destructor.
*/
~Vec3();
~Vec3() = default;
/**
* Indicates whether this vector contains all zeros.
@@ -188,6 +187,15 @@ public:
*/
void cross(const Vec3 &v);
/**
* Calculate cross product.
*
* @param v1 is a vector.
* @param v2 is a vector.
* @param dst is a vector used to calculate the cross product of v1 and v2.
*/
static void crossProduct(const Vec3 &v1, const Vec3 &v2, Vec3 *dst);
/**
* Computes the cross product of the specified vectors and stores the result in dst.
*
@@ -525,9 +533,11 @@ public:
* @return bool
*/
inline bool operator<(const Vec3 &rhs) const {
if (x < rhs.x && y < rhs.y && z < rhs.z)
return true;
return false;
bool temp = false;
if (x < rhs.x && y < rhs.y && z < rhs.z) {
temp = true;
}
return temp;
}
/**
@@ -538,9 +548,11 @@ public:
* @return bool
*/
inline bool operator>(const Vec3 &rhs) const {
if (x > rhs.x && y > rhs.y && z > rhs.z)
return true;
return false;
bool temp = false;
if (x > rhs.x && y > rhs.y && z > rhs.z) {
temp = true;
}
return temp;
}
/**
@@ -580,6 +592,8 @@ public:
static const Vec3 UNIT_Y;
/** equals to Vec3(0,0,1) */
static const Vec3 UNIT_Z;
/** equals to Vec3(0,0,-1) */
static const Vec3 FORWARD;
};
/**
+2
View File
@@ -18,6 +18,8 @@
This file was modified to fit the cocos2d-x project
*/
#pragma once
#include <cmath>
#include "math/Vec3.h"
#include "math/Mat4.h"
@@ -24,7 +24,6 @@
****************************************************************************/
#include "PipelineSceneData.h"
#include "gfx-base/GFXCommandBuffer.h"
#include "gfx-base/GFXDevice.h"
#include "gfx-base/GFXFramebuffer.h"
@@ -35,8 +34,6 @@ void PipelineSceneData::activate(gfx::Device *device, RenderPipeline *pipeline)
{
_device = device;
_pipeline = pipeline;
_sphere = CC_NEW(scene::Sphere);
}
void PipelineSceneData::setPipelineSharedSceneData(scene::PipelineSharedSceneData *data)
@@ -46,8 +43,6 @@ void PipelineSceneData::setPipelineSharedSceneData(scene::PipelineSharedSceneDat
void PipelineSceneData::destroy()
{
CC_SAFE_DELETE(_sphere);
for (auto &pair : _shadowFrameBufferMap) {
pair.second->destroy();
delete pair.second;
@@ -55,16 +55,26 @@ public:
inline const RenderObjectList & getShadowObjects() const { return _shadowObjects; }
inline void setRenderObjects(RenderObjectList &&ro) { _renderObjects = std::forward<RenderObjectList>(ro); }
inline void setShadowObjects(RenderObjectList &&ro) { _shadowObjects = std::forward<RenderObjectList>(ro); }
inline scene::Sphere * getSphere() const { return _sphere; }
inline float getShadowCameraFar() const { return _shadowCameraFar; }
inline void setShadowCameraFar(float shadowDistance) { _shadowCameraFar = shadowDistance; }
inline Mat4 getMatShadowView() const { return _matShadowView; }
inline void setMatShadowView(const Mat4 &matShadowView) { _matShadowView = matShadowView; }
inline Mat4 getMatShadowProj() const { return _matShadowProj; }
inline void setMatShadowProj(const Mat4 &matShadowProj) { _matShadowProj = matShadowProj; }
inline Mat4 getMatShadowViewProj() const { return _matShadowViewProj; }
inline void setMatShadowViewProj(const Mat4 &matShadowViewProj) { _matShadowViewProj = matShadowViewProj; }
private:
RenderObjectList _renderObjects;
RenderObjectList _shadowObjects;
scene::PipelineSharedSceneData *_sharedSceneData = nullptr;
RenderPipeline * _pipeline = nullptr;
gfx::Device * _device = nullptr;
scene::Sphere * _sphere = nullptr;
scene::PipelineSharedSceneData *_sharedSceneData = nullptr;
RenderPipeline * _pipeline = nullptr;
gfx::Device * _device = nullptr;
float _shadowCameraFar = 0.0F;
Mat4 _matShadowView;
Mat4 _matShadowProj;
Mat4 _matShadowViewProj;
std::unordered_map<const scene::Light *, gfx::Framebuffer *> _shadowFrameBufferMap;
};
+110 -120
View File
@@ -47,9 +47,9 @@ namespace pipeline {
Mat4 matShadowViewProj;
void PipelineUBO::updateGlobalUBOView(const RenderPipeline * /*pipeline*/, std::array<float, UBOGlobal::COUNT> *bufferView) {
auto *const root = scene::Root::instance;
auto * device = gfx::Device::getInstance();
auto & uboGlobalView = *bufferView;
const scene::Root * root = scene::Root::instance;
const gfx::Device * device = gfx::Device::getInstance();
std::array<float, UBOGlobal::COUNT> &uboGlobalView = *bufferView;
const auto shadingWidth = std::floor(device->getWidth());
const auto shadingHeight = std::floor(device->getHeight());
@@ -71,24 +71,23 @@ void PipelineUBO::updateGlobalUBOView(const RenderPipeline * /*pipeline*/, std::
}
void PipelineUBO::updateCameraUBOView(const RenderPipeline *pipeline, float *output, const scene::Camera *camera) {
const auto *const scene = camera->scene;
const scene::DirectionalLight *mainLight = scene->getMainLight();
auto * sceneData = pipeline->getPipelineSceneData();
auto *const sharedData = sceneData->getSharedData();
const auto fpScale = sharedData->fpScale;
auto *const descriptorSet = pipeline->getDescriptorSet();
auto * ambient = sharedData->ambient;
auto * fog = sharedData->fog;
const auto isHDR = sharedData->isHDR;
const auto shadingScale = sharedData->shadingScale;
const scene::RenderScene * scene = camera->scene;
const scene::DirectionalLight * mainLight = scene->getMainLight();
const PipelineSceneData * sceneData = pipeline->getPipelineSceneData();
const scene::PipelineSharedSceneData *sharedData = sceneData->getSharedData();
const float fpScale = sharedData->fpScale;
const gfx::DescriptorSet * descriptorSet = pipeline->getDescriptorSet();
const scene::Ambient * ambient = sharedData->ambient;
const scene::Fog * fog = sharedData->fog;
const bool isHDR = sharedData->isHDR;
const float shadingScale = sharedData->shadingScale;
const gfx::Device * device = gfx::Device::getInstance();
auto *device = gfx::Device::getInstance();
const auto shadingWidth = static_cast<float>(std::floor(device->getWidth()));
const auto shadingHeight = static_cast<float>(std::floor(device->getHeight()));
const auto shadingWidth = std::floor(device->getWidth());
const auto shadingHeight = std::floor(device->getHeight());
output[UBOCamera::SCREEN_SCALE_OFFSET + 0] = static_cast<float>(camera->width / shadingWidth * shadingScale);
output[UBOCamera::SCREEN_SCALE_OFFSET + 1] = static_cast<float>(camera->height / shadingHeight * shadingScale);
output[UBOCamera::SCREEN_SCALE_OFFSET + 0] = static_cast<float>(camera->width) / shadingWidth * shadingScale;
output[UBOCamera::SCREEN_SCALE_OFFSET + 1] = static_cast<float>(camera->height) / shadingHeight * shadingScale;
output[UBOCamera::SCREEN_SCALE_OFFSET + 2] = 1.0F / output[UBOCamera::SCREEN_SCALE_OFFSET];
output[UBOCamera::SCREEN_SCALE_OFFSET + 3] = 1.0F / output[UBOCamera::SCREEN_SCALE_OFFSET + 1];
@@ -99,8 +98,8 @@ void PipelineUBO::updateCameraUBOView(const RenderPipeline *pipeline, float *out
output[UBOCamera::EXPOSURE_OFFSET + 3] = fpScale / exposure;
if (mainLight) {
TO_VEC3(output, mainLight->getDirection(), UBOCamera::MAIN_LIT_DIR_OFFSET);
TO_VEC3(output, mainLight->getColor(), UBOCamera::MAIN_LIT_COLOR_OFFSET);
TO_VEC3(output, mainLight->getDirection(), UBOCamera::MAIN_LIT_DIR_OFFSET)
TO_VEC3(output, mainLight->getColor(), UBOCamera::MAIN_LIT_COLOR_OFFSET)
if (mainLight->getUseColorTemperature()) {
const auto &colorTempRGB = mainLight->getColorTemperatureRGB();
output[UBOCamera::MAIN_LIT_COLOR_OFFSET + 0] *= colorTempRGB.x;
@@ -124,7 +123,7 @@ void PipelineUBO::updateCameraUBOView(const RenderPipeline *pipeline, float *out
} else {
skyColor.w = ambient->skyIllum * exposure;
}
TO_VEC4(output, skyColor, UBOCamera::AMBIENT_SKY_OFFSET);
TO_VEC4(output, skyColor, UBOCamera::AMBIENT_SKY_OFFSET)
output[UBOCamera::AMBIENT_GROUND_OFFSET + 0] = ambient->groundAlbedo.x;
output[UBOCamera::AMBIENT_GROUND_OFFSET + 1] = ambient->groundAlbedo.y;
@@ -136,7 +135,7 @@ void PipelineUBO::updateCameraUBOView(const RenderPipeline *pipeline, float *out
memcpy(output + UBOCamera::MAT_VIEW_OFFSET, camera->matView.m, sizeof(cc::Mat4));
memcpy(output + UBOCamera::MAT_VIEW_INV_OFFSET, camera->node->getWorldMatrix().m, sizeof(cc::Mat4));
TO_VEC3(output, camera->position, UBOCamera::CAMERA_POS_OFFSET);
TO_VEC3(output, camera->position, UBOCamera::CAMERA_POS_OFFSET)
memcpy(output + UBOCamera::MAT_PROJ_OFFSET, camera->matProj.m, sizeof(cc::Mat4));
memcpy(output + UBOCamera::MAT_PROJ_INV_OFFSET, camera->matProjInv.m, sizeof(cc::Mat4));
@@ -145,7 +144,7 @@ void PipelineUBO::updateCameraUBOView(const RenderPipeline *pipeline, float *out
output[UBOCamera::CAMERA_POS_OFFSET + 3] = getCombineSignY();
if (fog->enabled) {
TO_VEC4(output, fog->color, UBOCamera::GLOBAL_FOG_COLOR_OFFSET);
TO_VEC4(output, fog->color, UBOCamera::GLOBAL_FOG_COLOR_OFFSET)
output[UBOCamera::GLOBAL_FOG_BASE_OFFSET + 0] = fog->start;
output[UBOCamera::GLOBAL_FOG_BASE_OFFSET + 1] = fog->end;
@@ -158,70 +157,61 @@ void PipelineUBO::updateCameraUBOView(const RenderPipeline *pipeline, float *out
}
void PipelineUBO::updateShadowUBOView(const RenderPipeline *pipeline, std::array<float, UBOShadow::COUNT> *bufferView, const scene::Camera *camera) {
const auto *const scene = camera->scene;
const scene::DirectionalLight *mainLight = scene->getMainLight();
auto * device = gfx::Device::getInstance();
auto *const sceneData = pipeline->getPipelineSceneData();
auto *const shadowInfo = sceneData->getSharedData()->shadow;
auto & shadowUBO = *bufferView;
auto * sphere = sceneData->getSphere();
const bool hFTexture = supportsHalfFloatTexture(device);
const scene::RenderScene *const scene = camera->scene;
const scene::DirectionalLight * mainLight = scene->getMainLight();
gfx::Device * device = gfx::Device::getInstance();
const PipelineSceneData * sceneData = pipeline->getPipelineSceneData();
scene::Shadow *const shadowInfo = sceneData->getSharedData()->shadow;
std::array<float, UBOShadow::COUNT> &shadowUBO = *bufferView;
const bool hFTexture = supportsHalfFloatTexture(device);
if (shadowInfo->enabled) {
if (mainLight && shadowInfo->shadowType == scene::ShadowType::SHADOWMAP) {
auto *const node = mainLight->getNode();
cc::Mat4 matShadowCamera;
// light proj
float x;
float y;
float nearClamp;
float farClamp;
if (shadowInfo->autoAdapt) {
Vec3 tmpCenter;
getShadowWorldMatrix(sphere, node->getWorldRotation(), mainLight->getDirection(), &matShadowCamera, &tmpCenter);
const float radius = sphere->getRadius();
x = radius;
y = radius;
const float halfFar = tmpCenter.distance(sphere->getCenter());
farClamp = std::min(halfFar * COEFFICIENT_OF_EXPANSION, SHADOW_CAMERA_MAX_FAR);
} else {
matShadowCamera = mainLight->getNode()->getWorldMatrix();
x = shadowInfo->orthoSize;
y = shadowInfo->orthoSize;
farClamp = shadowInfo->farValue;
}
const auto matShadowView = matShadowCamera.getInversed();
const auto projectionSinY = device->getCapabilities().clipSpaceSignY;
Mat4 matShadowView;
Mat4 matShadowProj;
Mat4::createOrthographicOffCenter(-x, x, -y, y, shadowInfo->nearValue, farClamp, device->getCapabilities().clipSpaceMinZ, projectionSinY, &matShadowProj);
matShadowViewProj = matShadowProj;
matShadowViewProj.multiply(matShadowView);
Mat4 matShadowViewProj;
if (!shadowInfo->fixedArea) {
nearClamp = shadowInfo->nearValue;
farClamp = sceneData->getShadowCameraFar();
matShadowView = sceneData->getMatShadowView();
matShadowProj = sceneData->getMatShadowProj();
matShadowViewProj = sceneData->getMatShadowViewProj();
} else {
const auto *node = mainLight->getNode();
matShadowView = node->getWorldMatrix().getInversed();
const float x = shadowInfo->orthoSize;
const float y = shadowInfo->orthoSize;
nearClamp = shadowInfo->nearValue;
farClamp = shadowInfo->farValue;
const float projectionSinY = device->getCapabilities().clipSpaceSignY;
const float clipSpaceMinZ = device->getCapabilities().clipSpaceMinZ;
Mat4::createOrthographicOffCenter(-x, x, -y, y, -nearClamp,
farClamp, clipSpaceMinZ, projectionSinY, &matShadowProj);
matShadowViewProj = matShadowProj * matShadowView;
}
memcpy(shadowUBO.data() + UBOShadow::MAT_LIGHT_VIEW_OFFSET, matShadowView.m, sizeof(matShadowView));
const float shadowProjDepthInfos[4] = {matShadowProj.m[10], matShadowProj.m[14], matShadowProj.m[11], matShadowProj.m[15]};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_PROJ_DEPTH_INFO_OFFSET, &shadowProjDepthInfos, sizeof(shadowProjDepthInfos));
const float shadowProjInfos[4] = {matShadowProj.m[00], matShadowProj.m[05], 1.0F / matShadowProj.m[00], 1.0F / matShadowProj.m[05]};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_PROJ_INFO_OFFSET, &shadowProjInfos, sizeof(shadowProjInfos));
memcpy(shadowUBO.data() + UBOShadow::MAT_LIGHT_VIEW_PROJ_OFFSET, matShadowViewProj.m, sizeof(matShadowViewProj));
const float linear = hFTexture ? 1.0F : 0.0F;
float shadowNFLSInfos[4] = {shadowInfo->nearValue, farClamp, linear, 1.0F - shadowInfo->saturation};
const float linear = 0.0F;
const float shadowNFLSInfos[4] = {nearClamp, farClamp, linear, 1.0F - shadowInfo->saturation};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET, &shadowNFLSInfos, sizeof(shadowNFLSInfos));
float shadowWHPBInfos[4] = {shadowInfo->size.x, shadowInfo->size.y, static_cast<float>(shadowInfo->pcfType), shadowInfo->bias};
const float shadowWHPBInfos[4] = {shadowInfo->size.x, shadowInfo->size.y, static_cast<float>(shadowInfo->pcfType), shadowInfo->bias};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET, &shadowWHPBInfos, sizeof(shadowWHPBInfos));
const float packing = hFTexture ? 0.0F : 1.0F;
float shadowLPNNInfos[4] = {0.0F, packing, shadowInfo->normalBias, 0.0F};
const float shadowLPNNInfos[4] = {0.0F, packing, shadowInfo->normalBias, 0.0F};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET, &shadowLPNNInfos, sizeof(shadowLPNNInfos));
float shadowProjDepthInfos[4] = {matShadowProj.m[10], matShadowProj.m[14], matShadowProj.m[11], matShadowProj.m[15]};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_PROJ_DEPTH_INFO_OFFSET, &shadowProjDepthInfos, sizeof(shadowProjDepthInfos));
float shadowProjInfos[4] = {matShadowProj.m[00], matShadowProj.m[05], 1.0F / matShadowProj.m[00], 1.0F / matShadowProj.m[05]};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_PROJ_INFO_OFFSET, &shadowProjInfos, sizeof(shadowProjInfos));
} else if (mainLight && shadowInfo->shadowType == scene::ShadowType::PLANAR) {
updateDirLight(shadowInfo, mainLight, &shadowUBO);
}
@@ -231,51 +221,53 @@ void PipelineUBO::updateShadowUBOView(const RenderPipeline *pipeline, std::array
}
}
void PipelineUBO::updateShadowUBOLightView(const RenderPipeline *pipeline, std::array<float, UBOShadow::COUNT> *bufferView, const scene::Light *light) {
auto *const sceneData = pipeline->getPipelineSceneData();
auto * shadowInfo = sceneData->getSharedData()->shadow;
void PipelineUBO::updateShadowUBOLightView(const RenderPipeline *pipeline, std::array<float, UBOShadow::COUNT> *bufferView,
const scene::Light *light) {
const auto *sceneData = pipeline->getPipelineSceneData();
const auto *shadowInfo = sceneData->getSharedData()->shadow;
auto * device = gfx::Device::getInstance();
auto * sphere = sceneData->getSphere();
auto & shadowUBO = *bufferView;
const bool hFTexture = supportsHalfFloatTexture(device);
const float linear = 0.0F;
const float packing = hFTexture ? 0.0F : 1.0F;
switch (light->getType()) {
case scene::LightType::DIRECTIONAL: {
const auto *directionalLight = static_cast<const scene::DirectionalLight *>(light);
cc::Mat4 matShadowCamera;
float x;
float y;
float nearClamp;
float farClamp;
if (shadowInfo->autoAdapt) {
Vec3 tmpCenter;
getShadowWorldMatrix(sphere, directionalLight->getNode()->getWorldRotation(), directionalLight->getDirection(), &matShadowCamera, &tmpCenter);
const auto radius = sphere->getRadius();
x = radius;
y = radius;
const float halfFar = tmpCenter.distance(sphere->getCenter());
farClamp = std::min(halfFar * COEFFICIENT_OF_EXPANSION, SHADOW_CAMERA_MAX_FAR);
Mat4 matShadowView;
Mat4 matShadowProj;
Mat4 matShadowViewProj;
if (!shadowInfo->fixedArea) {
nearClamp = 0.1F;
farClamp = sceneData->getShadowCameraFar();
matShadowView = sceneData->getMatShadowView();
matShadowProj = sceneData->getMatShadowProj();
matShadowViewProj = sceneData->getMatShadowViewProj();
} else {
matShadowCamera = light->getNode()->getWorldMatrix();
x = shadowInfo->orthoSize;
y = shadowInfo->orthoSize;
farClamp = shadowInfo->farValue;
auto *const node = light->getNode();
matShadowView = node->getWorldMatrix().getInversed();
const float x = shadowInfo->orthoSize;
const float y = shadowInfo->orthoSize;
nearClamp = shadowInfo->nearValue;
farClamp = shadowInfo->farValue;
const float projectionSinY = device->getCapabilities().clipSpaceSignY;
const float clipSpaceMinZ = device->getCapabilities().clipSpaceMinZ;
Mat4::createOrthographicOffCenter(-x, x, -y, y, -nearClamp,
farClamp, clipSpaceMinZ, projectionSinY, &matShadowProj);
matShadowViewProj = matShadowProj * matShadowView;
}
const auto matShadowView = matShadowCamera.getInversed();
memcpy(shadowUBO.data() + UBOShadow::MAT_LIGHT_VIEW_OFFSET, matShadowView.m, sizeof(matShadowView));
const auto projectionSinY = device->getCapabilities().clipSpaceSignY;
Mat4::createOrthographicOffCenter(-x, x, -y, y, shadowInfo->nearValue, farClamp, device->getCapabilities().clipSpaceMinZ, projectionSinY, &matShadowViewProj);
float shadowProjDepthInfos[4] = {matShadowProj.m[10], matShadowProj.m[14], matShadowProj.m[11], matShadowProj.m[15]};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_PROJ_DEPTH_INFO_OFFSET, &shadowProjDepthInfos, sizeof(shadowProjDepthInfos));
float shadowProjInfos[4] = {matShadowProj.m[00], matShadowProj.m[05], 1.0F / matShadowProj.m[00], 1.0F / matShadowProj.m[05]};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_PROJ_INFO_OFFSET, &shadowProjInfos, sizeof(shadowProjInfos));
matShadowViewProj.multiply(matShadowView);
memcpy(shadowUBO.data() + UBOShadow::MAT_LIGHT_VIEW_PROJ_OFFSET, matShadowViewProj.m, sizeof(matShadowViewProj));
float shadowNFLSInfos[4] = {shadowInfo->nearValue, farClamp, linear, 1.0F - shadowInfo->saturation};
float shadowNFLSInfos[4] = {nearClamp, farClamp, linear, 1.0F - shadowInfo->saturation};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET, &shadowNFLSInfos, sizeof(shadowNFLSInfos));
float shadowLPNNInfos[4] = {0.0F, packing, shadowInfo->normalBias, 0.0F};
@@ -287,26 +279,28 @@ void PipelineUBO::updateShadowUBOLightView(const RenderPipeline *pipeline, std::
const auto matShadowView = matShadowCamera.getInversed();
memcpy(shadowUBO.data() + UBOShadow::MAT_LIGHT_VIEW_OFFSET, matShadowView.m, sizeof(matShadowView));
cc::Mat4::createPerspective(spotLight->getSpotAngle(), spotLight->getAspect(), 0.001F, spotLight->getRange(), &matShadowViewProj);
Mat4::createPerspective(spotLight->getSpotAngle(), spotLight->getAspect(), 0.001F, spotLight->getRange(), &matShadowViewProj);
matShadowViewProj.multiply(matShadowView);
memcpy(shadowUBO.data() + UBOShadow::MAT_LIGHT_VIEW_PROJ_OFFSET, matShadowViewProj.m, sizeof(matShadowViewProj));
float shadowNFLSInfos[4] = {0.01F, spotLight->getRange(), linear, 1.0F - shadowInfo->saturation};
const float shadowNFLSInfos[4] = {0.01F, spotLight->getRange(), linear, 1.0F - shadowInfo->saturation};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET, &shadowNFLSInfos, sizeof(shadowNFLSInfos));
float shadowLPNNInfos[4] = {1.0F, packing, shadowInfo->normalBias, 0.0F};
const float shadowLPNNInfos[4] = {1.0F, packing, shadowInfo->normalBias, 0.0F};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET, &shadowLPNNInfos, sizeof(shadowLPNNInfos));
} break;
case scene::LightType::SPHERE: break;
case scene::LightType::UNKNOWN: break;
default:
break;
}
float shadowWHPBInfos[4] = {shadowInfo->size.x, shadowInfo->size.y, static_cast<float>(shadowInfo->pcfType), shadowInfo->bias};
const float shadowWHPBInfos[4] = {shadowInfo->size.x, shadowInfo->size.y, static_cast<float>(shadowInfo->pcfType), shadowInfo->bias};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET, &shadowWHPBInfos, sizeof(shadowWHPBInfos));
const float color[4] = {shadowInfo->color.x, shadowInfo->color.y, shadowInfo->color.z, shadowInfo->color.w};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_COLOR_OFFSET, &color, sizeof(float) * 4);
memcpy(shadowUBO.data() + UBOShadow::SHADOW_COLOR_OFFSET, &color, sizeof(color));
}
static uint8_t combineSignY = 0;
@@ -368,7 +362,7 @@ void PipelineUBO::activate(gfx::Device *device, RenderPipeline *pipeline) {
void PipelineUBO::destroy() {
for (auto &ubo : _ubos) {
CC_SAFE_DESTROY(ubo);
CC_SAFE_DESTROY(ubo)
}
_ubos.clear();
}
@@ -385,18 +379,14 @@ void PipelineUBO::updateGlobalUBO() {
}
void PipelineUBO::updateCameraUBO(const scene::Camera *camera) {
auto *const globalDSManager = _pipeline->getGlobalDSManager();
auto *const ds = _pipeline->getDescriptorSet();
auto *const cmdBuffer = _pipeline->getCommandBuffers()[0];
PipelineUBO::updateCameraUBOView(_pipeline, _cameraUBOs.data(), camera);
cmdBuffer->updateBuffer(_cameraBuffer, _cameraUBOs.data());
}
void PipelineUBO::updateMultiCameraUBO(const vector<scene::Camera *> &cameras) {
auto *const ds = _pipeline->getDescriptorSet();
auto * device = _pipeline->getDevice();
auto cameraCount = cameras.size();
auto totalUboSize = static_cast<uint>(_alignedCameraUBOSize * cameraCount);
const auto cameraCount = cameras.size();
const auto totalUboSize = static_cast<uint>(_alignedCameraUBOSize * cameraCount);
if (_cameraBuffer->getSize() < totalUboSize) {
_cameraBuffer->resize(totalUboSize);
@@ -404,8 +394,8 @@ void PipelineUBO::updateMultiCameraUBO(const vector<scene::Camera *> &cameras) {
}
for (uint cameraIdx = 0; cameraIdx < cameraCount; ++cameraIdx) {
auto *camera = cameras[cameraIdx];
auto offset = cameraIdx * _alignedCameraUBOSize / sizeof(float);
const auto *camera = cameras[cameraIdx];
const auto offset = cameraIdx * _alignedCameraUBOSize / sizeof(float);
PipelineUBO::updateCameraUBOView(_pipeline, &_cameraUBOs[offset], camera);
}
_cameraBuffer->update(_cameraUBOs.data());
@@ -416,8 +406,8 @@ void PipelineUBO::updateMultiCameraUBO(const vector<scene::Camera *> &cameras) {
void PipelineUBO::updateShadowUBO(const scene::Camera *camera) {
auto *const ds = _pipeline->getDescriptorSet();
auto *const cmdBuffer = _pipeline->getCommandBuffers()[0];
auto *const sceneData = _pipeline->getPipelineSceneData();
auto *const shadowInfo = sceneData->getSharedData()->shadow;
const auto * sceneData = _pipeline->getPipelineSceneData();
const auto * shadowInfo = sceneData->getSharedData()->shadow;
const auto *const scene = camera->scene;
if (!shadowInfo->enabled) return;
@@ -437,7 +427,7 @@ void PipelineUBO::updateShadowUBO(const scene::Camera *camera) {
}
void PipelineUBO::updateShadowUBOLight(const scene::Light *light) {
auto *const ds = _pipeline->getDescriptorSet();
const auto *ds = _pipeline->getDescriptorSet();
auto *const cmdBuffer = _pipeline->getCommandBuffers()[0];
PipelineUBO::updateShadowUBOLightView(_pipeline, &_shadowUBO, light);
cmdBuffer->updateBuffer(ds->getBuffer(UBOShadow::BINDING), _shadowUBO.data(), UBOShadow::SIZE);
+153 -27
View File
@@ -29,8 +29,7 @@
#include "Define.h"
#include "RenderPipeline.h"
#include "SceneCulling.h"
#include "gfx-base/GFXBuffer.h"
#include "gfx-base/GFXDescriptorSet.h"
#include "gfx-base/GFXDevice.h"
#include "math/Quaternion.h"
#include "platform/Application.h"
#include "scene/AABB.h"
@@ -38,16 +37,15 @@
#include "scene/RenderScene.h"
#include "scene/Sphere.h"
#include "scene/SpotLight.h"
#include "scene/Frustum.h"
namespace cc {
namespace pipeline {
bool castBoundsInitialized = false;
scene::AABB castWorldBounds;
RenderObject genRenderObject(const scene::Model *model, const scene::Camera *camera) {
float depth = 0;
if (model->getNode()) {
auto *const node = model->getTransform();
const auto *node = model->getTransform();
cc::Vec3 position;
cc::Vec3::subtract(node->getWorldPosition(), camera->position, &position);
depth = position.dot(camera->forward);
@@ -68,7 +66,7 @@ void getShadowWorldMatrix(const scene::Sphere *sphere, const cc::Quaternion &rot
}
void updateSphereLight(scene::Shadow *shadows, const scene::Light *light, std::array<float, UBOShadow::COUNT> *shadowUBO) {
auto *const node = light->getNode();
const auto *node = light->getNode();
if (!node->getFlagsChanged() && !shadows->dirty) {
return;
}
@@ -106,9 +104,9 @@ void updateSphereLight(scene::Shadow *shadows, const scene::Light *light, std::a
}
void updateDirLight(scene::Shadow *shadows, const scene::Light *light, std::array<float, UBOShadow::COUNT> *shadowUBO) {
auto *const node = light->getNode();
const auto * node = light->getNode();
const auto &rotation = node->getWorldRotation();
Quaternion qt(rotation.x, rotation.y, rotation.z, rotation.w);
const Quaternion qt(rotation.x, rotation.y, rotation.z, rotation.w);
Vec3 forward(0, 0, -1.0F);
forward.transformQuat(qt);
const auto &normal = shadows->normal;
@@ -140,8 +138,8 @@ void updateDirLight(scene::Shadow *shadows, const scene::Light *light, std::arra
matLight.m[15] = 1;
memcpy(shadowUBO->data() + UBOShadow::MAT_LIGHT_PLANE_PROJ_OFFSET, matLight.m, sizeof(matLight));
float color[4] = {shadows->color.x, shadows->color.y, shadows->color.z, shadows->color.w};
memcpy(shadowUBO->data() + UBOShadow::SHADOW_COLOR_OFFSET, &color, sizeof(float) * 4);
const float color[4] = {shadows->color.x, shadows->color.y, shadows->color.z, shadows->color.w};
memcpy(shadowUBO->data() + UBOShadow::SHADOW_COLOR_OFFSET, &color, sizeof(color));
}
void lightCollecting(scene::Camera *camera, std::vector<const scene::Light *> *validLights) {
@@ -159,21 +157,153 @@ void lightCollecting(scene::Camera *camera, std::vector<const scene::Light *> *v
}
}
CC_SAFE_DELETE(sphere);
CC_SAFE_DELETE(sphere)
}
void sceneCulling(RenderPipeline *pipeline, scene::Camera *camera) {
auto *const sceneData = pipeline->getPipelineSceneData();
auto *const sharedData = sceneData->getSharedData();
auto *const shadows = sharedData->shadow;
auto *const skyBox = sharedData->skybox;
const auto *const scene = camera->scene;
Mat4 getCameraWorldMatrix(const scene::Camera* camera) {
Mat4 out;
if (!camera || !camera->node) {
return out;
}
const scene::Node *cameraNode = camera->node;
const Vec3 & position = cameraNode->getWorldPosition();
const Quaternion & rotation = cameraNode->getWorldRotation();
Mat4::fromRT(rotation, position, &out);
out.m[8] *= -1.0F;
out.m[9] *= -1.0F;
out.m[10] *= -1.0F;
return out;
}
void updateDirFrustum(const scene::Sphere *cameraBoundingSphere, const Quaternion &rotation, float range, scene::Frustum *dirLightFrustum) {
const float radius = cameraBoundingSphere->getRadius();
const Vec3 &position = cameraBoundingSphere->getCenter();
Mat4 matWorldTrans;
Mat4::fromRT(rotation, position, &matWorldTrans);
matWorldTrans.m[8] *= -1.0F;
matWorldTrans.m[9] *= -1.0F;
matWorldTrans.m[10] *= -1.0F;
dirLightFrustum->createOrtho(radius, radius, -range, radius, matWorldTrans);
}
void quantizeDirLightShadowCamera(RenderPipeline *pipeline, const scene::Camera *camera, scene::Frustum *out) {
const gfx::Device * device = gfx::Device::getInstance();
PipelineSceneData *const sceneData = pipeline->getPipelineSceneData();
const scene::PipelineSharedSceneData *sharedData = sceneData->getSharedData();
const scene::Shadow * shadowInfo = sharedData->shadow;
const scene::RenderScene *const scene = camera->scene;
const scene::DirectionalLight * mainLight = scene->getMainLight();
const float invisibleOcclusionRange = shadowInfo->invisibleOcclusionRange;
const float shadowMapWidth = shadowInfo->size.x;
const auto * node = mainLight->getNode();
const Quaternion & rotation = node->getWorldRotation();
// Raw data.
const Mat4 matWorldTrans = getCameraWorldMatrix(camera);
scene::Frustum validFrustum;
validFrustum.type = scene::ShapeEnums::SHAPE_FRUSTUM_ACCURATE;
validFrustum.split(0.1F, shadowInfo->shadowDistance, camera->aspect, camera->fov, matWorldTrans);
scene::Frustum lightViewFrustum = validFrustum.clone();
// view matrix with range back.
Mat4 matShadowTrans;
Mat4::fromRT(rotation, Vec3::ZERO, &matShadowTrans);
Mat4 matShadowView = matShadowTrans.getInversed();
Mat4 matShadowViewInv = matShadowView.getInversed();
const Mat4 shadowViewArbitraryPos = matShadowView.clone();
lightViewFrustum.transform(matShadowView);
// bounding box in light space.
scene::AABB castLightViewBounds;
scene::AABB::fromPoints(Vec3(10000000.0F, 10000000.0F, 10000000.0F), Vec3(-10000000.0F, -10000000.0F, -10000000.0F), &castLightViewBounds);
castLightViewBounds.merge(lightViewFrustum);
const float r = castLightViewBounds.getHalfExtents().z * 2.0F;
Vec3 shadowPos(castLightViewBounds.getCenter().x, castLightViewBounds.getCenter().y,
castLightViewBounds.getCenter().z + castLightViewBounds.getHalfExtents().z + invisibleOcclusionRange);
shadowPos.transformMat4(shadowPos, matShadowViewInv);
Mat4::fromRT(rotation, shadowPos, &matShadowTrans);
matShadowView = matShadowTrans.getInversed();
matShadowViewInv = matShadowView.getInversed();
// calculate projection matrix params.
// min value may lead to some shadow leaks.
const float orthoSizeMin = validFrustum.vertices[0].distance(validFrustum.vertices[6]);
// max value is accurate but poor usage for shadowMap
scene::Sphere cameraBoundingSphere;
cameraBoundingSphere.setCenter(Vec3(0.0F, 0.0F, 0.0F));
cameraBoundingSphere.setRadius(-1.0F);
cameraBoundingSphere.merge(validFrustum);
const float orthoSizeMax = cameraBoundingSphere.getRadius() * 2.0F;
// use lerp(min, accurate_max) to save shadowMap usage
const float orthoSize = orthoSizeMin * 0.8F + orthoSizeMax * 0.2F;
sceneData->setShadowCameraFar(r + invisibleOcclusionRange);
// snap to whole texels
const float halfOrthoSize = orthoSize * 0.5F;
Mat4 matShadowProj;
const float projectionSinY = device->getCapabilities().clipSpaceSignY;
const float clipSpaceMinZ = device->getCapabilities().clipSpaceMinZ;
Mat4::createOrthographicOffCenter(-halfOrthoSize, halfOrthoSize, -halfOrthoSize, halfOrthoSize, 0.1F, sceneData->getShadowCameraFar(),
clipSpaceMinZ, projectionSinY, &matShadowProj);
if (shadowMapWidth > 0.0F) {
const Mat4 matShadowViewProjArbitraryPos = matShadowProj * shadowViewArbitraryPos;
Vec3 projPos;
projPos.transformMat4(shadowPos, matShadowViewProjArbitraryPos);
const float invActualSize = 2.0F / shadowMapWidth;
const Vec2 texelSize(invActualSize, invActualSize);
const float modX = fmodf(projPos.x, texelSize.x);
const float modY = fmodf(projPos.y, texelSize.y);
const Vec3 projSnap(projPos.x - modX, projPos.y - modY, projPos.z);
const Mat4 matShadowViewProjArbitaryPosInv = matShadowViewProjArbitraryPos.getInversed();
Vec3 snap;
snap.transformMat4(projSnap, matShadowViewProjArbitaryPosInv);
Mat4::fromRT(rotation, snap, &matShadowTrans);
matShadowView = matShadowTrans.getInversed();
matShadowViewInv = matShadowView.getInversed();
out->createOrtho(orthoSize, orthoSize, 0.1F, sceneData->getShadowCameraFar(), matShadowViewInv);
} else {
for (uint i = 0; i < 8; i++) {
out->vertices[i].setZero();
}
out->updatePlanes();
}
const Mat4 matShadowViewProj = matShadowProj * matShadowView;
sceneData->setMatShadowView(matShadowView);
sceneData->setMatShadowProj(matShadowProj);
sceneData->setMatShadowViewProj(matShadowViewProj);
}
void sceneCulling(RenderPipeline *pipeline, scene::Camera *camera) {
PipelineSceneData *const sceneData = pipeline->getPipelineSceneData();
const scene::PipelineSharedSceneData *sharedData = sceneData->getSharedData();
const scene::Shadow * shadowInfo = sharedData->shadow;
const scene::Skybox * skyBox = sharedData->skybox;
const scene::RenderScene *const scene = camera->scene;
const scene::DirectionalLight * mainLight = scene->getMainLight();
scene::Frustum dirLightFrustum;
castBoundsInitialized = false;
RenderObjectList shadowObjects;
bool isShadowMap = false;
if (shadows->enabled && shadows->shadowType == scene::ShadowType::SHADOWMAP) {
if (shadowInfo->enabled && shadowInfo->shadowType == scene::ShadowType::SHADOWMAP) {
isShadowMap = true;
// update dirLightFrustum
if (mainLight && mainLight->getNode()) {
quantizeDirLightShadowCamera(pipeline, camera, &dirLightFrustum);
} else {
for (Vec3 &vertex : dirLightFrustum.vertices) {
vertex.setZero();
}
dirLightFrustum.updatePlanes();
}
}
RenderObjectList renderObjects;
@@ -182,7 +312,7 @@ void sceneCulling(RenderPipeline *pipeline, scene::Camera *camera) {
renderObjects.emplace_back(genRenderObject(skyBox->model, camera));
}
for (auto *model : scene->getModels()) {
for (const auto *model : scene->getModels()) {
// filter model by view visibility
if (model->getEnabled()) {
const auto visibility = camera->visibility;
@@ -192,12 +322,9 @@ void sceneCulling(RenderPipeline *pipeline, scene::Camera *camera) {
// shadow render Object
const auto *modelWorldBounds = model->getWorldBounds();
if (isShadowMap && model->getCastShadow() && modelWorldBounds) {
if (!castBoundsInitialized) {
castWorldBounds.set(modelWorldBounds->getCenter(), modelWorldBounds->getHalfExtents());
castBoundsInitialized = true;
}
castWorldBounds.merge(*modelWorldBounds);
shadowObjects.emplace_back(genRenderObject(model, camera));
if (!modelWorldBounds->aabbFrustum(dirLightFrustum)){
shadowObjects.emplace_back(genRenderObject(model, camera));
}
}
// frustum culling
if (modelWorldBounds && !modelWorldBounds->aabbFrustum(camera->frustum)) {
@@ -210,7 +337,6 @@ void sceneCulling(RenderPipeline *pipeline, scene::Camera *camera) {
}
if (isShadowMap) {
sceneData->getSphere()->define(castWorldBounds);
sceneData->setShadowObjects(std::move(shadowObjects));
}
@@ -29,6 +29,7 @@
#include "scene/Define.h"
#include "scene/Light.h"
#include "scene/Sphere.h"
#include "scene/Frustum.h"
namespace cc {
class Mat4;
@@ -40,11 +41,13 @@ struct RenderObject;
class RenderPipeline;
RenderObject genRenderObject(const scene::Model *, const scene::Camera *);
void lightCollecting(scene::Camera *, std::vector<const scene::Light *> *);
void sceneCulling(RenderPipeline *, scene::Camera *);
void updateSphereLight(scene::Shadow *shadows, const scene::Light *light, std::array<float, UBOShadow::COUNT> *);
void updateDirLight(scene::Shadow *shadows, const scene::Light *light, std::array<float, UBOShadow::COUNT> *);
void getShadowWorldMatrix(const scene::Sphere *sphere, const cc::Quaternion &rotation, const cc::Vec3 &dir, cc::Mat4 *shadowWorldMat, cc::Vec3 *out);
void quantizeDirLightShadowCamera(RenderPipeline *pipeline, const scene::Camera *camera, scene::Frustum *out);
void lightCollecting(scene::Camera *, std::vector<const scene::Light *> *);
void sceneCulling(RenderPipeline *, scene::Camera *);
void updateSphereLight(scene::Shadow *shadows, const scene::Light *light, std::array<float, UBOShadow::COUNT> *);
void updateDirLight(scene::Shadow *shadows, const scene::Light *light, std::array<float, UBOShadow::COUNT> *);
void getShadowWorldMatrix(const scene::Sphere *sphere, const cc::Quaternion &rotation, const cc::Vec3 &dir, cc::Mat4 *shadowWorldMat, cc::Vec3 *out);
Mat4 getCameraWorldMatrix(const scene::Camera *camera);
void updateDirFrustum(const scene::Sphere *cameraBoundingSphere, const Quaternion &rotation, float range, scene::Frustum *dirLightFrustum);
} // namespace pipeline
} // namespace cc
@@ -72,8 +72,16 @@ void ShadowMapBatchedQueue::gatherLightPasses(const scene::Light *light, gfx::Co
add(model, cmdBuffer);
}
} break;
default:
break;
case scene::LightType::SPHERE: {
} break;
case scene::LightType::UNKNOWN: {
} break;
default: {
} break;
}
}
}
@@ -137,9 +145,9 @@ void ShadowMapBatchedQueue::recordCommandBuffer(gfx::Device *device, gfx::Render
}
void ShadowMapBatchedQueue::destroy() {
CC_SAFE_DELETE(_batchedQueue);
CC_SAFE_DELETE(_batchedQueue)
CC_SAFE_DELETE(_instancedQueue);
CC_SAFE_DELETE(_instancedQueue)
_buffer = nullptr;
}
@@ -29,6 +29,7 @@
#include "scene/Light.h"
#include "scene/Model.h"
#include "scene/SubModel.h"
#include "scene/Camera.h"
namespace cc {
namespace pipeline {
@@ -276,7 +276,7 @@ void LightingStage::activate(RenderPipeline *pipeline, RenderFlow *flow) {
_reflectionComp = new ReflectionComp();
gfx::ColorAttachment cAttch = {
gfx::Format::RGBA16F,
gfx::Format::RGBA8,
gfx::SampleCount::X1,
gfx::LoadOp::LOAD,
gfx::StoreOp::STORE,
+42 -6
View File
@@ -25,6 +25,8 @@
#include "AABB.h"
#include "base/TypeDef.h"
namespace cc {
namespace scene {
bool AABB::aabbAabb(AABB *aabb) const {
@@ -74,21 +76,55 @@ void AABB::getBoundary(cc::Vec3 *minPos, cc::Vec3 *maxPos) const {
}
void AABB::merge(const AABB &aabb) {
cc::Vec3 minA = getCenter() - getHalfExtents();
cc::Vec3 minB = aabb.getCenter() - aabb.getHalfExtents();
cc::Vec3 maxA = getCenter() + getHalfExtents();
cc::Vec3 maxB = aabb.getCenter() + aabb.getHalfExtents();
const cc::Vec3 minA = getCenter() - getHalfExtents();
const cc::Vec3 minB = aabb.getCenter() - aabb.getHalfExtents();
const cc::Vec3 maxA = getCenter() + getHalfExtents();
const cc::Vec3 maxB = aabb.getCenter() + aabb.getHalfExtents();
cc::Vec3 maxP;
cc::Vec3 minP;
cc::Vec3::max(maxA, maxB, &maxP);
cc::Vec3::min(minA, minB, &minP);
cc::Vec3 addP = maxP + minP;
cc::Vec3 subP = maxP - minP;
const cc::Vec3 addP = maxP + minP;
const cc::Vec3 subP = maxP - minP;
setCenter(addP * 0.5F);
setHalfExtents(subP * 0.5F);
}
void AABB::merge(const cc::Vec3& point) {
cc::Vec3 minPos = getCenter() - getHalfExtents();
cc::Vec3 maxPos = getCenter() + getHalfExtents();
if (point.x < minPos.x) {
minPos.x = point.x;
}
if (point.y < minPos.y) {
minPos.y = point.y;
}
if (point.z < minPos.z) {
minPos.z = point.z;
}
if (point.x > maxPos.x) {
maxPos.x = point.x;
}
if (point.y > maxPos.y) {
maxPos.y = point.y;
}
if (point.z > maxPos.z) {
maxPos.z = point.z;
}
const Vec3 center = (minPos + maxPos) * 0.5F;
setCenter(center);
setHalfExtents(maxPos.x - center.x, maxPos.y - center.y, maxPos.z - center.z);
}
void AABB::merge(const Frustum& frustum) {
const std::array<Vec3, 8> &vertices = frustum.vertices;
for (uint i = 0; i < vertices.max_size(); ++i) {
merge(vertices[i]);
}
}
void AABB::set(const cc::Vec3 &centerVal, const cc::Vec3 &halfExtentVal) {
setCenter(centerVal);
setHalfExtents(halfExtentVal);
+2
View File
@@ -55,6 +55,8 @@ public:
int aabbPlane(const Plane &) const;
void getBoundary(cc::Vec3 *minPos, cc::Vec3 *maxPos) const;
void merge(const AABB &aabb);
void merge(const cc::Vec3 &point);
void merge(const Frustum &frustum);
void set(const cc::Vec3 &centerVal, const cc::Vec3 &halfExtentVal);
void transform(const Mat4 &m, AABB *out) const;
inline void setCenter(float x, float y, float z) { _aabbLayout->center.set(x, y, z); }
+4 -2
View File
@@ -44,8 +44,10 @@ struct Camera final {
uint32_t width{0};
uint32_t height{0};
uint32_t clearFlag{0};
float exposure{0};
float clearDepth{0};
float exposure{0.0F};
float clearDepth{0.0F};
float fov{0.0F};
float aspect{0.0F};
Vec4 viewPort;
uint32_t clearStencil{0};
uint32_t visibility{0};
+3 -1
View File
@@ -69,8 +69,10 @@ struct Shadow {
bool enabled{false};
bool dirty{false};
bool shadowMapDirty{false};
bool autoAdapt{false};
bool fixedArea{false};
ShadowType shadowType{ShadowType::PLANAR};
float invisibleOcclusionRange{0.0F};
float shadowDistance{0.0F};
float distance{0.0F};
Pass * instancePass{nullptr};
Pass * planarPass{nullptr};
+106
View File
@@ -39,6 +39,85 @@ const std::vector<cc::Vec3> VEC_VALS{
{-1, -1, -1},
{1, -1, -1}};
} // namespace
// Define from 3 vertices.
void Plane::define(const Vec3 &v0, const Vec3 &v1, const Vec3 &v2) {
const Vec3 dist1 = v1 - v0;
const Vec3 dist2 = v2 - v0;
Vec3 dist;
Vec3::crossProduct(dist1, dist2, &dist);
define(dist, v0);
}
// Define from a normal vector and a point on the plane.
void Plane::define(const Vec3 &normal, const Vec3 &point) {
n = normal.getNormalized();
d = -normal.dot(point);
}
// Return signed distance to a point.
float Plane::distance(const Vec3 &point) const {
return n.dot(point) + d;
}
Plane Plane::clone() const {
Plane plane;
plane.n.set(n);
plane.d = d;
return plane;
}
void Frustum::createOrtho(const float width, const float height, const float near, const float far, const Mat4& transform) {
const float halfWidth = width * 0.5F;
const float halfHeight = height * 0.5F;
vertices[0].transformMat4( Vec3(halfWidth, halfHeight, near), transform);
vertices[1].transformMat4(Vec3(-halfWidth, halfHeight, near), transform);
vertices[2].transformMat4(Vec3(-halfWidth, -halfHeight, near), transform);
vertices[3].transformMat4(Vec3(halfWidth, -halfHeight, near), transform);
vertices[4].transformMat4(Vec3(halfWidth, halfHeight, far), transform);
vertices[5].transformMat4(Vec3(-halfWidth, halfHeight, far), transform);
vertices[6].transformMat4(Vec3(-halfWidth, -halfHeight, far), transform);
vertices[7].transformMat4(Vec3(halfWidth, -halfHeight, far), transform);
updatePlanes();
}
void Frustum::split(float start, float end, float aspect, float fov, const Mat4& transform) {
const float h = tanf(fov * 0.5F);
const float w = h * aspect;
const Vec3 nearTemp(start * w, start * h, start);
const Vec3 farTemp(end * w, end * h, end);
vertices[0].transformMat4(Vec3(nearTemp.x, nearTemp.y, nearTemp.z), transform);
vertices[1].transformMat4(Vec3(-nearTemp.x, nearTemp.y, nearTemp.z), transform);
vertices[2].transformMat4(Vec3(-nearTemp.x, -nearTemp.y, nearTemp.z), transform);
vertices[3].transformMat4(Vec3(nearTemp.x, -nearTemp.y, nearTemp.z), transform);
vertices[4].transformMat4(Vec3(farTemp.x, farTemp.y, farTemp.z), transform);
vertices[5].transformMat4(Vec3(-farTemp.x, farTemp.y, farTemp.z), transform);
vertices[6].transformMat4(Vec3(-farTemp.x, -farTemp.y, farTemp.z), transform);
vertices[7].transformMat4(Vec3(farTemp.x, -farTemp.y, farTemp.z), transform);
updatePlanes();
}
void Frustum::updatePlanes() {
// left plane
planes[0].define(vertices[1], vertices[5], vertices[6]);
// right plane
planes[1].define(vertices[3], vertices[7], vertices[4]);
// bottom plane
planes[2].define(vertices[6], vertices[7], vertices[3]);
// top plane
planes[3].define(vertices[0], vertices[4], vertices[5]);
// near plane
planes[4].define(vertices[2], vertices[3], vertices[0]);
// far plane
planes[5].define(vertices[7], vertices[6], vertices[5]);
}
void Frustum::update(const Mat4 &m, const Mat4 &inv) {
// left plane
planes[0].n.set(m.m[3] + m.m[0], m.m[7] + m.m[4], m.m[11] + m.m[8]);
@@ -74,5 +153,32 @@ void Frustum::update(const Mat4 &m, const Mat4 &inv) {
i++;
}
}
Frustum Frustum::clone() {
Frustum frustum;
for (uint i = 0; i < planes.size(); ++i) {
frustum.planes[i] = planes[i].clone();
}
frustum.type = type;
for (uint i = 0; i < vertices.size(); ++i) {
frustum.vertices[i].set(vertices[i]);
}
return frustum;
}
void Frustum::transform(const Mat4 &transform) {
vertices[0].transformMat4(vertices[0], transform);
vertices[1].transformMat4(vertices[1], transform);
vertices[2].transformMat4(vertices[2], transform);
vertices[3].transformMat4(vertices[3], transform);
vertices[4].transformMat4(vertices[4], transform);
vertices[5].transformMat4(vertices[5], transform);
vertices[6].transformMat4(vertices[6], transform);
vertices[7].transformMat4(vertices[7], transform);
}
} // namespace scene
} // namespace cc
+14 -2
View File
@@ -42,16 +42,28 @@ enum class ShapeEnums {
SHAPE_FRUSTUM_ACCURATE = (1 << 8),
SHAPE_CAPSULE = (1 << 9),
};
struct Plane final {
float d{0.F};
Vec3 n;
void define(const Vec3 &v0, const Vec3 &v1, const Vec3 &v2);
void define(const Vec3 &normal, const Vec3 &point);
float distance(const Vec3 &point) const;
Plane clone() const;
};
struct Frustum final {
std::array<Vec3, 8> vertices;
std::array<Plane, 6> planes;
void update(const Mat4 &m, const Mat4 &inv);
ShapeEnums type{ShapeEnums::SHAPE_FRUSTUM};
void createOrtho(float width, float height, float near, float far, const Mat4 &transform);
void split(float start, float end, float aspect, float fov, const Mat4 &transform);
void updatePlanes();
void update(const Mat4 &m, const Mat4 &inv);
Frustum clone();
void transform(const Mat4 &transform);
ShapeEnums type{ShapeEnums::SHAPE_FRUSTUM};
};
} // namespace scene
+17 -11
View File
@@ -23,8 +23,9 @@
THE SOFTWARE.
****************************************************************************/
#include "scene/Sphere.h"
#include <algorithm>
#include "scene/Sphere.h"
#include "base/TypeDef.h"
namespace cc {
namespace scene {
@@ -52,18 +53,16 @@ bool Sphere::interset(const Frustum &frustum) const {
[self](const Plane &plane) { return self->interset(plane) != -1; });
}
void Sphere::mergePoint(const Vec3 &point) {
void Sphere::merge(const Vec3 &point) {
if (_radius < 0.0F) {
_center = point;
_radius = 0.0F;
return;
}
auto offset = point - _center;
auto distance = offset.length();
if (distance > _radius) {
auto half = (distance - _radius) * 0.5F;
Vec3 offset = point - _center;
if (const float distance = offset.length(); distance > _radius) {
const float half = (distance - _radius) * 0.5F;
_radius += half;
offset.scale(half / distance);
_center += offset;
@@ -88,12 +87,12 @@ void Sphere::define(const AABB &aabb) {
_center += (half / dist) * offset;
}
void Sphere::mergeAABB(const AABB *aabb) {
void Sphere::merge(const AABB *aabb) {
cc::Vec3 minPos;
cc::Vec3 maxPos;
aabb->getBoundary(&minPos, &maxPos);
mergePoint(minPos);
mergePoint(maxPos);
merge(minPos);
merge(maxPos);
}
int Sphere::spherePlane(const Plane &plane) {
@@ -108,7 +107,7 @@ int Sphere::spherePlane(const Plane &plane) {
return 1;
}
bool Sphere::sphereFrustum(const Frustum &frustum) {
bool Sphere::sphereFrustum(const Frustum &frustum) const {
const auto &planes = frustum.planes;
const auto *self = this;
return std::all_of(planes.begin(),
@@ -117,5 +116,12 @@ bool Sphere::sphereFrustum(const Frustum &frustum) {
[self](const Plane &plane) { return self->interset(plane) != -1; });
}
void Sphere::merge(const Frustum &frustum) {
const std::array<Vec3, 8> &vertices = frustum.vertices;
for (uint i = 0; i < vertices.max_size(); ++i) {
merge(vertices[i]);
}
}
} // namespace scene
} // namespace cc
+5 -4
View File
@@ -47,15 +47,16 @@ public:
inline void setRadius(float val) { _radius = val; }
void define(const AABB &aabb);
void mergeAABB(const AABB *aabb);
void mergePoint(const Vec3 &point);
void merge(const AABB *aabb);
void merge(const Vec3 &point);
bool interset(const Frustum &frustum) const;
int interset(const Plane &plane) const;
int spherePlane(const Plane &plane);
bool sphereFrustum(const Frustum &frustum);
bool sphereFrustum(const Frustum &frustum) const;
void merge(const Frustum &frustum);
private:
float _radius{0};
float _radius{-1.0F};
Vec3 _center;
};