ftpiiu_plugin/source/switch/imgui_deko3d.cpp

540 lines
17 KiB
C++
Raw Normal View History

2020-04-05 21:16:16 +02:00
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
2020-05-16 03:35:43 +02:00
// The MIT License (MIT)
//
2020-04-05 21:16:16 +02:00
// Copyright (C) 2020 Michael Theall
//
2020-05-16 03:35:43 +02:00
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
2020-04-05 21:16:16 +02:00
//
2020-05-16 03:35:43 +02:00
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
2020-04-05 21:16:16 +02:00
//
2020-05-16 03:35:43 +02:00
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
2020-04-05 21:16:16 +02:00
2020-04-17 22:32:39 +02:00
#ifndef CLASSIC
2020-04-05 21:16:16 +02:00
#include "imgui_deko3d.h"
#include "fs.h"
2020-04-09 06:59:24 +02:00
#include "imgui.h"
2020-04-05 21:16:16 +02:00
2020-04-09 06:59:24 +02:00
#include <deko3d.hpp>
2020-04-05 21:16:16 +02:00
#define GLM_FORCE_DEFAULT_ALIGNED_GENTYPES
#define GLM_FORCE_INTRINSICS
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/mat4x4.hpp>
#include <sys/stat.h>
#include <unistd.h>
2020-04-09 06:59:24 +02:00
#include <array>
2020-04-05 21:16:16 +02:00
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <numeric>
2020-04-09 06:59:24 +02:00
#include <optional>
#include <vector>
2020-04-05 21:16:16 +02:00
namespace
{
2020-04-09 06:59:24 +02:00
/// \brief Vertex buffer size
constexpr auto VTXBUF_SIZE = 1024u * 1024u;
2020-04-06 07:36:03 +02:00
/// \brief Index buffer size
2020-04-09 06:59:24 +02:00
constexpr auto IDXBUF_SIZE = 1024u * 1024u;
2020-04-05 21:16:16 +02:00
2020-04-06 07:36:03 +02:00
/// \brief Vertex shader UBO
2020-04-05 21:16:16 +02:00
struct VertUBO
{
2020-04-06 07:36:03 +02:00
/// \brief Projection matrix
2020-04-05 21:16:16 +02:00
glm::mat4 projMtx;
};
2020-04-06 07:36:03 +02:00
/// \brief Fragment shader UBO
2020-04-05 21:16:16 +02:00
struct FragUBO
{
2020-04-06 07:36:03 +02:00
/// \brief Whether drawing a font or not
2020-04-05 21:16:16 +02:00
std::uint32_t font;
};
2020-04-06 07:36:03 +02:00
/// \brief Vertex attribute state
constexpr std::array VERTEX_ATTRIB_STATE = {
2020-04-05 21:16:16 +02:00
// clang-format off
DkVtxAttribState{0, 0, offsetof (ImDrawVert, pos), DkVtxAttribSize_2x32, DkVtxAttribType_Float, 0},
DkVtxAttribState{0, 0, offsetof (ImDrawVert, uv), DkVtxAttribSize_2x32, DkVtxAttribType_Float, 0},
DkVtxAttribState{0, 0, offsetof (ImDrawVert, col), DkVtxAttribSize_4x8, DkVtxAttribType_Unorm, 0},
// clang-format on
};
2020-04-06 07:36:03 +02:00
/// \brief Vertex buffer state
constexpr std::array VERTEX_BUFFER_STATE = {
2020-04-05 21:16:16 +02:00
DkVtxBufferState{sizeof (ImDrawVert), 0},
};
2020-04-06 07:36:03 +02:00
/// \brief Shader code memblock
2020-04-05 21:16:16 +02:00
dk::UniqueMemBlock s_codeMemBlock;
2020-04-06 07:36:03 +02:00
/// \brief Shaders (vertex, fragment)
2020-04-05 21:16:16 +02:00
dk::Shader s_shaders[2];
2020-04-06 07:36:03 +02:00
/// \brief UBO memblock
2020-04-05 21:16:16 +02:00
dk::UniqueMemBlock s_uboMemBlock;
2020-04-06 07:36:03 +02:00
/// \brief Vertex data memblock
2020-04-09 06:59:24 +02:00
std::vector<dk::UniqueMemBlock> s_vtxMemBlock;
2020-04-06 07:36:03 +02:00
/// \brief Index data memblock
2020-04-09 06:59:24 +02:00
std::vector<dk::UniqueMemBlock> s_idxMemBlock;
2020-04-05 21:16:16 +02:00
2020-04-09 06:59:24 +02:00
/// \brief Font image memblock
dk::UniqueMemBlock s_fontImageMemBlock;
/// \brief Font texture handle
DkResHandle s_fontTextureHandle;
2020-04-05 21:16:16 +02:00
2020-04-06 07:36:03 +02:00
/// \brief Load shader code
2020-04-09 06:59:24 +02:00
void loadShaders (dk::UniqueDevice &device_)
2020-04-05 21:16:16 +02:00
{
2020-04-06 07:36:03 +02:00
/// \brief Shader file descriptor
2020-04-05 21:16:16 +02:00
struct ShaderFile
{
2020-04-06 07:36:03 +02:00
/// \brief Parameterized constructor
/// \param shader_ Shader object
/// \param path_ Path to source code
2020-04-05 21:16:16 +02:00
ShaderFile (dk::Shader &shader_, char const *const path_)
: shader (shader_), path (path_), size (getSize (path_))
{
}
2020-04-06 07:36:03 +02:00
/// \brief Get size of a file
/// \param path_ Path to file
2020-04-05 21:16:16 +02:00
static std::size_t getSize (char const *const path_)
{
struct stat st;
2020-04-09 06:59:24 +02:00
auto const rc = ::stat (path_, &st);
2020-04-05 21:16:16 +02:00
if (rc != 0)
{
std::fprintf (stderr, "stat(%s): %s\n", path_, std::strerror (errno));
std::abort ();
}
return st.st_size;
}
2020-04-06 07:36:03 +02:00
/// \brief Shader object
2020-04-05 21:16:16 +02:00
dk::Shader &shader;
2020-04-06 07:36:03 +02:00
/// \brief Path to source code
2020-04-05 21:16:16 +02:00
char const *const path;
2020-04-06 07:36:03 +02:00
/// \brief Source code file size
2020-04-05 21:16:16 +02:00
std::size_t const size;
};
auto shaderFiles = {ShaderFile{s_shaders[0], "romfs:/shaders/imgui_vsh.dksh"},
ShaderFile{s_shaders[1], "romfs:/shaders/imgui_fsh.dksh"}};
2020-04-06 07:36:03 +02:00
// calculate total size of shaders
2020-04-05 21:16:16 +02:00
auto const codeSize = std::accumulate (std::begin (shaderFiles),
std::end (shaderFiles),
DK_SHADER_CODE_UNUSABLE_SIZE,
[] (auto const sum_, auto const &file_) {
2020-04-09 06:59:24 +02:00
return sum_ + imgui::deko3d::align (file_.size, DK_SHADER_CODE_ALIGNMENT);
2020-04-05 21:16:16 +02:00
});
2020-04-06 07:36:03 +02:00
// create shader code memblock
2020-04-09 06:59:24 +02:00
s_codeMemBlock =
dk::MemBlockMaker{device_, imgui::deko3d::align (codeSize, DK_MEMBLOCK_ALIGNMENT)}
.setFlags (
DkMemBlockFlags_CpuUncached | DkMemBlockFlags_GpuCached | DkMemBlockFlags_Code)
.create ();
2020-04-05 21:16:16 +02:00
auto const addr = static_cast<std::uint8_t *> (s_codeMemBlock.getCpuAddr ());
std::size_t offset = 0;
2020-04-06 07:36:03 +02:00
// read shaders into memblock
2020-04-05 21:16:16 +02:00
for (auto &file : shaderFiles)
{
std::uint32_t const codeOffset = offset;
fs::File fp;
if (!fp.open (file.path))
{
std::fprintf (stderr, "open(%s): %s\n", file.path, std::strerror (errno));
std::abort ();
}
if (!fp.readAll (&addr[offset], file.size))
{
std::fprintf (stderr, "read(%s): %s\n", file.path, std::strerror (errno));
std::abort ();
}
dk::ShaderMaker{s_codeMemBlock, codeOffset}.initialize (file.shader);
2020-04-09 06:59:24 +02:00
offset = imgui::deko3d::align (offset + file.size, DK_SHADER_CODE_ALIGNMENT);
2020-04-05 21:16:16 +02:00
}
}
2020-04-06 07:36:03 +02:00
/// \brief Setup render state
2020-04-09 06:59:24 +02:00
/// \param cmdBuf_ Command buffer
2020-04-06 07:36:03 +02:00
/// \param drawData_ Data to draw
/// \param width_ Framebuffer width
/// \param height_ Framebuffer height
2020-04-09 06:59:24 +02:00
DkCmdList setupRenderState (dk::UniqueCmdBuf &cmdBuf_,
2020-04-05 21:16:16 +02:00
ImDrawData *const drawData_,
unsigned const width_,
unsigned const height_)
{
2020-04-06 07:36:03 +02:00
// setup viewport, orthographic projection matrix
// our visible imgui space lies from drawData_->DisplayPos (top left) to
2020-04-05 21:16:16 +02:00
// drawData_->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single
// viewport apps.
auto const L = drawData_->DisplayPos.x;
auto const R = drawData_->DisplayPos.x + drawData_->DisplaySize.x;
auto const T = drawData_->DisplayPos.y;
auto const B = drawData_->DisplayPos.y + drawData_->DisplaySize.y;
VertUBO vertUBO;
vertUBO.projMtx = glm::orthoRH_ZO (L, R, B, T, -1.0f, 1.0f);
2020-04-06 07:36:03 +02:00
// create command buffer to initialize/reset render state
2020-04-09 06:59:24 +02:00
cmdBuf_.setViewports (0, DkViewport{0.0f, 0.0f, width_, height_});
cmdBuf_.bindShaders (DkStageFlag_GraphicsMask, {&s_shaders[0], &s_shaders[1]});
cmdBuf_.bindUniformBuffer (DkStage_Vertex,
2020-04-05 21:16:16 +02:00
0,
s_uboMemBlock.getGpuAddr (),
2020-04-09 06:59:24 +02:00
imgui::deko3d::align (sizeof (VertUBO), DK_UNIFORM_BUF_ALIGNMENT));
cmdBuf_.pushConstants (s_uboMemBlock.getGpuAddr (),
imgui::deko3d::align (sizeof (VertUBO), DK_UNIFORM_BUF_ALIGNMENT),
2020-04-05 21:16:16 +02:00
0,
sizeof (VertUBO),
&vertUBO);
2020-04-09 06:59:24 +02:00
cmdBuf_.bindUniformBuffer (DkStage_Fragment,
2020-04-05 21:16:16 +02:00
0,
2020-04-09 06:59:24 +02:00
s_uboMemBlock.getGpuAddr () +
imgui::deko3d::align (sizeof (VertUBO), DK_UNIFORM_BUF_ALIGNMENT),
imgui::deko3d::align (sizeof (FragUBO), DK_UNIFORM_BUF_ALIGNMENT));
cmdBuf_.bindRasterizerState (dk::RasterizerState{}.setCullMode (DkFace_None));
cmdBuf_.bindColorState (dk::ColorState{}.setBlendEnable (0, true));
cmdBuf_.bindColorWriteState (dk::ColorWriteState{});
cmdBuf_.bindDepthStencilState (dk::DepthStencilState{}.setDepthTestEnable (false));
cmdBuf_.bindBlendStates (0,
2020-04-05 21:16:16 +02:00
dk::BlendState{}.setFactors (DkBlendFactor_SrcAlpha,
DkBlendFactor_InvSrcAlpha,
DkBlendFactor_InvSrcAlpha,
DkBlendFactor_Zero));
2020-04-09 06:59:24 +02:00
cmdBuf_.bindVtxAttribState (VERTEX_ATTRIB_STATE);
cmdBuf_.bindVtxBufferState (VERTEX_BUFFER_STATE);
2020-04-05 21:16:16 +02:00
2020-04-09 06:59:24 +02:00
return cmdBuf_.finishList ();
2020-04-05 21:16:16 +02:00
}
}
2020-04-09 06:59:24 +02:00
void imgui::deko3d::init (dk::UniqueDevice &device_,
dk::UniqueQueue &queue_,
dk::UniqueCmdBuf &cmdBuf_,
dk::SamplerDescriptor &samplerDescriptor_,
dk::ImageDescriptor &imageDescriptor_,
DkResHandle fontTextureHandle_,
unsigned const imageCount_)
2020-04-05 21:16:16 +02:00
{
auto &io = ImGui::GetIO ();
2020-04-05 21:16:16 +02:00
// setup back-end capabilities flags
2020-04-05 21:16:16 +02:00
io.BackendRendererName = "deko3d";
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset;
2020-04-06 07:36:03 +02:00
// load shader code
2020-04-09 06:59:24 +02:00
loadShaders (device_);
2020-04-05 21:16:16 +02:00
2020-04-06 07:36:03 +02:00
// create UBO memblock
2020-04-09 06:59:24 +02:00
s_uboMemBlock = dk::MemBlockMaker{device_,
2020-04-05 21:16:16 +02:00
align (align (sizeof (VertUBO), DK_UNIFORM_BUF_ALIGNMENT) +
align (sizeof (FragUBO), DK_UNIFORM_BUF_ALIGNMENT),
DK_MEMBLOCK_ALIGNMENT)}
.setFlags (DkMemBlockFlags_CpuUncached | DkMemBlockFlags_GpuCached)
.create ();
2020-04-09 06:59:24 +02:00
// create memblocks for each image slot
for (std::size_t i = 0; i < imageCount_; ++i)
2020-04-05 21:16:16 +02:00
{
2020-04-06 07:36:03 +02:00
// create vertex data memblock
2020-04-09 06:59:24 +02:00
s_vtxMemBlock.emplace_back (
dk::MemBlockMaker{device_, align (VTXBUF_SIZE, DK_MEMBLOCK_ALIGNMENT)}
.setFlags (DkMemBlockFlags_CpuUncached | DkMemBlockFlags_GpuCached)
.create ());
2020-04-05 21:16:16 +02:00
2020-04-06 07:36:03 +02:00
// create index data memblock
2020-04-09 06:59:24 +02:00
s_idxMemBlock.emplace_back (
dk::MemBlockMaker{device_, align (IDXBUF_SIZE, DK_MEMBLOCK_ALIGNMENT)}
.setFlags (DkMemBlockFlags_CpuUncached | DkMemBlockFlags_GpuCached)
.create ());
2020-04-05 21:16:16 +02:00
}
2020-04-06 07:36:03 +02:00
// get texture atlas
2020-04-09 06:59:24 +02:00
io.Fonts->SetTexID (makeTextureID (fontTextureHandle_));
s_fontTextureHandle = fontTextureHandle_;
2020-04-05 21:16:16 +02:00
unsigned char *pixels;
int width;
int height;
io.Fonts->GetTexDataAsAlpha8 (&pixels, &width, &height);
2020-04-06 07:36:03 +02:00
// create memblock for transfer
2020-04-05 21:16:16 +02:00
dk::UniqueMemBlock memBlock =
2020-04-09 06:59:24 +02:00
dk::MemBlockMaker{device_, align (width * height, DK_MEMBLOCK_ALIGNMENT)}
2020-04-05 21:16:16 +02:00
.setFlags (DkMemBlockFlags_CpuUncached | DkMemBlockFlags_GpuCached)
.create ();
std::memcpy (memBlock.getCpuAddr (), pixels, width * height);
2020-04-06 07:36:03 +02:00
// initialize sampler descriptor
2020-04-09 06:59:24 +02:00
samplerDescriptor_.initialize (
2020-04-05 21:16:16 +02:00
dk::Sampler{}
.setFilter (DkFilter_Linear, DkFilter_Linear)
.setWrapMode (DkWrapMode_ClampToEdge, DkWrapMode_ClampToEdge, DkWrapMode_ClampToEdge));
2020-04-06 07:36:03 +02:00
// initialize texture atlas image layout
2020-04-05 21:16:16 +02:00
dk::ImageLayout layout;
2020-04-09 06:59:24 +02:00
dk::ImageLayoutMaker{device_}
2020-04-05 21:16:16 +02:00
.setFlags (0)
.setFormat (DkImageFormat_R8_Unorm)
.setDimensions (width, height)
.initialize (layout);
2020-04-09 06:59:24 +02:00
auto const fontAlign = layout.getAlignment ();
auto const fontSize = layout.getSize ();
2020-04-05 21:16:16 +02:00
2020-04-09 06:59:24 +02:00
// create image memblock
s_fontImageMemBlock = dk::MemBlockMaker{device_,
align (fontSize, std::max<unsigned> (fontAlign, DK_MEMBLOCK_ALIGNMENT))}
.setFlags (DkMemBlockFlags_GpuCached | DkMemBlockFlags_Image)
.create ();
2020-04-05 21:16:16 +02:00
2020-04-09 06:59:24 +02:00
// initialize font texture atlas image descriptor
dk::Image fontTexture;
fontTexture.initialize (layout, s_fontImageMemBlock, 0);
imageDescriptor_.initialize (fontTexture);
2020-04-05 21:16:16 +02:00
2020-04-09 06:59:24 +02:00
// copy font texture atlas to image view
dk::ImageView imageView{fontTexture};
cmdBuf_.copyBufferToImage ({memBlock.getGpuAddr ()}, imageView, {0, 0, 0, width, height, 1});
2020-04-05 21:16:16 +02:00
2020-04-09 06:59:24 +02:00
// submit commands to transfer font texture
queue_.submitCommands (cmdBuf_.finishList ());
2020-04-05 21:16:16 +02:00
2020-04-09 06:59:24 +02:00
// wait for commands to complete before releasing memblock
queue_.waitIdle ();
}
2020-04-05 21:16:16 +02:00
2020-04-09 06:59:24 +02:00
void imgui::deko3d::exit ()
{
s_fontImageMemBlock = nullptr;
2020-04-05 21:16:16 +02:00
2020-04-09 06:59:24 +02:00
s_idxMemBlock.clear ();
s_vtxMemBlock.clear ();
2020-04-05 21:16:16 +02:00
2020-04-09 06:59:24 +02:00
s_uboMemBlock = nullptr;
s_codeMemBlock = nullptr;
2020-04-05 21:16:16 +02:00
}
2020-04-09 06:59:24 +02:00
void imgui::deko3d::render (dk::UniqueDevice &device_,
dk::UniqueQueue &queue_,
dk::UniqueCmdBuf &cmdBuf_,
unsigned const slot_)
2020-04-05 21:16:16 +02:00
{
2020-04-06 07:36:03 +02:00
// get ImGui draw data
2020-04-05 21:16:16 +02:00
auto const drawData = ImGui::GetDrawData ();
if (drawData->CmdListsCount <= 0)
return;
2020-04-06 07:36:03 +02:00
// get framebuffer dimensions
2020-04-05 21:16:16 +02:00
unsigned width = drawData->DisplaySize.x * drawData->FramebufferScale.x;
unsigned height = drawData->DisplaySize.y * drawData->FramebufferScale.y;
if (width <= 0 || height <= 0)
return;
2020-04-06 07:36:03 +02:00
// setup desired render state
2020-04-09 06:59:24 +02:00
auto const setupCmd = setupRenderState (cmdBuf_, drawData, width, height);
queue_.submitCommands (setupCmd);
2020-04-05 21:16:16 +02:00
2020-04-09 06:59:24 +02:00
// currently bound texture
std::optional<DkResHandle> boundTextureHandle;
2020-04-05 21:16:16 +02:00
2020-04-06 07:36:03 +02:00
// will project scissor/clipping rectangles into framebuffer space
2020-04-05 21:16:16 +02:00
// (0,0) unless using multi-viewports
auto const clipOff = drawData->DisplayPos;
// (1,1) unless using retina display which are often (2,2)
auto const clipScale = drawData->FramebufferScale;
2020-04-06 07:36:03 +02:00
// check if we need to grow vertex data memblock
2020-04-09 06:59:24 +02:00
if (s_vtxMemBlock[slot_].getSize () < drawData->TotalVtxCount * sizeof (ImDrawVert))
2020-04-05 21:16:16 +02:00
{
2020-04-09 06:59:24 +02:00
// add 10% to avoid growing many frames in a row
std::size_t const count = drawData->TotalVtxCount * 1.1f;
s_vtxMemBlock[slot_] =
dk::MemBlockMaker{device_, align (count * sizeof (ImDrawVert), DK_MEMBLOCK_ALIGNMENT)}
2020-04-05 21:16:16 +02:00
.setFlags (DkMemBlockFlags_CpuUncached | DkMemBlockFlags_GpuCached)
.create ();
}
2020-04-06 07:36:03 +02:00
// check if we need to grow index data memblock
2020-04-09 06:59:24 +02:00
if (s_idxMemBlock[slot_].getSize () < drawData->TotalIdxCount * sizeof (ImDrawIdx))
2020-04-05 21:16:16 +02:00
{
2020-04-09 06:59:24 +02:00
// add 10% to avoid growing many frames in a row
std::size_t const count = drawData->TotalIdxCount * 1.1f;
s_idxMemBlock[slot_] =
dk::MemBlockMaker{device_, align (count * sizeof (ImDrawIdx), DK_MEMBLOCK_ALIGNMENT)}
2020-04-05 21:16:16 +02:00
.setFlags (DkMemBlockFlags_CpuUncached | DkMemBlockFlags_GpuCached)
.create ();
}
2020-04-06 07:36:03 +02:00
// get base cpu addresses
2020-04-09 06:59:24 +02:00
auto const cpuVtx = static_cast<std::uint8_t *> (s_vtxMemBlock[slot_].getCpuAddr ());
auto const cpuIdx = static_cast<std::uint8_t *> (s_idxMemBlock[slot_].getCpuAddr ());
2020-04-05 21:16:16 +02:00
2020-04-06 07:36:03 +02:00
// get base gpu addresses
2020-04-09 06:59:24 +02:00
auto const gpuVtx = s_vtxMemBlock[slot_].getGpuAddr ();
auto const gpuIdx = s_idxMemBlock[slot_].getGpuAddr ();
2020-04-05 21:16:16 +02:00
2020-04-06 07:36:03 +02:00
// get memblock sizes
2020-04-09 06:59:24 +02:00
auto const sizeVtx = s_vtxMemBlock[slot_].getSize ();
auto const sizeIdx = s_idxMemBlock[slot_].getSize ();
2020-04-05 21:16:16 +02:00
2020-04-06 07:36:03 +02:00
// bind vertex/index data memblocks
2020-04-09 06:59:24 +02:00
static_assert (sizeof (ImDrawIdx) == sizeof (std::uint16_t));
cmdBuf_.bindVtxBuffer (0, gpuVtx, sizeVtx);
cmdBuf_.bindIdxBuffer (DkIdxFormat_Uint16, gpuIdx);
2020-04-05 21:16:16 +02:00
2020-04-06 07:36:03 +02:00
// render command lists
2020-04-05 21:16:16 +02:00
std::size_t offsetVtx = 0;
std::size_t offsetIdx = 0;
for (int i = 0; i < drawData->CmdListsCount; ++i)
{
auto const &cmdList = *drawData->CmdLists[i];
auto const vtxSize = cmdList.VtxBuffer.Size * sizeof (ImDrawVert);
auto const idxSize = cmdList.IdxBuffer.Size * sizeof (ImDrawIdx);
2020-04-06 07:36:03 +02:00
// double check that we don't overrun vertex data memblock
2020-04-05 21:16:16 +02:00
if (sizeVtx - offsetVtx < vtxSize)
{
std::fprintf (stderr, "Not enough vertex buffer\n");
std::fprintf (stderr, "\t%zu/%u used, need %zu\n", offsetVtx, sizeVtx, vtxSize);
continue;
}
2020-04-06 07:36:03 +02:00
// double check that we don't overrun index data memblock
2020-04-05 21:16:16 +02:00
if (sizeIdx - offsetIdx < idxSize)
{
std::fprintf (stderr, "Not enough index buffer\n");
std::fprintf (stderr, "\t%zu/%u used, need %zu\n", offsetIdx, sizeIdx, idxSize);
continue;
}
2020-04-06 07:36:03 +02:00
// copy vertex/index data into memblocks
2020-04-05 21:16:16 +02:00
std::memcpy (cpuVtx + offsetVtx, cmdList.VtxBuffer.Data, vtxSize);
std::memcpy (cpuIdx + offsetIdx, cmdList.IdxBuffer.Data, idxSize);
for (auto const &cmd : cmdList.CmdBuffer)
{
if (cmd.UserCallback)
{
2020-04-06 07:36:03 +02:00
// submit commands to preserve ordering
2020-04-09 06:59:24 +02:00
queue_.submitCommands (cmdBuf_.finishList ());
2020-04-05 21:16:16 +02:00
2020-04-06 07:36:03 +02:00
// user callback, registered via ImDrawList::AddCallback()
2020-04-05 21:16:16 +02:00
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to
// request the renderer to reset render state.)
if (cmd.UserCallback == ImDrawCallback_ResetRenderState)
2020-04-09 06:59:24 +02:00
queue_.submitCommands (setupCmd);
2020-04-05 21:16:16 +02:00
else
cmd.UserCallback (&cmdList, &cmd);
}
else
{
2020-04-06 07:36:03 +02:00
// project scissor/clipping rectangles into framebuffer space
2020-04-05 21:16:16 +02:00
ImVec4 clip;
clip.x = (cmd.ClipRect.x - clipOff.x) * clipScale.x;
clip.y = (cmd.ClipRect.y - clipOff.y) * clipScale.y;
clip.z = (cmd.ClipRect.z - clipOff.x) * clipScale.x;
clip.w = (cmd.ClipRect.w - clipOff.y) * clipScale.y;
2020-04-09 06:59:24 +02:00
// check if clip coordinate are outside of the framebuffer
2020-04-06 07:36:03 +02:00
if (clip.x >= width || clip.y >= height || clip.z < 0.0f || clip.w < 0.0f)
continue;
// keep scissor coordinates inside viewport
if (clip.x < 0.0f)
clip.x = 0.0f;
if (clip.y < 0.0f)
clip.y = 0.0f;
if (clip.z > width)
clip.z = width;
if (clip.w > height)
clip.z = height;
// apply scissor boundaries
2020-04-09 06:59:24 +02:00
cmdBuf_.setScissors (
2020-04-06 07:36:03 +02:00
0, DkScissor{clip.x, clip.y, clip.z - clip.x, clip.w - clip.y});
2020-04-09 06:59:24 +02:00
// get texture handle
auto const textureHandle = reinterpret_cast<std::uintptr_t> (cmd.TextureId);
2020-04-06 07:36:03 +02:00
// check if we need to bind a new texture
2020-04-09 06:59:24 +02:00
if (!boundTextureHandle || textureHandle != *boundTextureHandle)
2020-04-05 21:16:16 +02:00
{
2020-04-09 06:59:24 +02:00
// check if this is the first draw or changing to or from the font texture
if (!boundTextureHandle || textureHandle == s_fontTextureHandle ||
*boundTextureHandle == s_fontTextureHandle)
{
FragUBO fragUBO;
fragUBO.font = (textureHandle == s_fontTextureHandle);
// update fragment shader UBO
cmdBuf_.pushConstants (
s_uboMemBlock.getGpuAddr () +
align (sizeof (VertUBO), DK_UNIFORM_BUF_ALIGNMENT),
align (sizeof (FragUBO), DK_UNIFORM_BUF_ALIGNMENT),
0,
sizeof (FragUBO),
&fragUBO);
}
boundTextureHandle = textureHandle;
2020-04-06 07:36:03 +02:00
// bind the new texture
2020-04-09 06:59:24 +02:00
cmdBuf_.bindTextures (DkStage_Fragment, 0, textureHandle);
2020-04-05 21:16:16 +02:00
}
2020-04-06 07:36:03 +02:00
// draw the draw list
2020-04-09 06:59:24 +02:00
cmdBuf_.drawIndexed (DkPrimitive_Triangles,
2020-04-06 07:36:03 +02:00
cmd.ElemCount,
1,
cmd.IdxOffset + offsetIdx / sizeof (ImDrawIdx),
cmd.VtxOffset + offsetVtx / sizeof (ImDrawVert),
0);
2020-04-05 21:16:16 +02:00
}
}
offsetVtx += vtxSize;
offsetIdx += idxSize;
}
2020-04-06 07:36:03 +02:00
// submit final commands
2020-04-09 06:59:24 +02:00
queue_.submitCommands (cmdBuf_.finishList ());
2020-04-05 21:16:16 +02:00
}
2020-04-17 22:32:39 +02:00
#endif