Renderer性能优化

main
wuyize 2024-02-26 22:07:04 +08:00
parent fc75668c68
commit d968b585fb
15 changed files with 33 additions and 35 deletions

View File

@ -207,6 +207,7 @@
<None Include="res\Shaders\painting.comp" />
<None Include="res\Shaders\painting.frag" />
<None Include="res\Shaders\painting.vert" />
<None Include="res\Shaders\painting_shadow.frag" />
<None Include="res\Shaders\shader.frag" />
<None Include="res\Shaders\shader.vert" />
<None Include="res\Shaders\shadow_mapping.comp" />

View File

@ -465,6 +465,7 @@
<Filter>Resource Files\Shaders</Filter>
</None>
<None Include="res\Shaders\tone_mapping.comp" />
<None Include="res\Shaders\painting_shadow.frag" />
</ItemGroup>
<ItemGroup>
<QtUic Include="EditorWidgetItem.ui">

View File

@ -33,6 +33,7 @@
<file>Shaders/brdf_lut.comp</file>
<file>Shaders/pageId_downsample.comp</file>
<file>Shaders/tone_mapping.comp</file>
<file>Shaders/painting_shadow.frag</file>
</qresource>
<qresource prefix="/qt/etc">
<file>qt.conf</file>

View File

@ -46,12 +46,15 @@ void main()
if(gBaseColor.a<0.4)
discard;
gPosition = WorldPos;
if(texture_normal_available)
gNormal = getNormalFromMap();
else
gNormal = Normal;
if(!gl_FrontFacing)
gNormal = -gNormal;
gNormal = normalize(gNormal) / 2. + vec3(0.5);
if(texture_metallic_roughness_available)
gMetallicRoughness = texture(texture_metallic_roughness, TexCoords).bg;
else

View File

@ -7,11 +7,7 @@ in vec2 TexCoords;
void main()
{
vec4 baseColor;// = texture(texture_basecolor, TexCoords);
float lod = textureQueryLod(texture_basecolor, TexCoords).x;
while(!sparseTexelsResidentARB(sparseTextureLodARB(texture_basecolor, TexCoords, lod, baseColor))&&lod<textureQueryLevels(texture_basecolor))
lod++;
vec4 baseColor = texture(texture_basecolor, TexCoords);
if(baseColor.a<0.4)
discard;
}

View File

@ -28,12 +28,9 @@ void main()
discard;
//gBaseColor = vec4( vec3(1),1 );
gPosition = WorldPos;
gNormal = normalize(Normal);
//gMetallicRoughness = vec2(0,1);
lod = lodExpect;
gNormal = normalize(Normal) / 2. + vec3(0.5);
vec4 mt;
while(!sparseTexelsResidentARB(sparseTextureLodARB(texture_metallic_roughness, TexCoords, lod, mt))&&lod<levels)
lod++;
sparseTextureLodARB(texture_metallic_roughness, TexCoords, lod, mt);
gMetallicRoughness = mt.rg;
uint w = 1<<(levels-1-lodExpect);

View File

@ -0,0 +1,7 @@
#version 450 core
in vec2 TexCoords;
void main()
{
}

View File

@ -191,7 +191,7 @@ void main()
float roughness = texelFetch(gMetallicRoughness, pixelLocation, 0).g;
vec3 worldPos = texelFetch(gPosition, pixelLocation,0).xyz;
vec3 normal = texelFetch(gNormal, pixelLocation,0).xyz;
vec3 normal = (texelFetch(gNormal, pixelLocation,0).xyz - vec3(0.5)) * 2;
normal = normalize(normal);

View File

@ -11,7 +11,7 @@ Light::Light(Camera* camera)
void Renderer::Light::updateShadowCascadeLevels()
{
shadowCascadeLevels.clear();
const float levelCount = 5;
const float levelCount = 4;
const float lambda = 0.5;
for (int i = 1; i < levelCount; i++)
{

View File

@ -37,12 +37,12 @@ void Model::draw(QOpenGLShaderProgram* shaderProgram, QOpenGLShaderProgram* pain
mesh->draw(paintingProgram);
}
void Model::drawShadow(QOpenGLShaderProgram* shadowProgram)
void Model::drawShadow(QOpenGLShaderProgram* shadowProgram, QOpenGLShaderProgram* paintingShadowProgram)
{
for (auto& mesh : meshes)
mesh->drawShadow(shadowProgram);
for (auto& mesh : paintingMeshes)
mesh->drawShadow(shadowProgram);
mesh->drawShadow(paintingShadowProgram);
}
void Renderer::Model::loadModel(QString path)

View File

@ -13,7 +13,7 @@ namespace Renderer
public:
Model(QOpenGLContext* context, VirtualTextureManager* vtManager);
void draw(QOpenGLShaderProgram* shaderProgram, QOpenGLShaderProgram* paintingProgram);
void drawShadow(QOpenGLShaderProgram* shadowProgram);
void drawShadow(QOpenGLShaderProgram* shadowProgram, QOpenGLShaderProgram* paintingShadowProgram);
void loadModel(QString path);
void unloadModel();
const std::unordered_map<std::string, std::tuple<std::string, glm::vec2, glm::vec2>>& getPaintingMap();

View File

@ -28,8 +28,6 @@ void PaintingMesh::drawShadow(QOpenGLShaderProgram* shadowProgram)
{
if (shadowProgram->bind())
{
glFunc->glBindTextureUnit(0, textureBasecolor);
shadowProgram->setUniformValue("texture_basecolor", 0);
QOpenGLVertexArrayObject::Binder bind(&VAO);
shadowProgram->setUniformValue("model", model);
EBO.bind();

View File

@ -7,16 +7,14 @@ namespace Renderer
ShadowMapPass::ShadowMapPass(GladGLContext* gl, GLuint& shadowFboHandle, int& shadowMapResolution, Light& light, Model*& model)
: RenderPass(gl)
, modelShadowShader(":/Shaders/model_shadow.vert", ":/Shaders/model_shadow.frag", ":/Shaders/model_shadow.geom")
, paintingShadowShader(":/Shaders/model_shadow.vert", ":/Shaders/painting_shadow.frag", ":/Shaders/model_shadow.geom")
, shadowFboHandle(shadowFboHandle), shadowMapResolution(shadowMapResolution), light(light), model(model)
{
modelShadowShader.bind();
gl->GenBuffers(1, &lightSpaceMatricesUBO);
gl->BindBuffer(GL_UNIFORM_BUFFER, lightSpaceMatricesUBO);
gl->BufferData(GL_UNIFORM_BUFFER, sizeof(QMatrix4x4) * 16, nullptr, GL_STATIC_DRAW);
gl->CreateBuffers(1, &lightSpaceMatricesUBO);
gl->NamedBufferData(lightSpaceMatricesUBO, sizeof(QMatrix4x4) * 16, nullptr, GL_STATIC_DRAW);
gl->BindBufferBase(GL_UNIFORM_BUFFER, 0, lightSpaceMatricesUBO);
gl->BindBuffer(GL_UNIFORM_BUFFER, 0);
modelShadowShader.release();
}
void ShadowMapPass::dispatch()
{
const std::vector<QMatrix4x4> lightMatrices = light.getLightSpaceMatrices();
@ -32,7 +30,7 @@ namespace Renderer
gl->Clear(GL_DEPTH_BUFFER_BIT);
//gl->CullFace(GL_FRONT);
if (model != nullptr)
model->drawShadow(&modelShadowShader);
model->drawShadow(&modelShadowShader, &paintingShadowShader);
//gl->CullFace(GL_BACK);
gl->BindFramebuffer(GL_FRAMEBUFFER, 0);

View File

@ -25,7 +25,7 @@ namespace Renderer
ShadowMapPass(GladGLContext* gl, GLuint& shadowFboHandle, int& shadowMapResolution, Light& light, Model*& model);
void dispatch();
private:
Shader modelShadowShader;
Shader modelShadowShader, paintingShadowShader;
GLuint& shadowFboHandle;
int& shadowMapResolution;
Light& light;

View File

@ -207,12 +207,12 @@ void RendererGLWidget::paintGL()
projection = camera.GetProjectionMatrix();
view = camera.GetViewMatrix();
vtManager->commitMutex.lock();
//vtManager->commitMutex.lock();
shadowMapPass->dispatch();
geometryPass->dispatch();
pageIDCallbackPass->dispatch();
gl->Finish();
vtManager->commitMutex.unlock();
//gl->Finish();
//vtManager->commitMutex.unlock();
lightingPass->dispatch();
skyboxPass->dispatch();
toneMappingPass->dispatch();
@ -265,8 +265,6 @@ void RendererGLWidget::resizeGL(int width, int height)
frameHeight = ceil(devicePixelRatioF() * height);
qDebug() << frameWidth << "x" << frameHeight;
camera.Ratio = (float)frameWidth / (float)frameHeight;
//qDebug() << devicePixelRatioF() << width << height;
//gl->Viewport(0, 0, (GLint)devicePixelRatio()*width, (GLint)devicePixelRatio()*height);
if (fboPtr != nullptr)
{
@ -286,7 +284,7 @@ void RendererGLWidget::resizeGL(int width, int height)
gl->GenTextures(9, gbuffers + 1);
//Normal
gl->BindTexture(GL_TEXTURE_2D, gbuffers[1]);
gl->TexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, frameWidth, frameHeight, 0, GL_RGB, GL_FLOAT, NULL);
gl->TexImage2D(GL_TEXTURE_2D, 0, GL_RGB10, frameWidth, frameHeight, 0, GL_RGB, GL_UNSIGNED_INT, NULL);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gl->FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, gbuffers[1], 0);
@ -314,7 +312,7 @@ void RendererGLWidget::resizeGL(int width, int height)
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gl->FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT5, GL_TEXTURE_2D, gbuffers[5], 0);
//Depth, Deprecated
//Depth
gl->BindTexture(GL_TEXTURE_2D, gbuffers[6]);
gl->TexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, frameWidth, frameHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
@ -326,7 +324,6 @@ void RendererGLWidget::resizeGL(int width, int height)
GLenum attachments[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3, GL_COLOR_ATTACHMENT4, GL_COLOR_ATTACHMENT5 };
gl->DrawBuffers(6, attachments);
//gbuffers = fboPtr->textures();
if (gl->CheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
qDebug() << "Framebuffer not complete!";
@ -365,8 +362,7 @@ void RendererGLWidget::resizeGL(int width, int height)
gl->DeleteFramebuffers(1, &shadowFboHandle);
}
//shadowMapResolution = 1.5 * std::max(frameWidth, frameHeight);
shadowMapResolution = 2048;
shadowMapResolution = std::min(2048u, qNextPowerOfTwo(static_cast<int>(std::max(frameWidth, frameHeight) * 3.8)));
gl->GenFramebuffers(1, &shadowFboHandle);
{
gl->BindFramebuffer(GL_FRAMEBUFFER, shadowFboHandle);