4#include <QtQuick3DRuntimeRender/private/qssgrenderreflectionprobe_p.h>
5#include <QtQuick3DRuntimeRender/private/qssgrenderlayer_p.h>
6#include <QtQuick3DRuntimeRender/private/qssglayerrenderdata_p.h>
7#include <QtQuick3DRuntimeRender/private/qssgrendercontextcore_p.h>
8#include <QtQuick3DUtils/private/qssgrenderbasetypes_p.h>
15 : m_context(inContext)
27 entry.destroyRhiResources();
29 m_reflectionMapList.
clear();
35 QRhiTexture::Flags
flags = {})
39 qWarning(
"Failed to create reflection map texture of size %dx%d",
size.width(),
size.height());
48 if (!renderBuffer->create())
49 qWarning(
"Failed to build depth-stencil buffer of size %dx%d",
size.width(),
size.height());
66 QSize pixelSize(mapRes, mapRes);
77 pEntry = &m_reflectionMapList.
back();
98 for (
int i = 0;
i < 6; ++
i)
105 for (
int i = 0;
i < 6; ++
i)
124 qWarning(
"Failed to build reflection map render target");
133 mipmapCount =
qMin(mipmapCount, 6);
136 for (
int mipLevel = 0; mipLevel < mipmapCount; ++mipLevel) {
137 const QSize levelSize =
QSize(mapSize.
width() * std::pow(0.5, mipLevel),
138 mapSize.
height() * std::pow(0.5, mipLevel));
151 renderTarget->setDescription(rtDesc);
155 if (!renderTarget->create())
156 qWarning(
"Failed to build prefilter cube map render target");
157 renderTargets << renderTarget;
162 const auto &prefilterShaderStages =
m_context.
shaderCache()->loadBuiltinForRhi(
"reflectionprobeprefilter");
187 { 3 *
sizeof(float) }
198 int uBufSamplesElementSize = rhi->
ubufAligned(uBufSamplesSize);
207 *prefilterShaderStages->vertexStage(),
208 *prefilterShaderStages->fragmentStage()
223 qWarning(
"failed to create pre-filter reflection map pipeline state");
225 const auto &irradianceShaderStages =
m_context.
shaderCache()->loadBuiltinForRhi(
"environmentmapprefilter");
232 *irradianceShaderStages->vertexStage(),
233 *irradianceShaderStages->fragmentStage()
252 qWarning(
"failed to create irradiance reflection map pipeline state");
282 for (
int i = 0;
i < m_reflectionMapList.
size();
i++) {
292 : m_probeIndex(
std::numeric_limits<
quint32>::max())
386 bits = ((
bits & 0x55555555u) << 1u) | ((
bits & 0xAAAAAAAAu) >> 1u);
387 bits = ((
bits & 0x33333333u) << 2u) | ((
bits & 0xCCCCCCCCu) >> 2u);
388 bits = ((
bits & 0x0F0F0F0Fu) << 4u) | ((
bits & 0xF0F0F0F0u) >> 4u);
389 bits = ((
bits & 0x00FF00FFu) << 8u) | ((
bits & 0xFF00FF00u) >> 8u);
390 return float(
bits) * 2.3283064365386963e-10;
400 float a = roughness*roughness;
402 float phi = 2.0f *
M_PI * xi.
x();
403 float cosTheta = sqrt((1.0f - xi.
y()) / (1.0f + (
a*
a - 1.0f) * xi.
y()));
404 float sinTheta = sqrt(1.0f - cosTheta * cosTheta);
407 return QVector3D(cos(phi) * sinTheta, sin(phi) * sinTheta, cosTheta);
412 float a = roughness * roughness;
414 float nDotH2 = nDotH * nDotH;
417 float denom = nDotH2 * (
a2 - 1.0f) + 1.0f;
418 denom =
M_PI * denom * denom;
425 float &invTotalWeight,
uint &sampleCount)
433 const float pdf = D * half.
z() / (4.0f * half.
z()) + 0.0001f;
434 const float saTexel = 4.0f *
M_PI / (6.0f * resolution * resolution);
436 float mipLevel = roughness == 0.0f ? 0.0f : 0.5f *
log2(saSample / saTexel);
439 invTotalWeight += light.
z();
445 invTotalWeight = 1.0f / invTotalWeight;
458 rub->uploadStaticBuffer(vertexBuffer,
cube);
459 cb->resourceUpdate(rub);
466 int uBufSamplesElementSize = rhi->
ubufAligned(uBufSamplesSize);
467 int uBufIrradianceElementSize = rhi->
ubufAligned(20);
475 viewMatrix.
lookAt(eye, center, up);
500 mipmapCount =
qMin(mipmapCount, 6);
502 const float resolution = mapSize.
width();
506 for (
int mipLevel = 0; mipLevel < mipmapCount - 1; ++mipLevel) {
508 const float roughness = float(mipLevel) / float(mipmapCount - 2);
509 float invTotalWeight = 0.0f;
510 uint sampleCount = 0;
512 sampleDirections.
clear();
520 const float roughness = 0.0f;
521 const float lodBias = 0.0f;
522 const int distribution = 0;
523 const int sampleCount = resolution / 4;
532 cb->resourceUpdate(rub);
535 for (
int mipLevel = 0; mipLevel < mipmapCount; ++mipLevel) {
546 if (mipLevel < mipmapCount - 1) {
549 cb->setVertexInput(0, 1, &vbufBinding);
553 { 2,
quint32(uBufSamplesElementSize * mipLevel) }
559 cb->setVertexInput(0, 1, &vbufBinding);
563 { 2,
quint32(uBufIrradianceElementSize) }
592 e.m_probeIndex = probeIdx;
593 e.m_rhiPrefilteredCube = prefiltered;
603 e.m_probeIndex = probeIdx;
605 e.m_rhiPrefilteredCube = prefiltered;
606 e.m_rhiDepthStencil = depthStencil;
static QByteArray number(int, int base=10)
Returns a byte-array representing the whole number n as text.
The QColor class provides colors based on RGB, HSV or CMYK values.
qsizetype size() const noexcept
const_pointer constData() const noexcept
void push_back(parameter_type t)
iterator insert(const Key &key, const T &value)
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
void lookAt(const QVector3D &eye, const QVector3D ¢er, const QVector3D &up)
Multiplies this matrix by a viewing matrix derived from an eye point.
void perspective(float verticalAngle, float aspectRatio, float nearPlane, float farPlane)
Multiplies this matrix by another that applies a perspective projection.
const float * constData() const
Returns a constant pointer to the raw data of this matrix.
virtual bool create()=0
Creates the corresponding native graphics resources.
void setLevel(int level)
Sets the mip level.
void setLayer(int layer)
Sets the layer index.
QPair< QRhiBuffer *, quint32 > VertexInput
Synonym for QPair<QRhiBuffer *, quint32>.
void setCullMode(CullMode mode)
Sets the specified face culling mode.
void setFrontFace(FrontFace f)
Sets the front face mode f.
void setShaderResourceBindings(QRhiShaderResourceBindings *srb)
Associates with srb describing the resource binding layout and the resources (QRhiBuffer,...
void setDepthOp(CompareOp op)
Sets the depth comparison function op.
void setVertexInputLayout(const QRhiVertexInputLayout &layout)
Specifies the vertex input layout.
void setShaderStages(std::initializer_list< QRhiShaderStage > list)
Sets the list of shader stages.
void setRenderPassDescriptor(QRhiRenderPassDescriptor *desc)
Associates with the specified QRhiRenderPassDescriptor desc.
virtual bool create()=0
Creates the corresponding native graphics resources.
Type
Specifies the type of the renderbuffer.
virtual QRhiRenderPassDescriptor * newCompatibleRenderPassDescriptor() const =0
void setRenderPassDescriptor(QRhiRenderPassDescriptor *desc)
Sets the QRhiRenderPassDescriptor desc for use with this render target.
void generateMips(QRhiTexture *tex)
Enqueues a mipmap generation operation for the specified texture tex.
void updateDynamicBuffer(QRhiBuffer *buf, quint32 offset, quint32 size, const void *data)
Enqueues updating a region of a QRhiBuffer buf created with the type QRhiBuffer::Dynamic.
void setName(const QByteArray &name)
Sets a name for the object.
void deleteLater()
When called without a frame being recorded, this function is equivalent to deleting the object.
static QRhiShaderResourceBinding sampledTexture(int binding, StageFlags stage, QRhiTexture *tex, QRhiSampler *sampler)
static QRhiShaderResourceBinding uniformBufferWithDynamicOffset(int binding, StageFlags stage, QRhiBuffer *buf, quint32 size)
void setBindings(std::initializer_list< QRhiShaderResourceBinding > list)
Sets the list of bindings.
void setDepthStencilBuffer(QRhiRenderBuffer *renderBuffer)
Sets the renderBuffer for depth-stencil.
void setColorAttachments(std::initializer_list< QRhiColorAttachment > list)
Sets the list of color attachments.
void setDescription(const QRhiTextureRenderTargetDescription &desc)
Sets the render target description desc.
virtual QRhiRenderPassDescriptor * newCompatibleRenderPassDescriptor()=0
virtual bool create()=0
Creates the corresponding native graphics resources.
Format
Specifies the texture format.
QRhiBuffer * newBuffer(QRhiBuffer::Type type, QRhiBuffer::UsageFlags usage, quint32 size)
int ubufAligned(int v) const
QMatrix4x4 clipSpaceCorrMatrix() const
bool isYUpInFramebuffer() const
QRhiShaderResourceBindings * newShaderResourceBindings()
QRhiRenderBuffer * newRenderBuffer(QRhiRenderBuffer::Type type, const QSize &pixelSize, int sampleCount=1, QRhiRenderBuffer::Flags flags={}, QRhiTexture::Format backingFormatHint=QRhiTexture::UnknownFormat)
static int mipLevelsForSize(const QSize &size)
QRhiTextureRenderTarget * newTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc, QRhiTextureRenderTarget::Flags flags={})
QRhiGraphicsPipeline * newGraphicsPipeline()
QRhiTexture * newTexture(QRhiTexture::Format format, const QSize &pixelSize, int sampleCount=1, QRhiTexture::Flags flags={})
QRhiResourceUpdateBatch * nextResourceUpdateBatch()
static const char * displayName(QSSGRenderTextureCubeFace face)
@ MipModeFollowRenderImage
const std::unique_ptr< QSSGRhiContext > & rhiContext() const
const std::unique_ptr< QSSGBufferManager > & bufferManager() const
const std::unique_ptr< QSSGShaderCache > & shaderCache() const
~QSSGRenderReflectionMap()
QSSGReflectionMapEntry * reflectionMapEntry(int probeIdx)
void releaseCachedResources()
void addTexturedReflectionMapEntry(qint32 probeIdx, const QSSGRenderReflectionProbe &probe)
const QSSGRenderContextInterface & m_context
QSSGRenderReflectionMap(const QSSGRenderContextInterface &inContext)
void addReflectionMapEntry(qint32 probeIdx, const QSSGRenderReflectionProbe &probe)
static QRhiCommandBuffer::BeginPassFlags commonPassFlags()
constexpr int height() const noexcept
Returns the height.
constexpr int width() const noexcept
Returns the width.
QByteArray toLatin1() const &
constexpr size_type size() const noexcept
void resize(qsizetype sz)
const T * constData() const
The QVector2D class represents a vector or vertex in 2D space.
constexpr float y() const noexcept
Returns the y coordinate of this point.
constexpr float x() const noexcept
Returns the x coordinate of this point.
The QVector3D class represents a vector or vertex in 3D space.
void normalize() noexcept
Normalizes the current vector in place.
constexpr float z() const noexcept
Returns the z coordinate of this point.
The QVector4D class represents a vector or vertex in 4D space.
QMap< QString, QString > map
[6]
qDeleteAll(list.begin(), list.end())
Combined button and popup list for selecting options.
#define QByteArrayLiteral(str)
constexpr const T & qMin(const T &a, const T &b)
GLint GLsizei GLsizei height
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei GLsizei GLenum format
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const void * bits
#define Q_QUICK3D_PROFILE_START(Type)
#define Q_QUICK3D_PROFILE_END_WITH_ID(Type, Payload, POID)
#define QSSG_RENDERPASS_NAME(passName, level, face)
#define Q_QUICK3D_PROFILE_END_WITH_STRING(Type, Payload, Str)
#define Q_QUICK3D_PROFILE_ASSIGN_ID(bgnode, obj)
static constexpr QSSGRenderTextureCubeFace QSSGRenderTextureCubeFaces[]
float distributionGGX(float nDotH, float roughness)
static QRhiRenderBuffer * allocateRhiReflectionRenderBuffer(QRhi *rhi, QRhiRenderBuffer::Type type, const QSize &size)
QT_BEGIN_NAMESPACE const int prefilterSampleCount
static QRhiTexture * allocateRhiReflectionTexture(QRhi *rhi, QRhiTexture::Format format, const QSize &size, QRhiTexture::Flags flags={})
void fillPrefilterValues(float roughness, float resolution, QVarLengthArray< QVector4D, prefilterSampleCount > &sampleDirections, float &invTotalWeight, uint &sampleCount)
float radicalInverseVdC(uint bits)
static const float cube[]
QVector3D importanceSampleGGX(QVector2D xi, float roughness)
QVector2D hammersley(uint i, uint N)
#define QSSGRHICTX_STAT(ctx, f)
SSL_CTX int(* cb)(SSL *ssl, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
myFilter draw(painter, QPoint(0, 0), originalPixmap)
QVarLengthArray< QRhiTextureRenderTarget *, 6 > m_rhiRenderTargets
QSSGRenderReflectionProbe::ReflectionTimeSlicing m_timeSlicing
QVarLengthArray< QRhiShaderResourceBindings *, 6 > m_skyBoxSrbs
static QSSGReflectionMapEntry withRhiTexturedCubeMap(quint32 probeIdx, QRhiTexture *preFiltered)
QRhiRenderPassDescriptor * m_rhiRenderPassDesc
static QSSGReflectionMapEntry withRhiCubeMap(quint32 probeIdx, QRhiTexture *cube, QRhiTexture *prefiltered, QRhiRenderBuffer *depthStencil)
QRhiShaderResourceBindings * m_irradianceSrb
QRhiShaderResourceBindings * m_prefilterSrb
QMap< int, QSize > m_prefilterMipLevelSizes
void destroyRhiResources()
void renderMips(QSSGRhiContext *context)
QRhiTexture * m_rhiPrefilteredCube
QRhiRenderPassDescriptor * m_rhiPrefilterRenderPassDesc
QRhiBuffer * m_prefilterFragBuffer
QRhiBuffer * m_prefilterVertBuffer
QRhiGraphicsPipeline * m_irradiancePipeline
QRhiBuffer * m_irradianceFragBuffer
QRhiGraphicsPipeline * m_prefilterPipeline
QMap< int, QVarLengthArray< QRhiTextureRenderTarget *, 6 > > m_rhiPrefilterRenderTargetsMap
QRhiRenderBuffer * m_rhiDepthStencil
QSSGRenderTextureCubeFace m_timeSliceFace
QSSGRenderImage * texture
ReflectionTimeSlicing timeSlicing