2019-10-09 19:16:12 +00:00
|
|
|
/*
|
|
|
|
Created on: Oct 8, 2019
|
|
|
|
|
|
|
|
Copyright 2019 flyinghead
|
|
|
|
|
|
|
|
This file is part of Flycast.
|
|
|
|
|
|
|
|
Flycast is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Flycast is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Flycast. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include "drawer.h"
|
|
|
|
#include "hw/pvr/pvr_mem.h"
|
|
|
|
|
|
|
|
void Drawer::SortTriangles()
|
|
|
|
{
|
|
|
|
sortedPolys.resize(pvrrc.render_passes.used());
|
|
|
|
sortedIndexes.resize(pvrrc.render_passes.used());
|
|
|
|
sortedIndexCount = 0;
|
|
|
|
RenderPass previousPass = {};
|
|
|
|
|
|
|
|
for (int render_pass = 0; render_pass < pvrrc.render_passes.used(); render_pass++)
|
|
|
|
{
|
|
|
|
const RenderPass& current_pass = pvrrc.render_passes.head()[render_pass];
|
|
|
|
sortedIndexes[render_pass].clear();
|
|
|
|
if (current_pass.autosort)
|
|
|
|
{
|
|
|
|
GenSorted(previousPass.tr_count, current_pass.tr_count - previousPass.tr_count, sortedPolys[render_pass], sortedIndexes[render_pass]);
|
|
|
|
for (auto& poly : sortedPolys[render_pass])
|
|
|
|
poly.first += sortedIndexCount;
|
|
|
|
sortedIndexCount += sortedIndexes[render_pass].size();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sortedPolys[render_pass].clear();
|
|
|
|
previousPass = current_pass;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 19:08:14 +00:00
|
|
|
TileClipping BaseDrawer::SetTileClip(u32 val, vk::Rect2D& clipRect)
|
2019-10-09 19:16:12 +00:00
|
|
|
{
|
2020-05-11 13:50:16 +00:00
|
|
|
int rect[4] = {};
|
|
|
|
TileClipping clipmode = ::GetTileClip(val, matrices.GetViewportMatrix(), rect);
|
2021-01-16 17:50:54 +00:00
|
|
|
if (clipmode != TileClipping::Off)
|
|
|
|
{
|
|
|
|
clipRect.offset.x = rect[0];
|
|
|
|
clipRect.offset.y = rect[1];
|
|
|
|
clipRect.extent.width = rect[2];
|
|
|
|
clipRect.extent.height = rect[3];
|
|
|
|
}
|
2020-05-11 13:50:16 +00:00
|
|
|
|
|
|
|
return clipmode;
|
2019-10-09 19:16:12 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 18:31:01 +00:00
|
|
|
void BaseDrawer::SetBaseScissor(const vk::Extent2D& viewport)
|
2019-11-13 19:08:14 +00:00
|
|
|
{
|
2021-03-01 09:13:40 +00:00
|
|
|
bool wide_screen_on = config::Widescreen && !pvrrc.isRenderFramebuffer
|
|
|
|
&& !matrices.IsClipped() && !config::Rotate90;
|
2019-11-13 19:08:14 +00:00
|
|
|
if (!wide_screen_on)
|
|
|
|
{
|
2020-11-19 21:29:06 +00:00
|
|
|
float width;
|
|
|
|
float height;
|
|
|
|
float min_x;
|
|
|
|
float min_y;
|
|
|
|
glm::vec4 clip_min(pvrrc.fb_X_CLIP.min, pvrrc.fb_Y_CLIP.min, 0, 1);
|
|
|
|
glm::vec4 clip_dim(pvrrc.fb_X_CLIP.max - pvrrc.fb_X_CLIP.min + 1,
|
|
|
|
pvrrc.fb_Y_CLIP.max - pvrrc.fb_Y_CLIP.min + 1, 0, 0);
|
|
|
|
clip_min = matrices.GetScissorMatrix() * clip_min;
|
|
|
|
clip_dim = matrices.GetScissorMatrix() * clip_dim;
|
|
|
|
|
|
|
|
min_x = clip_min[0];
|
|
|
|
min_y = clip_min[1];
|
|
|
|
width = clip_dim[0];
|
|
|
|
height = clip_dim[1];
|
|
|
|
if (width < 0)
|
2019-11-13 19:08:14 +00:00
|
|
|
{
|
2020-11-19 21:29:06 +00:00
|
|
|
min_x += width;
|
|
|
|
width = -width;
|
2019-11-13 19:08:14 +00:00
|
|
|
}
|
2020-11-19 21:29:06 +00:00
|
|
|
if (height < 0)
|
2019-11-13 19:08:14 +00:00
|
|
|
{
|
2020-11-19 21:29:06 +00:00
|
|
|
min_y += height;
|
|
|
|
height = -height;
|
2019-11-13 19:08:14 +00:00
|
|
|
}
|
2020-11-19 21:29:06 +00:00
|
|
|
|
|
|
|
baseScissor = vk::Rect2D(
|
|
|
|
vk::Offset2D((u32) std::max(lroundf(min_x), 0L),
|
|
|
|
(u32) std::max(lroundf(min_y), 0L)),
|
|
|
|
vk::Extent2D((u32) std::max(lroundf(width), 0L),
|
|
|
|
(u32) std::max(lroundf(height), 0L)));
|
2019-11-13 19:08:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-19 18:31:01 +00:00
|
|
|
baseScissor = { 0, 0, (u32)viewport.width, (u32)viewport.height };
|
2019-11-13 19:08:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-09 19:16:12 +00:00
|
|
|
void Drawer::DrawPoly(const vk::CommandBuffer& cmdBuffer, u32 listType, bool sortTriangles, const PolyParam& poly, u32 first, u32 count)
|
|
|
|
{
|
2019-10-13 20:01:20 +00:00
|
|
|
vk::Rect2D scissorRect;
|
|
|
|
TileClipping tileClip = SetTileClip(poly.tileclip, scissorRect);
|
2019-12-04 18:06:14 +00:00
|
|
|
if (tileClip == TileClipping::Outside)
|
|
|
|
SetScissor(cmdBuffer, scissorRect);
|
|
|
|
else
|
|
|
|
SetScissor(cmdBuffer, baseScissor);
|
2019-10-13 20:01:20 +00:00
|
|
|
|
|
|
|
float trilinearAlpha = 1.f;
|
2020-01-21 18:52:09 +00:00
|
|
|
if (poly.tsp.FilterMode > 1 && poly.pcw.Texture && listType != ListType_Punch_Through && poly.tcw.MipMapped == 1)
|
2019-10-09 19:16:12 +00:00
|
|
|
{
|
2021-04-17 16:40:58 +00:00
|
|
|
trilinearAlpha = 0.25f * (poly.tsp.MipMapD & 0x3);
|
2019-10-09 19:16:12 +00:00
|
|
|
if (poly.tsp.FilterMode == 2)
|
|
|
|
// Trilinear pass A
|
2021-04-17 16:40:58 +00:00
|
|
|
trilinearAlpha = 1.f - trilinearAlpha;
|
2019-10-09 19:16:12 +00:00
|
|
|
}
|
2021-04-17 16:40:58 +00:00
|
|
|
bool gpuPalette = poly.texture != nullptr ? poly.texture->gpuPalette : false;
|
2020-07-08 16:17:15 +00:00
|
|
|
float palette_index = 0.f;
|
2021-04-17 16:40:58 +00:00
|
|
|
if (gpuPalette)
|
2020-07-08 16:17:15 +00:00
|
|
|
{
|
|
|
|
if (poly.tcw.PixelFmt == PixelPal4)
|
|
|
|
palette_index = float(poly.tcw.PalSelect << 4) / 1023.f;
|
|
|
|
else
|
|
|
|
palette_index = float((poly.tcw.PalSelect >> 4) << 8) / 1023.f;
|
|
|
|
}
|
2019-10-09 19:16:12 +00:00
|
|
|
|
2021-04-17 16:40:58 +00:00
|
|
|
if (tileClip == TileClipping::Inside || trilinearAlpha != 1.f || gpuPalette)
|
2019-10-16 08:40:06 +00:00
|
|
|
{
|
2020-07-08 16:17:15 +00:00
|
|
|
std::array<float, 6> pushConstants = {
|
2019-12-09 18:37:14 +00:00
|
|
|
(float)scissorRect.offset.x,
|
|
|
|
(float)scissorRect.offset.y,
|
|
|
|
(float)scissorRect.offset.x + (float)scissorRect.extent.width,
|
|
|
|
(float)scissorRect.offset.y + (float)scissorRect.extent.height,
|
2020-07-08 16:17:15 +00:00
|
|
|
trilinearAlpha,
|
|
|
|
palette_index
|
2019-12-09 18:37:14 +00:00
|
|
|
};
|
2019-10-11 10:38:09 +00:00
|
|
|
cmdBuffer.pushConstants<float>(pipelineManager->GetPipelineLayout(), vk::ShaderStageFlagBits::eFragment, 0, pushConstants);
|
2019-10-16 08:40:06 +00:00
|
|
|
}
|
2019-10-09 19:16:12 +00:00
|
|
|
|
2021-04-17 16:40:58 +00:00
|
|
|
vk::Pipeline pipeline = pipelineManager->GetPipeline(listType, sortTriangles, poly, gpuPalette);
|
2019-10-09 19:16:12 +00:00
|
|
|
cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline);
|
2022-03-12 16:56:46 +00:00
|
|
|
if (poly.pcw.Texture || poly.isNaomi2())
|
|
|
|
{
|
|
|
|
vk::DeviceSize offset = 0;
|
|
|
|
u32 index = 0;
|
|
|
|
if (poly.isNaomi2())
|
|
|
|
{
|
|
|
|
switch (listType)
|
|
|
|
{
|
|
|
|
case ListType_Opaque:
|
|
|
|
offset = offsets.naomi2OpaqueOffset;
|
|
|
|
index = &poly - pvrrc.global_param_op.head();
|
|
|
|
break;
|
|
|
|
case ListType_Punch_Through:
|
|
|
|
offset = offsets.naomi2PunchThroughOffset;
|
|
|
|
index = &poly - pvrrc.global_param_pt.head();
|
|
|
|
break;
|
|
|
|
case ListType_Translucent:
|
|
|
|
offset = offsets.naomi2TranslucentOffset;
|
|
|
|
index = &poly - pvrrc.global_param_tr.head();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
descriptorSets.bindPerPolyDescriptorSets(cmdBuffer, poly, index, *GetMainBuffer(0)->buffer, offset, offsets.lightsOffset);
|
|
|
|
}
|
2019-10-09 19:16:12 +00:00
|
|
|
cmdBuffer.drawIndexed(count, 1, first, 0, 0);
|
|
|
|
}
|
|
|
|
|
2021-11-04 08:13:47 +00:00
|
|
|
void Drawer::DrawSorted(const vk::CommandBuffer& cmdBuffer, const std::vector<SortTrigDrawParam>& polys, bool multipass)
|
2019-10-09 19:16:12 +00:00
|
|
|
{
|
|
|
|
for (const SortTrigDrawParam& param : polys)
|
|
|
|
DrawPoly(cmdBuffer, ListType_Translucent, true, *param.ppid, pvrrc.idx.used() + param.first, param.count);
|
2021-11-04 08:13:47 +00:00
|
|
|
if (multipass && config::TranslucentPolygonDepthMask)
|
|
|
|
{
|
|
|
|
// Write to the depth buffer now. The next render pass might need it. (Cosmic Smash)
|
|
|
|
for (const SortTrigDrawParam& param : polys)
|
|
|
|
{
|
|
|
|
if (param.ppid->isp.ZWriteDis)
|
|
|
|
continue;
|
2022-03-12 16:56:46 +00:00
|
|
|
vk::Pipeline pipeline = pipelineManager->GetDepthPassPipeline(param.ppid->isp.CullMode, param.ppid->isNaomi2());
|
2021-11-04 08:13:47 +00:00
|
|
|
cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline);
|
|
|
|
vk::Rect2D scissorRect;
|
|
|
|
TileClipping tileClip = SetTileClip(param.ppid->tileclip, scissorRect);
|
|
|
|
if (tileClip == TileClipping::Outside)
|
|
|
|
SetScissor(cmdBuffer, scissorRect);
|
|
|
|
else
|
|
|
|
SetScissor(cmdBuffer, baseScissor);
|
|
|
|
cmdBuffer.drawIndexed(param.count, 1, pvrrc.idx.used() + param.first, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
2019-10-09 19:16:12 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 20:30:34 +00:00
|
|
|
void Drawer::DrawList(const vk::CommandBuffer& cmdBuffer, u32 listType, bool sortTriangles, const List<PolyParam>& polys, u32 first, u32 last)
|
2019-10-09 19:16:12 +00:00
|
|
|
{
|
2020-02-25 15:34:49 +00:00
|
|
|
const PolyParam *pp_end = polys.head() + last;
|
|
|
|
for (const PolyParam *pp = polys.head() + first; pp != pp_end; pp++)
|
|
|
|
if (pp->count > 2)
|
|
|
|
DrawPoly(cmdBuffer, listType, sortTriangles, *pp, pp->first, pp->count);
|
2019-10-09 19:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Drawer::DrawModVols(const vk::CommandBuffer& cmdBuffer, int first, int count)
|
|
|
|
{
|
2021-03-01 09:13:40 +00:00
|
|
|
if (count == 0 || pvrrc.modtrig.used() == 0 || !config::ModifierVolumes)
|
2019-10-09 19:16:12 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
vk::Buffer buffer = GetMainBuffer(0)->buffer.get();
|
2019-10-15 14:49:20 +00:00
|
|
|
cmdBuffer.bindVertexBuffers(0, 1, &buffer, &offsets.modVolOffset);
|
2019-12-04 14:51:26 +00:00
|
|
|
SetScissor(cmdBuffer, baseScissor);
|
2019-10-09 19:16:12 +00:00
|
|
|
|
|
|
|
ModifierVolumeParam* params = &pvrrc.global_param_mvo.head()[first];
|
|
|
|
|
|
|
|
int mod_base = -1;
|
|
|
|
vk::Pipeline pipeline;
|
|
|
|
|
2020-03-29 18:58:49 +00:00
|
|
|
for (int cmv = 0; cmv < count; cmv++)
|
2019-10-09 19:16:12 +00:00
|
|
|
{
|
|
|
|
ModifierVolumeParam& param = params[cmv];
|
|
|
|
|
|
|
|
if (param.count == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
u32 mv_mode = param.isp.DepthMode;
|
|
|
|
|
|
|
|
if (mod_base == -1)
|
|
|
|
mod_base = param.first;
|
|
|
|
|
|
|
|
if (!param.isp.VolumeLast && mv_mode > 0)
|
2022-03-12 16:56:46 +00:00
|
|
|
pipeline = pipelineManager->GetModifierVolumePipeline(ModVolMode::Or, param.isp.CullMode, param.isNaomi2()); // OR'ing (open volume or quad)
|
2019-10-09 19:16:12 +00:00
|
|
|
else
|
2022-03-12 16:56:46 +00:00
|
|
|
pipeline = pipelineManager->GetModifierVolumePipeline(ModVolMode::Xor, param.isp.CullMode, param.isNaomi2()); // XOR'ing (closed volume)
|
|
|
|
|
2019-10-09 19:16:12 +00:00
|
|
|
cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline);
|
2022-03-12 16:56:46 +00:00
|
|
|
descriptorSets.bindPerPolyDescriptorSets(cmdBuffer, param, first + cmv, *GetMainBuffer(0)->buffer, offsets.naomi2ModVolOffset);
|
|
|
|
|
2019-10-09 19:16:12 +00:00
|
|
|
cmdBuffer.draw(param.count * 3, 1, param.first * 3, 0);
|
|
|
|
|
|
|
|
if (mv_mode == 1 || mv_mode == 2)
|
|
|
|
{
|
|
|
|
// Sum the area
|
2022-03-12 16:56:46 +00:00
|
|
|
pipeline = pipelineManager->GetModifierVolumePipeline(mv_mode == 1 ? ModVolMode::Inclusion : ModVolMode::Exclusion, param.isp.CullMode, param.isNaomi2());
|
2019-10-09 19:16:12 +00:00
|
|
|
cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline);
|
|
|
|
cmdBuffer.draw((param.first + param.count - mod_base) * 3, 1, mod_base * 3, 0);
|
|
|
|
mod_base = -1;
|
|
|
|
}
|
|
|
|
}
|
2019-10-15 14:49:20 +00:00
|
|
|
const vk::DeviceSize offset = 0;
|
|
|
|
cmdBuffer.bindVertexBuffers(0, 1, &buffer, &offset);
|
2019-10-09 19:16:12 +00:00
|
|
|
|
|
|
|
std::array<float, 5> pushConstants = { 1 - FPU_SHAD_SCALE.scale_factor / 256.f, 0, 0, 0, 0 };
|
|
|
|
cmdBuffer.pushConstants<float>(pipelineManager->GetPipelineLayout(), vk::ShaderStageFlagBits::eFragment, 0, pushConstants);
|
|
|
|
|
2022-03-12 16:56:46 +00:00
|
|
|
pipeline = pipelineManager->GetModifierVolumePipeline(ModVolMode::Final, 0, false);
|
2019-10-09 19:16:12 +00:00
|
|
|
cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline);
|
|
|
|
cmdBuffer.drawIndexed(4, 1, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
2019-10-15 14:49:20 +00:00
|
|
|
void Drawer::UploadMainBuffer(const VertexShaderUniforms& vertexUniforms, const FragmentShaderUniforms& fragmentUniforms)
|
2019-10-09 19:16:12 +00:00
|
|
|
{
|
2022-03-12 16:56:46 +00:00
|
|
|
BufferPacker packer;
|
2019-10-09 19:16:12 +00:00
|
|
|
|
2019-10-15 14:49:20 +00:00
|
|
|
// Vertex
|
2022-03-12 16:56:46 +00:00
|
|
|
packer.add(pvrrc.verts.head(), pvrrc.verts.bytes());
|
2019-10-15 14:49:20 +00:00
|
|
|
// Modifier Volumes
|
2022-03-12 16:56:46 +00:00
|
|
|
offsets.modVolOffset = packer.add(pvrrc.modtrig.head(), pvrrc.modtrig.bytes());
|
2019-10-15 14:49:20 +00:00
|
|
|
// Index
|
2022-03-12 16:56:46 +00:00
|
|
|
offsets.indexOffset = packer.add(pvrrc.idx.head(), pvrrc.idx.bytes());
|
2019-10-09 19:16:12 +00:00
|
|
|
for (const std::vector<u32>& idx : sortedIndexes)
|
|
|
|
if (!idx.empty())
|
2022-03-12 16:56:46 +00:00
|
|
|
packer.add(&idx[0], idx.size() * sizeof(u32));
|
2019-10-15 14:49:20 +00:00
|
|
|
// Uniform buffers
|
2022-03-12 16:56:46 +00:00
|
|
|
offsets.vertexUniformOffset = packer.addUniform(&vertexUniforms, sizeof(vertexUniforms));
|
|
|
|
offsets.fragmentUniformOffset = packer.addUniform(&fragmentUniforms, sizeof(fragmentUniforms));
|
|
|
|
|
|
|
|
std::vector<u8> n2uniforms;
|
|
|
|
std::vector<u8> n2lights;
|
|
|
|
if (settings.platform.isNaomi2())
|
|
|
|
{
|
|
|
|
uploadNaomi2Uniforms(packer, offsets, n2uniforms, false);
|
|
|
|
offsets.lightsOffset = uploadNaomi2Lights(packer, n2lights);
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferData *buffer = GetMainBuffer(packer.size());
|
|
|
|
packer.upload(*buffer);
|
2019-10-09 19:16:12 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 16:17:15 +00:00
|
|
|
bool Drawer::Draw(const Texture *fogTexture, const Texture *paletteTexture)
|
2019-10-09 19:16:12 +00:00
|
|
|
{
|
2019-11-13 19:08:14 +00:00
|
|
|
FragmentShaderUniforms fragUniforms = MakeFragmentUniforms<FragmentShaderUniforms>();
|
2019-10-09 19:16:12 +00:00
|
|
|
|
|
|
|
SortTriangles();
|
2019-10-13 20:01:20 +00:00
|
|
|
currentScissor = vk::Rect2D();
|
2019-10-09 19:16:12 +00:00
|
|
|
|
|
|
|
vk::CommandBuffer cmdBuffer = BeginRenderPass();
|
2021-11-07 17:21:44 +00:00
|
|
|
if (!pvrrc.isRTT && (FB_R_CTRL.fb_enable == 0 || VO_CONTROL.blank_video == 1))
|
|
|
|
{
|
|
|
|
// Video output disabled
|
|
|
|
return true;
|
|
|
|
}
|
2019-10-09 19:16:12 +00:00
|
|
|
|
2021-12-15 08:36:22 +00:00
|
|
|
setFirstProvokingVertex(pvrrc);
|
2019-12-10 11:51:10 +00:00
|
|
|
|
2022-03-12 16:56:46 +00:00
|
|
|
// Do per-poly sorting
|
|
|
|
RenderPass previous_pass = {};
|
|
|
|
if (config::PerStripSorting)
|
|
|
|
for (int render_pass = 0; render_pass < pvrrc.render_passes.used(); render_pass++)
|
|
|
|
{
|
|
|
|
const RenderPass& current_pass = pvrrc.render_passes.head()[render_pass];
|
|
|
|
if (current_pass.autosort)
|
|
|
|
SortPParams(previous_pass.tr_count, current_pass.tr_count - previous_pass.tr_count);
|
|
|
|
previous_pass = current_pass;
|
|
|
|
}
|
|
|
|
|
2019-10-09 19:16:12 +00:00
|
|
|
// Upload vertex and index buffers
|
2020-11-19 21:29:06 +00:00
|
|
|
VertexShaderUniforms vtxUniforms;
|
2022-03-12 16:56:46 +00:00
|
|
|
vtxUniforms.ndcMat = matrices.GetNormalMatrix();
|
2020-11-19 21:29:06 +00:00
|
|
|
|
2019-10-15 14:49:20 +00:00
|
|
|
UploadMainBuffer(vtxUniforms, fragUniforms);
|
2019-10-09 19:16:12 +00:00
|
|
|
|
|
|
|
// Update per-frame descriptor set and bind it
|
2022-03-12 16:56:46 +00:00
|
|
|
descriptorSets.updateUniforms(GetMainBuffer(0)->buffer.get(), (u32)offsets.vertexUniformOffset, (u32)offsets.fragmentUniformOffset,
|
2020-07-08 16:17:15 +00:00
|
|
|
fogTexture->GetImageView(), paletteTexture->GetImageView());
|
2022-03-12 16:56:46 +00:00
|
|
|
descriptorSets.bindPerFrameDescriptorSets(cmdBuffer);
|
2019-10-09 19:16:12 +00:00
|
|
|
|
|
|
|
// Bind vertex and index buffers
|
2019-10-15 14:49:20 +00:00
|
|
|
const vk::DeviceSize zeroOffset[] = { 0 };
|
2019-10-09 19:16:12 +00:00
|
|
|
const vk::Buffer buffer = GetMainBuffer(0)->buffer.get();
|
2019-10-15 14:49:20 +00:00
|
|
|
cmdBuffer.bindVertexBuffers(0, 1, &buffer, zeroOffset);
|
|
|
|
cmdBuffer.bindIndexBuffer(buffer, offsets.indexOffset, vk::IndexType::eUint32);
|
2019-10-09 19:16:12 +00:00
|
|
|
|
2019-12-11 21:31:35 +00:00
|
|
|
// Make sure to push constants even if not used
|
|
|
|
std::array<float, 5> pushConstants = { 0, 0, 0, 0, 0 };
|
|
|
|
cmdBuffer.pushConstants<float>(pipelineManager->GetPipelineLayout(), vk::ShaderStageFlagBits::eFragment, 0, pushConstants);
|
|
|
|
|
2022-03-12 16:56:46 +00:00
|
|
|
previous_pass = {};
|
2019-10-09 19:16:12 +00:00
|
|
|
for (int render_pass = 0; render_pass < pvrrc.render_passes.used(); render_pass++)
|
|
|
|
{
|
|
|
|
const RenderPass& current_pass = pvrrc.render_passes.head()[render_pass];
|
|
|
|
|
2019-10-25 20:30:34 +00:00
|
|
|
DEBUG_LOG(RENDERER, "Render pass %d OP %d PT %d TR %d MV %d autosort %d", render_pass + 1,
|
2019-10-09 19:16:12 +00:00
|
|
|
current_pass.op_count - previous_pass.op_count,
|
|
|
|
current_pass.pt_count - previous_pass.pt_count,
|
|
|
|
current_pass.tr_count - previous_pass.tr_count,
|
2019-10-25 20:30:34 +00:00
|
|
|
current_pass.mvo_count - previous_pass.mvo_count, current_pass.autosort);
|
|
|
|
DrawList(cmdBuffer, ListType_Opaque, false, pvrrc.global_param_op, previous_pass.op_count, current_pass.op_count);
|
|
|
|
DrawList(cmdBuffer, ListType_Punch_Through, false, pvrrc.global_param_pt, previous_pass.pt_count, current_pass.pt_count);
|
2019-10-09 19:16:12 +00:00
|
|
|
DrawModVols(cmdBuffer, previous_pass.mvo_count, current_pass.mvo_count - previous_pass.mvo_count);
|
|
|
|
if (current_pass.autosort)
|
|
|
|
{
|
2021-03-01 09:13:40 +00:00
|
|
|
if (!config::PerStripSorting)
|
2021-11-04 08:13:47 +00:00
|
|
|
DrawSorted(cmdBuffer, sortedPolys[render_pass], render_pass + 1 < pvrrc.render_passes.used());
|
2019-10-09 19:16:12 +00:00
|
|
|
else
|
2019-10-25 20:30:34 +00:00
|
|
|
DrawList(cmdBuffer, ListType_Translucent, true, pvrrc.global_param_tr, previous_pass.tr_count, current_pass.tr_count);
|
2019-10-09 19:16:12 +00:00
|
|
|
}
|
|
|
|
else
|
2019-10-25 20:30:34 +00:00
|
|
|
DrawList(cmdBuffer, ListType_Translucent, false, pvrrc.global_param_tr, previous_pass.tr_count, current_pass.tr_count);
|
2019-10-09 19:16:12 +00:00
|
|
|
previous_pass = current_pass;
|
|
|
|
}
|
|
|
|
|
2019-10-23 16:13:28 +00:00
|
|
|
return !pvrrc.isRTT;
|
2019-10-09 19:16:12 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 18:37:14 +00:00
|
|
|
void TextureDrawer::Init(SamplerManager *samplerManager, ShaderManager *shaderManager, TextureCache *textureCache)
|
|
|
|
{
|
|
|
|
if (!rttPipelineManager)
|
|
|
|
rttPipelineManager = std::unique_ptr<RttPipelineManager>(new RttPipelineManager());
|
|
|
|
rttPipelineManager->Init(shaderManager);
|
|
|
|
Drawer::Init(samplerManager, rttPipelineManager.get());
|
|
|
|
|
|
|
|
this->textureCache = textureCache;
|
|
|
|
}
|
|
|
|
|
2019-10-09 19:16:12 +00:00
|
|
|
vk::CommandBuffer TextureDrawer::BeginRenderPass()
|
|
|
|
{
|
2021-04-08 08:38:26 +00:00
|
|
|
DEBUG_LOG(RENDERER, "RenderToTexture packmode=%d stride=%d - %d x %d @ %06x", FB_W_CTRL.fb_packmode, FB_W_LINESTRIDE.stride * 8,
|
|
|
|
pvrrc.fb_X_CLIP.max + 1, pvrrc.fb_Y_CLIP.max + 1, FB_W_SOF1 & VRAM_MASK);
|
2019-10-23 16:13:28 +00:00
|
|
|
matrices.CalcMatrices(&pvrrc);
|
|
|
|
|
2019-10-09 19:16:12 +00:00
|
|
|
textureAddr = FB_W_SOF1 & VRAM_MASK;
|
2021-04-08 08:38:26 +00:00
|
|
|
u32 origWidth = pvrrc.fb_X_CLIP.max + 1;
|
|
|
|
u32 origHeight = pvrrc.fb_Y_CLIP.max + 1;
|
2020-05-11 13:50:16 +00:00
|
|
|
u32 heightPow2 = 8;
|
2021-05-10 18:07:23 +00:00
|
|
|
while (heightPow2 < origHeight)
|
2019-10-09 19:16:12 +00:00
|
|
|
heightPow2 *= 2;
|
2020-05-11 13:50:16 +00:00
|
|
|
u32 widthPow2 = 8;
|
2021-05-11 14:47:54 +00:00
|
|
|
while (widthPow2 < origWidth)
|
2019-10-09 19:16:12 +00:00
|
|
|
widthPow2 *= 2;
|
2021-05-10 18:07:23 +00:00
|
|
|
float upscale = 1.f;
|
|
|
|
if (!config::RenderToTextureBuffer)
|
|
|
|
upscale = config::RenderResolution / 480.f;
|
|
|
|
u32 upscaledWidth = origWidth * upscale;
|
|
|
|
u32 upscaledHeight = origHeight * upscale;
|
|
|
|
widthPow2 *= upscale;
|
|
|
|
heightPow2 *= upscale;
|
2019-10-09 19:16:12 +00:00
|
|
|
|
2019-12-09 18:37:14 +00:00
|
|
|
rttPipelineManager->CheckSettingsChange();
|
2019-10-09 19:16:12 +00:00
|
|
|
VulkanContext *context = GetContext();
|
2019-10-25 17:20:15 +00:00
|
|
|
vk::Device device = context->GetDevice();
|
2019-10-09 19:16:12 +00:00
|
|
|
|
2019-12-09 18:37:14 +00:00
|
|
|
NewImage();
|
2019-10-09 19:16:12 +00:00
|
|
|
vk::CommandBuffer commandBuffer = commandPool->Allocate();
|
|
|
|
commandBuffer.begin(vk::CommandBufferBeginInfo(vk::CommandBufferUsageFlagBits::eOneTimeSubmit));
|
|
|
|
|
2019-12-09 18:37:14 +00:00
|
|
|
if (!depthAttachment || widthPow2 > depthAttachment->getExtent().width || heightPow2 > depthAttachment->getExtent().height)
|
2019-10-09 19:16:12 +00:00
|
|
|
{
|
|
|
|
if (!depthAttachment)
|
2019-11-26 09:42:44 +00:00
|
|
|
depthAttachment = std::unique_ptr<FramebufferAttachment>(new FramebufferAttachment(context->GetPhysicalDevice(), device));
|
2019-12-09 18:37:14 +00:00
|
|
|
else
|
|
|
|
GetContext()->WaitIdle();
|
2019-12-04 14:51:26 +00:00
|
|
|
depthAttachment->Init(widthPow2, heightPow2, GetContext()->GetDepthFormat(),
|
|
|
|
vk::ImageUsageFlagBits::eDepthStencilAttachment | vk::ImageUsageFlagBits::eTransientAttachment);
|
2019-10-09 19:16:12 +00:00
|
|
|
}
|
2019-12-09 18:37:14 +00:00
|
|
|
vk::Image colorImage;
|
2019-10-09 19:16:12 +00:00
|
|
|
vk::ImageView colorImageView;
|
|
|
|
vk::ImageLayout colorImageCurrentLayout;
|
|
|
|
|
2021-03-01 09:13:40 +00:00
|
|
|
if (!config::RenderToTextureBuffer)
|
2019-10-09 19:16:12 +00:00
|
|
|
{
|
|
|
|
// TexAddr : fb_rtt.TexAddr, Reserved : 0, StrideSel : 0, ScanOrder : 1
|
|
|
|
TCW tcw = { { textureAddr >> 3, 0, 0, 1 } };
|
|
|
|
switch (FB_W_CTRL.fb_packmode) {
|
|
|
|
case 0:
|
|
|
|
case 3:
|
|
|
|
tcw.PixelFmt = Pixel1555;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
tcw.PixelFmt = Pixel565;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
tcw.PixelFmt = Pixel4444;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-11-01 12:05:22 +00:00
|
|
|
TSP tsp = { { 0 } };
|
2020-05-11 13:50:16 +00:00
|
|
|
for (tsp.TexU = 0; tsp.TexU <= 7 && (8u << tsp.TexU) < origWidth; tsp.TexU++);
|
|
|
|
for (tsp.TexV = 0; tsp.TexV <= 7 && (8u << tsp.TexV) < origHeight; tsp.TexV++);
|
2019-10-09 19:16:12 +00:00
|
|
|
|
2019-10-21 14:39:16 +00:00
|
|
|
texture = textureCache->getTextureCacheData(tsp, tcw);
|
2019-10-09 19:16:12 +00:00
|
|
|
if (texture->IsNew())
|
2019-10-21 14:39:16 +00:00
|
|
|
{
|
2019-10-09 19:16:12 +00:00
|
|
|
texture->Create();
|
2019-10-21 14:39:16 +00:00
|
|
|
texture->SetPhysicalDevice(GetContext()->GetPhysicalDevice());
|
2019-10-25 17:20:15 +00:00
|
|
|
texture->SetDevice(device);
|
2019-10-21 14:39:16 +00:00
|
|
|
}
|
2019-12-09 18:37:14 +00:00
|
|
|
else if (textureCache->IsInFlight(texture))
|
|
|
|
{
|
|
|
|
texture->readOnlyImageView = *texture->imageView;
|
|
|
|
textureCache->DestroyLater(texture);
|
|
|
|
}
|
2020-07-10 16:50:53 +00:00
|
|
|
textureCache->SetInFlight(texture);
|
2019-12-09 18:37:14 +00:00
|
|
|
|
|
|
|
if (texture->format != vk::Format::eR8G8B8A8Unorm || texture->extent.width != widthPow2 || texture->extent.height != heightPow2)
|
2019-10-09 19:16:12 +00:00
|
|
|
{
|
|
|
|
texture->extent = vk::Extent2D(widthPow2, heightPow2);
|
|
|
|
texture->format = vk::Format::eR8G8B8A8Unorm;
|
2020-07-10 16:50:53 +00:00
|
|
|
texture->needsStaging = true;
|
2019-10-09 19:16:12 +00:00
|
|
|
texture->CreateImage(vk::ImageTiling::eOptimal, vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eSampled,
|
2020-07-10 16:50:53 +00:00
|
|
|
vk::ImageLayout::eUndefined, vk::ImageAspectFlagBits::eColor);
|
2019-10-09 19:16:12 +00:00
|
|
|
colorImageCurrentLayout = vk::ImageLayout::eUndefined;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
colorImageCurrentLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
|
|
|
|
}
|
|
|
|
colorImage = *texture->image;
|
|
|
|
colorImageView = texture->GetImageView();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-09 18:37:14 +00:00
|
|
|
if (!colorAttachment || widthPow2 > colorAttachment->getExtent().width || heightPow2 > colorAttachment->getExtent().height)
|
2019-10-09 19:16:12 +00:00
|
|
|
{
|
|
|
|
if (!colorAttachment)
|
2019-11-26 09:42:44 +00:00
|
|
|
colorAttachment = std::unique_ptr<FramebufferAttachment>(new FramebufferAttachment(context->GetPhysicalDevice(), device));
|
2019-12-09 18:37:14 +00:00
|
|
|
else
|
|
|
|
GetContext()->WaitIdle();
|
2019-11-29 18:28:22 +00:00
|
|
|
colorAttachment->Init(widthPow2, heightPow2, vk::Format::eR8G8B8A8Unorm,
|
|
|
|
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eTransferSrc);
|
2019-12-09 18:37:14 +00:00
|
|
|
colorImageCurrentLayout = vk::ImageLayout::eUndefined;
|
2019-10-09 19:16:12 +00:00
|
|
|
}
|
2019-12-09 18:37:14 +00:00
|
|
|
else
|
|
|
|
colorImageCurrentLayout = vk::ImageLayout::eTransferSrcOptimal;
|
2019-11-10 09:16:18 +00:00
|
|
|
colorImage = colorAttachment->GetImage();
|
|
|
|
colorImageView = colorAttachment->GetImageView();
|
2019-10-09 19:16:12 +00:00
|
|
|
}
|
|
|
|
width = widthPow2;
|
|
|
|
height = heightPow2;
|
|
|
|
|
2019-12-09 18:37:14 +00:00
|
|
|
setImageLayout(commandBuffer, colorImage, vk::Format::eR8G8B8A8Unorm, 1, colorImageCurrentLayout, vk::ImageLayout::eColorAttachmentOptimal);
|
2019-12-04 14:51:26 +00:00
|
|
|
|
2019-10-09 19:16:12 +00:00
|
|
|
vk::ImageView imageViews[] = {
|
|
|
|
colorImageView,
|
2019-11-10 09:16:18 +00:00
|
|
|
depthAttachment->GetImageView(),
|
2019-10-09 19:16:12 +00:00
|
|
|
};
|
2019-12-09 18:37:14 +00:00
|
|
|
framebuffers.resize(GetContext()->GetSwapChainSize());
|
|
|
|
framebuffers[GetCurrentImage()] = device.createFramebufferUnique(vk::FramebufferCreateInfo(vk::FramebufferCreateFlags(),
|
|
|
|
rttPipelineManager->GetRenderPass(), ARRAY_SIZE(imageViews), imageViews, widthPow2, heightPow2, 1));
|
2019-10-09 19:16:12 +00:00
|
|
|
|
|
|
|
const vk::ClearValue clear_colors[] = { vk::ClearColorValue(std::array<float, 4> { 0.f, 0.f, 0.f, 1.f }), vk::ClearDepthStencilValue { 0.f, 0 } };
|
2019-12-09 18:37:14 +00:00
|
|
|
commandBuffer.beginRenderPass(vk::RenderPassBeginInfo(rttPipelineManager->GetRenderPass(), *framebuffers[GetCurrentImage()],
|
2019-10-09 19:16:12 +00:00
|
|
|
vk::Rect2D( { 0, 0 }, { width, height }), 2, clear_colors), vk::SubpassContents::eInline);
|
|
|
|
commandBuffer.setViewport(0, vk::Viewport(0.0f, 0.0f, (float)upscaledWidth, (float)upscaledHeight, 1.0f, 0.0f));
|
2021-04-08 08:38:26 +00:00
|
|
|
baseScissor = vk::Rect2D(vk::Offset2D(pvrrc.fb_X_CLIP.min * upscale, pvrrc.fb_Y_CLIP.min * upscale),
|
|
|
|
vk::Extent2D(upscaledWidth, upscaledHeight));
|
2019-10-13 20:01:20 +00:00
|
|
|
commandBuffer.setScissor(0, baseScissor);
|
2019-10-09 19:16:12 +00:00
|
|
|
currentCommandBuffer = commandBuffer;
|
|
|
|
|
|
|
|
return commandBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureDrawer::EndRenderPass()
|
|
|
|
{
|
|
|
|
currentCommandBuffer.endRenderPass();
|
|
|
|
|
2021-04-08 08:38:26 +00:00
|
|
|
u32 clippedWidth = pvrrc.fb_X_CLIP.max + 1;
|
|
|
|
u32 clippedHeight = pvrrc.fb_Y_CLIP.max + 1;
|
2019-12-10 11:51:10 +00:00
|
|
|
|
2021-03-01 09:13:40 +00:00
|
|
|
if (config::RenderToTextureBuffer)
|
2019-10-09 19:16:12 +00:00
|
|
|
{
|
2019-12-10 11:51:10 +00:00
|
|
|
vk::BufferImageCopy copyRegion(0, clippedWidth, clippedHeight, vk::ImageSubresourceLayers(vk::ImageAspectFlagBits::eColor, 0, 0, 1), vk::Offset3D(0, 0, 0),
|
|
|
|
vk::Extent3D(vk::Extent2D(clippedWidth, clippedHeight), 1));
|
2019-11-10 09:16:18 +00:00
|
|
|
currentCommandBuffer.copyImageToBuffer(colorAttachment->GetImage(), vk::ImageLayout::eTransferSrcOptimal,
|
2019-10-09 19:16:12 +00:00
|
|
|
*colorAttachment->GetBufferData()->buffer, copyRegion);
|
|
|
|
|
|
|
|
vk::BufferMemoryBarrier bufferMemoryBarrier(
|
|
|
|
vk::AccessFlagBits::eTransferWrite,
|
|
|
|
vk::AccessFlagBits::eHostRead,
|
|
|
|
VK_QUEUE_FAMILY_IGNORED,
|
|
|
|
VK_QUEUE_FAMILY_IGNORED,
|
|
|
|
*colorAttachment->GetBufferData()->buffer,
|
|
|
|
0,
|
|
|
|
VK_WHOLE_SIZE);
|
|
|
|
currentCommandBuffer.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer,
|
|
|
|
vk::PipelineStageFlagBits::eHost, {}, nullptr, bufferMemoryBarrier, nullptr);
|
|
|
|
}
|
|
|
|
currentCommandBuffer.end();
|
|
|
|
|
|
|
|
currentCommandBuffer = nullptr;
|
|
|
|
commandPool->EndFrame();
|
|
|
|
|
2021-03-01 09:13:40 +00:00
|
|
|
if (config::RenderToTextureBuffer)
|
2019-10-09 19:16:12 +00:00
|
|
|
{
|
2019-11-29 18:28:22 +00:00
|
|
|
vk::Fence fence = commandPool->GetCurrentFence();
|
|
|
|
GetContext()->GetDevice().waitForFences(1, &fence, true, UINT64_MAX);
|
2019-10-09 19:16:12 +00:00
|
|
|
|
|
|
|
u16 *dst = (u16 *)&vram[textureAddr];
|
|
|
|
|
|
|
|
PixelBuffer<u32> tmpBuf;
|
2019-12-10 11:51:10 +00:00
|
|
|
tmpBuf.init(clippedWidth, clippedHeight);
|
|
|
|
colorAttachment->GetBufferData()->download(clippedWidth * clippedHeight * 4, tmpBuf.data());
|
|
|
|
WriteTextureToVRam(clippedWidth, clippedHeight, (u8 *)tmpBuf.data(), dst);
|
2019-10-09 19:16:12 +00:00
|
|
|
}
|
2020-12-15 14:09:42 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
//memset(&vram[fb_rtt.TexAddr << 3], '\0', size);
|
2019-10-09 19:16:12 +00:00
|
|
|
|
2020-12-15 14:09:42 +00:00
|
|
|
texture->dirty = 0;
|
2021-03-05 17:22:42 +00:00
|
|
|
libCore_vramlock_Lock(texture->sa_tex, texture->sa + texture->size - 1, texture);
|
2020-12-15 14:09:42 +00:00
|
|
|
}
|
|
|
|
Drawer::EndRenderPass();
|
2019-10-09 19:16:12 +00:00
|
|
|
}
|
2019-10-13 20:01:20 +00:00
|
|
|
|
2021-03-19 18:31:01 +00:00
|
|
|
void ScreenDrawer::Init(SamplerManager *samplerManager, ShaderManager *shaderManager, const vk::Extent2D& viewport)
|
2019-12-03 20:29:37 +00:00
|
|
|
{
|
|
|
|
this->shaderManager = shaderManager;
|
2019-12-04 14:51:26 +00:00
|
|
|
if (this->viewport != viewport)
|
|
|
|
{
|
|
|
|
framebuffers.clear();
|
|
|
|
colorAttachments.clear();
|
|
|
|
depthAttachment.reset();
|
2021-01-09 17:16:39 +00:00
|
|
|
transitionNeeded.clear();
|
|
|
|
clearNeeded.clear();
|
|
|
|
frameRendered = false;
|
2019-12-04 14:51:26 +00:00
|
|
|
}
|
|
|
|
this->viewport = viewport;
|
|
|
|
if (!depthAttachment)
|
|
|
|
{
|
|
|
|
depthAttachment = std::unique_ptr<FramebufferAttachment>(
|
|
|
|
new FramebufferAttachment(GetContext()->GetPhysicalDevice(), GetContext()->GetDevice()));
|
|
|
|
depthAttachment->Init(viewport.width, viewport.height, GetContext()->GetDepthFormat(),
|
|
|
|
vk::ImageUsageFlagBits::eDepthStencilAttachment | vk::ImageUsageFlagBits::eTransientAttachment);
|
|
|
|
}
|
2019-12-03 20:29:37 +00:00
|
|
|
|
2021-01-09 17:16:39 +00:00
|
|
|
if (!renderPassLoad)
|
2019-12-03 20:29:37 +00:00
|
|
|
{
|
|
|
|
vk::AttachmentDescription attachmentDescriptions[] = {
|
|
|
|
// Color attachment
|
|
|
|
vk::AttachmentDescription(vk::AttachmentDescriptionFlags(), GetContext()->GetColorFormat(), vk::SampleCountFlagBits::e1,
|
2020-12-15 14:09:42 +00:00
|
|
|
vk::AttachmentLoadOp::eLoad, vk::AttachmentStoreOp::eStore,
|
2019-12-03 20:29:37 +00:00
|
|
|
vk::AttachmentLoadOp::eDontCare, vk::AttachmentStoreOp::eDontCare,
|
2020-12-15 14:09:42 +00:00
|
|
|
vk::ImageLayout::eShaderReadOnlyOptimal, vk::ImageLayout::eShaderReadOnlyOptimal),
|
2019-12-03 20:29:37 +00:00
|
|
|
// Depth attachment
|
|
|
|
vk::AttachmentDescription(vk::AttachmentDescriptionFlags(), GetContext()->GetDepthFormat(), vk::SampleCountFlagBits::e1,
|
|
|
|
vk::AttachmentLoadOp::eClear, vk::AttachmentStoreOp::eDontCare,
|
|
|
|
vk::AttachmentLoadOp::eClear, vk::AttachmentStoreOp::eDontCare,
|
|
|
|
vk::ImageLayout::eUndefined, vk::ImageLayout::eDepthStencilAttachmentOptimal),
|
|
|
|
};
|
|
|
|
vk::AttachmentReference colorReference(0, vk::ImageLayout::eColorAttachmentOptimal);
|
|
|
|
vk::AttachmentReference depthReference(1, vk::ImageLayout::eDepthStencilAttachmentOptimal);
|
|
|
|
|
|
|
|
vk::SubpassDescription subpasses[] = {
|
|
|
|
vk::SubpassDescription(vk::SubpassDescriptionFlags(), vk::PipelineBindPoint::eGraphics,
|
|
|
|
0, nullptr,
|
|
|
|
1, &colorReference,
|
|
|
|
nullptr,
|
|
|
|
&depthReference),
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<vk::SubpassDependency> dependencies;
|
|
|
|
dependencies.emplace_back(0, VK_SUBPASS_EXTERNAL, vk::PipelineStageFlagBits::eColorAttachmentOutput, vk::PipelineStageFlagBits::eFragmentShader,
|
|
|
|
vk::AccessFlagBits::eColorAttachmentWrite, vk::AccessFlagBits::eShaderRead, vk::DependencyFlagBits::eByRegion);
|
|
|
|
|
2021-01-09 17:16:39 +00:00
|
|
|
renderPassLoad = GetContext()->GetDevice().createRenderPassUnique(vk::RenderPassCreateInfo(vk::RenderPassCreateFlags(),
|
|
|
|
ARRAY_SIZE(attachmentDescriptions), attachmentDescriptions,
|
|
|
|
ARRAY_SIZE(subpasses), subpasses,
|
|
|
|
dependencies.size(), dependencies.data()));
|
|
|
|
attachmentDescriptions[0].loadOp = vk::AttachmentLoadOp::eClear;
|
|
|
|
renderPassClear = GetContext()->GetDevice().createRenderPassUnique(vk::RenderPassCreateInfo(vk::RenderPassCreateFlags(),
|
2019-12-03 20:29:37 +00:00
|
|
|
ARRAY_SIZE(attachmentDescriptions), attachmentDescriptions,
|
|
|
|
ARRAY_SIZE(subpasses), subpasses,
|
|
|
|
dependencies.size(), dependencies.data()));
|
|
|
|
}
|
2020-12-15 14:09:42 +00:00
|
|
|
size_t size = GetSwapChainSize();
|
2019-12-03 20:29:37 +00:00
|
|
|
if (colorAttachments.size() > size)
|
|
|
|
{
|
|
|
|
colorAttachments.resize(size);
|
|
|
|
framebuffers.resize(size);
|
2021-01-09 17:16:39 +00:00
|
|
|
transitionNeeded.resize(size);
|
|
|
|
clearNeeded.resize(size);
|
2019-12-03 20:29:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vk::ImageView attachments[] = {
|
|
|
|
nullptr,
|
|
|
|
depthAttachment->GetImageView(),
|
|
|
|
};
|
|
|
|
while (colorAttachments.size() < size)
|
|
|
|
{
|
|
|
|
colorAttachments.push_back(std::unique_ptr<FramebufferAttachment>(
|
|
|
|
new FramebufferAttachment(GetContext()->GetPhysicalDevice(), GetContext()->GetDevice())));
|
|
|
|
colorAttachments.back()->Init(viewport.width, viewport.height, GetContext()->GetColorFormat(),
|
|
|
|
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eSampled);
|
|
|
|
attachments[0] = colorAttachments.back()->GetImageView();
|
2021-01-09 17:16:39 +00:00
|
|
|
vk::FramebufferCreateInfo createInfo(vk::FramebufferCreateFlags(), *renderPassLoad,
|
2019-12-03 20:29:37 +00:00
|
|
|
ARRAY_SIZE(attachments), attachments, viewport.width, viewport.height, 1);
|
|
|
|
framebuffers.push_back(GetContext()->GetDevice().createFramebufferUnique(createInfo));
|
2021-01-09 17:16:39 +00:00
|
|
|
transitionNeeded.push_back(true);
|
2021-01-16 17:50:54 +00:00
|
|
|
clearNeeded.push_back(true);
|
2019-12-03 20:29:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-09 17:16:39 +00:00
|
|
|
frameRendered = false;
|
2019-12-03 20:29:37 +00:00
|
|
|
|
|
|
|
if (!screenPipelineManager)
|
|
|
|
screenPipelineManager = std::unique_ptr<PipelineManager>(new PipelineManager());
|
2021-01-09 17:16:39 +00:00
|
|
|
screenPipelineManager->Init(shaderManager, *renderPassLoad);
|
2019-12-03 20:29:37 +00:00
|
|
|
Drawer::Init(samplerManager, screenPipelineManager.get());
|
|
|
|
}
|
|
|
|
|
2019-10-13 20:01:20 +00:00
|
|
|
vk::CommandBuffer ScreenDrawer::BeginRenderPass()
|
|
|
|
{
|
2019-11-29 18:28:22 +00:00
|
|
|
vk::CommandBuffer commandBuffer = commandPool->Allocate();
|
|
|
|
commandBuffer.begin(vk::CommandBufferBeginInfo(vk::CommandBufferUsageFlagBits::eOneTimeSubmit));
|
|
|
|
|
2021-01-09 17:16:39 +00:00
|
|
|
if (transitionNeeded[GetCurrentImage()])
|
2020-12-15 14:09:42 +00:00
|
|
|
{
|
2021-01-09 17:16:39 +00:00
|
|
|
setImageLayout(commandBuffer, colorAttachments[GetCurrentImage()]->GetImage(), GetContext()->GetColorFormat(),
|
|
|
|
1, vk::ImageLayout::eUndefined, vk::ImageLayout::eShaderReadOnlyOptimal);
|
|
|
|
transitionNeeded[GetCurrentImage()] = false;
|
2020-12-15 14:09:42 +00:00
|
|
|
}
|
|
|
|
|
2021-01-09 17:16:39 +00:00
|
|
|
vk::RenderPass renderPass = clearNeeded[GetCurrentImage()] ? *renderPassClear : *renderPassLoad;
|
|
|
|
clearNeeded[GetCurrentImage()] = false;
|
2019-11-29 18:28:22 +00:00
|
|
|
const vk::ClearValue clear_colors[] = { vk::ClearColorValue(std::array<float, 4> { 0.f, 0.f, 0.f, 1.f }), vk::ClearDepthStencilValue { 0.f, 0 } };
|
2021-01-09 17:16:39 +00:00
|
|
|
commandBuffer.beginRenderPass(vk::RenderPassBeginInfo(renderPass, *framebuffers[GetCurrentImage()],
|
2019-11-29 18:28:22 +00:00
|
|
|
vk::Rect2D( { 0, 0 }, viewport), 2, clear_colors), vk::SubpassContents::eInline);
|
2021-04-17 16:40:58 +00:00
|
|
|
commandBuffer.setViewport(0, vk::Viewport(0.0f, 0.0f, (float)viewport.width, (float)viewport.height, 1.0f, 0.0f));
|
2019-10-23 16:13:28 +00:00
|
|
|
|
2021-03-19 18:31:01 +00:00
|
|
|
matrices.CalcMatrices(&pvrrc, viewport.width, viewport.height);
|
2019-10-23 16:13:28 +00:00
|
|
|
|
2021-03-19 18:31:01 +00:00
|
|
|
SetBaseScissor(viewport);
|
2019-10-13 20:01:20 +00:00
|
|
|
commandBuffer.setScissor(0, baseScissor);
|
2019-11-29 18:28:22 +00:00
|
|
|
currentCommandBuffer = commandBuffer;
|
|
|
|
|
2019-10-13 20:01:20 +00:00
|
|
|
return commandBuffer;
|
|
|
|
}
|
2019-11-29 18:28:22 +00:00
|
|
|
|
|
|
|
void ScreenDrawer::EndRenderPass()
|
|
|
|
{
|
|
|
|
currentCommandBuffer.endRenderPass();
|
|
|
|
currentCommandBuffer.end();
|
2019-12-03 20:29:37 +00:00
|
|
|
currentCommandBuffer = nullptr;
|
2019-11-29 18:28:22 +00:00
|
|
|
commandPool->EndFrame();
|
2020-12-15 14:09:42 +00:00
|
|
|
Drawer::EndRenderPass();
|
|
|
|
frameRendered = true;
|
2019-11-29 18:28:22 +00:00
|
|
|
}
|