Don't change the viewport for every tile we trace

This commit is contained in:
Magnus Norddahl 2023-09-19 00:25:32 +02:00 committed by Christoph Oelckers
parent b9e2a4cb1a
commit 8654c3396f
4 changed files with 41 additions and 34 deletions

View file

@ -141,6 +141,12 @@ void VkLightmap::RenderBakeImage()
cmdbuffer->bindDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, raytrace.pipelineLayout.get(), 0, raytrace.descriptorSet0.get()); cmdbuffer->bindDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, raytrace.pipelineLayout.get(), 0, raytrace.descriptorSet0.get());
cmdbuffer->bindDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, raytrace.pipelineLayout.get(), 1, raytrace.descriptorSet1.get()); cmdbuffer->bindDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, raytrace.pipelineLayout.get(), 1, raytrace.descriptorSet1.get());
VkViewport viewport = {};
viewport.maxDepth = 1;
viewport.width = (float)bakeImageSize;
viewport.height = (float)bakeImageSize;
fb->GetCommands()->GetTransferCommands()->setViewport(0, 1, &viewport);
for (int i = 0, count = selectedSurfaces.Size(); i < count; i++) for (int i = 0, count = selectedSurfaces.Size(); i < count; i++)
{ {
auto& selectedSurface = selectedSurfaces[i]; auto& selectedSurface = selectedSurfaces[i];
@ -152,14 +158,6 @@ void VkLightmap::RenderBakeImage()
continue; continue;
} }
VkViewport viewport = {};
viewport.maxDepth = 1;
viewport.x = (float)selectedSurface.X - 1;
viewport.y = (float)selectedSurface.Y - 1;
viewport.width = (float)(targetSurface->texWidth + 2);
viewport.height = (float)(targetSurface->texHeight + 2);
fb->GetCommands()->GetTransferCommands()->setViewport(0, 1, &viewport);
bool buffersFull = false; bool buffersFull = false;
// Paint all surfaces part of the smoothing group into the surface // Paint all surfaces part of the smoothing group into the surface
@ -199,19 +197,20 @@ void VkLightmap::RenderBakeImage()
LightmapPushConstants pc; LightmapPushConstants pc;
pc.LightStart = firstLight; pc.LightStart = firstLight;
pc.LightEnd = firstLight + lightCount; pc.LightEnd = firstLight + lightCount;
pc.TileX = selectedSurface.X; pc.TileX = (float)selectedSurface.X;
pc.TileY = selectedSurface.Y; pc.TileY = (float)selectedSurface.Y;
pc.SurfaceIndex = mesh->GetSurfaceIndex(targetSurface); pc.SurfaceIndex = mesh->GetSurfaceIndex(targetSurface);
pc.TextureSize = (float)bakeImageSize;
pc.LightmapOrigin = targetSurface->worldOrigin - targetSurface->worldStepX - targetSurface->worldStepY; pc.LightmapOrigin = targetSurface->worldOrigin - targetSurface->worldStepX - targetSurface->worldStepY;
pc.LightmapStepX = targetSurface->worldStepX * viewport.width; pc.LightmapStepX = targetSurface->worldStepX * viewport.width;
pc.LightmapStepY = targetSurface->worldStepY * viewport.height; pc.LightmapStepY = targetSurface->worldStepY * viewport.height;
pc.TileWidth = targetSurface->texWidth; pc.TileWidth = (float)targetSurface->texWidth;
pc.TileHeight = targetSurface->texHeight; pc.TileHeight = (float)targetSurface->texHeight;
pc.WorldToLocal = targetSurface->translateWorldToLocal; pc.WorldToLocal = targetSurface->translateWorldToLocal;
pc.ProjLocalToU = targetSurface->projLocalToU; pc.ProjLocalToU = targetSurface->projLocalToU;
pc.ProjLocalToV = targetSurface->projLocalToV; pc.ProjLocalToV = targetSurface->projLocalToV;
fb->GetCommands()->GetTransferCommands()->pushConstants(raytrace.pipelineLayout.get(), VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(LightmapPushConstants), &pc);
fb->GetCommands()->GetTransferCommands()->pushConstants(raytrace.pipelineLayout.get(), VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(LightmapPushConstants), &pc);
fb->GetCommands()->GetTransferCommands()->drawIndexed(surface->numElements, 1, surface->startElementIndex, 0, 0); fb->GetCommands()->GetTransferCommands()->drawIndexed(surface->numElements, 1, surface->startElementIndex, 0, 0);
} }

View file

@ -24,7 +24,7 @@ struct LightmapPushConstants
int32_t SurfaceIndex; int32_t SurfaceIndex;
int32_t PushPadding1; int32_t PushPadding1;
FVector3 LightmapOrigin; FVector3 LightmapOrigin;
float PushPadding2; float TextureSize;
FVector3 LightmapStepX; FVector3 LightmapStepX;
float PushPadding3; float PushPadding3;
FVector3 LightmapStepY; FVector3 LightmapStepY;
@ -35,10 +35,10 @@ struct LightmapPushConstants
float PushPadding6; float PushPadding6;
FVector3 ProjLocalToV; FVector3 ProjLocalToV;
float PushPadding7; float PushPadding7;
int32_t TileX; float TileX;
int32_t TileY; float TileY;
int32_t TileWidth; float TileWidth;
int32_t TileHeight; float TileHeight;
}; };
struct LightmapBakeImage struct LightmapBakeImage

View file

@ -75,7 +75,7 @@ layout(push_constant) uniform PushConstants
int SurfaceIndex; int SurfaceIndex;
int PushPadding1; int PushPadding1;
vec3 LightmapOrigin; vec3 LightmapOrigin;
float PushPadding2; float TextureSize;
vec3 LightmapStepX; vec3 LightmapStepX;
float PushPadding3; float PushPadding3;
vec3 LightmapStepY; vec3 LightmapStepY;
@ -86,10 +86,10 @@ layout(push_constant) uniform PushConstants
float PushPadding6; float PushPadding6;
vec3 ProjLocalToV; vec3 ProjLocalToV;
float PushPadding7; float PushPadding7;
int TileX; float TileX;
int TileY; float TileY;
int TileWidth; float TileWidth;
int TileHeight; float TileHeight;
}; };
layout(location = 0) centroid in vec3 worldpos; layout(location = 0) centroid in vec3 worldpos;

View file

@ -6,7 +6,7 @@ layout(push_constant) uniform PushConstants
int SurfaceIndex; int SurfaceIndex;
int PushPadding1; int PushPadding1;
vec3 LightmapOrigin; vec3 LightmapOrigin;
float PushPadding2; float TextureSize;
vec3 LightmapStepX; vec3 LightmapStepX;
float PushPadding3; float PushPadding3;
vec3 LightmapStepY; vec3 LightmapStepY;
@ -17,10 +17,10 @@ layout(push_constant) uniform PushConstants
float PushPadding6; float PushPadding6;
vec3 ProjLocalToV; vec3 ProjLocalToV;
float PushPadding7; float PushPadding7;
int TileX; float TileX;
int TileY; float TileY;
int TileWidth; float TileWidth;
int TileHeight; float TileHeight;
}; };
layout(location = 0) in vec3 aPosition; layout(location = 0) in vec3 aPosition;
@ -28,11 +28,19 @@ layout(location = 0) out vec3 worldpos;
void main() void main()
{ {
worldpos = aPosition;
// Project to position relative to tile
vec3 localPos = aPosition - WorldToLocal; vec3 localPos = aPosition - WorldToLocal;
float u = (1.0f + dot(localPos, ProjLocalToU)) / float(TileWidth + 2); float x = 1.0 + dot(localPos, ProjLocalToU);
float v = (1.0f + dot(localPos, ProjLocalToV)) / float(TileHeight + 2); float y = 1.0 + dot(localPos, ProjLocalToV);
worldpos = LightmapOrigin + LightmapStepX * u + LightmapStepY * v; // Find the position in the output texture
gl_Position = vec4(vec2(u, v) * 2.0 - 1.0, 0.0, 1.0); gl_Position = vec4(vec2(TileX + x, TileY + y) / TextureSize * 2.0 - 1.0, 0.0, 1.0);
// Clip all surfaces to the edge of the tile (effectly we are applying a viewport/scissor to the tile)
gl_ClipDistance[0] = x;
gl_ClipDistance[1] = y;
gl_ClipDistance[2] = TileWidth + 2.0 - x;
gl_ClipDistance[3] = TileHeight + 2.0 - y;
} }