Add imgui pass
This commit is contained in:
parent
7ba8f776e4
commit
0d939eacdf
22 changed files with 32344 additions and 136 deletions
1
3rdparty/CMakeLists.txt
vendored
1
3rdparty/CMakeLists.txt
vendored
|
@ -1,2 +1,3 @@
|
||||||
|
add_subdirectory(imgui)
|
||||||
add_subdirectory(nlohmann)
|
add_subdirectory(nlohmann)
|
||||||
add_subdirectory(stb)
|
add_subdirectory(stb)
|
||||||
|
|
13
3rdparty/imgui/CMakeLists.txt
vendored
Normal file
13
3rdparty/imgui/CMakeLists.txt
vendored
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
add_library(imgui
|
||||||
|
src/imgui.cpp
|
||||||
|
src/imgui_demo.cpp
|
||||||
|
src/imgui_draw.cpp
|
||||||
|
src/imgui_widgets.cpp)
|
||||||
|
target_link_libraries(imgui
|
||||||
|
PRIVATE
|
||||||
|
stb)
|
||||||
|
target_include_directories(imgui
|
||||||
|
PUBLIC
|
||||||
|
include)
|
||||||
|
|
||||||
|
add_library(imgui::imgui ALIAS imgui)
|
72
3rdparty/imgui/include/imconfig.h
vendored
Normal file
72
3rdparty/imgui/include/imconfig.h
vendored
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// COMPILE-TIME OPTIONS FOR DEAR IMGUI
|
||||||
|
// Runtime options (clipboard callbacks, enabling various features, etc.) can generally be set via the ImGuiIO structure.
|
||||||
|
// You can use ImGui::SetAllocatorFunctions() before calling ImGui::CreateContext() to rewire memory allocation functions.
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// A) You may edit imconfig.h (and not overwrite it when updating imgui, or maintain a patch/branch with your modifications to imconfig.h)
|
||||||
|
// B) or add configuration directives in your own file and compile with #define IMGUI_USER_CONFIG "myfilename.h"
|
||||||
|
// If you do so you need to make sure that configuration settings are defined consistently _everywhere_ dear imgui is used, which include
|
||||||
|
// the imgui*.cpp files but also _any_ of your code that uses imgui. This is because some compile-time options have an affect on data structures.
|
||||||
|
// Defining those options in imconfig.h will ensure every compilation unit gets to see the same data structure layouts.
|
||||||
|
// Call IMGUI_CHECKVERSION() from your .cpp files to verify that the data structures your files are using are matching the ones imgui.cpp is using.
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
//---- Define assertion handler. Defaults to calling assert().
|
||||||
|
//#define IM_ASSERT(_EXPR) MyAssert(_EXPR)
|
||||||
|
//#define IM_ASSERT(_EXPR) ((void)(_EXPR)) // Disable asserts
|
||||||
|
|
||||||
|
//---- Define attributes of all API symbols declarations, e.g. for DLL under Windows.
|
||||||
|
//#define IMGUI_API __declspec( dllexport )
|
||||||
|
//#define IMGUI_API __declspec( dllimport )
|
||||||
|
|
||||||
|
//---- Don't define obsolete functions/enums names. Consider enabling from time to time after updating to avoid using soon-to-be obsolete function/names.
|
||||||
|
//#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS
|
||||||
|
|
||||||
|
//---- Don't implement demo windows functionality (ShowDemoWindow()/ShowStyleEditor()/ShowUserGuide() methods will be empty)
|
||||||
|
//---- It is very strongly recommended to NOT disable the demo windows during development. Please read the comments in imgui_demo.cpp.
|
||||||
|
//#define IMGUI_DISABLE_DEMO_WINDOWS
|
||||||
|
|
||||||
|
//---- Don't implement some functions to reduce linkage requirements.
|
||||||
|
//#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS // [Win32] Don't implement default clipboard handler. Won't use and link with OpenClipboard/GetClipboardData/CloseClipboard etc.
|
||||||
|
//#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] Don't implement default IME handler. Won't use and link with ImmGetContext/ImmSetCompositionWindow.
|
||||||
|
//#define IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS // Don't implement ImFormatString/ImFormatStringV so you can implement them yourself if you don't want to link with vsnprintf.
|
||||||
|
//#define IMGUI_DISABLE_MATH_FUNCTIONS // Don't implement ImFabs/ImSqrt/ImPow/ImFmod/ImCos/ImSin/ImAcos/ImAtan2 wrapper so you can implement them yourself. Declare your prototypes in imconfig.h.
|
||||||
|
//#define IMGUI_DISABLE_DEFAULT_ALLOCATORS // Don't implement default allocators calling malloc()/free() to avoid linking with them. You will need to call ImGui::SetAllocatorFunctions().
|
||||||
|
|
||||||
|
//---- Include imgui_user.h at the end of imgui.h as a convenience
|
||||||
|
//#define IMGUI_INCLUDE_IMGUI_USER_H
|
||||||
|
|
||||||
|
//---- Pack colors to BGRA8 instead of RGBA8 (to avoid converting from one to another)
|
||||||
|
//#define IMGUI_USE_BGRA_PACKED_COLOR
|
||||||
|
|
||||||
|
//---- Avoid multiple STB libraries implementations, or redefine path/filenames to prioritize another version
|
||||||
|
// By default the embedded implementations are declared static and not available outside of imgui cpp files.
|
||||||
|
//#define IMGUI_STB_TRUETYPE_FILENAME "my_folder/stb_truetype.h"
|
||||||
|
//#define IMGUI_STB_RECT_PACK_FILENAME "my_folder/stb_rect_pack.h"
|
||||||
|
//#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION
|
||||||
|
//#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION
|
||||||
|
|
||||||
|
//---- Define constructor and implicit cast operators to convert back<>forth between your math types and ImVec2/ImVec4.
|
||||||
|
// This will be inlined as part of ImVec2 and ImVec4 class declarations.
|
||||||
|
/*
|
||||||
|
#define IM_VEC2_CLASS_EXTRA \
|
||||||
|
ImVec2(const MyVec2& f) { x = f.x; y = f.y; } \
|
||||||
|
operator MyVec2() const { return MyVec2(x,y); }
|
||||||
|
|
||||||
|
#define IM_VEC4_CLASS_EXTRA \
|
||||||
|
ImVec4(const MyVec4& f) { x = f.x; y = f.y; z = f.z; w = f.w; } \
|
||||||
|
operator MyVec4() const { return MyVec4(x,y,z,w); }
|
||||||
|
*/
|
||||||
|
|
||||||
|
//---- Use 32-bit vertex indices (default is 16-bit) to allow meshes with more than 64K vertices. Render function needs to support it.
|
||||||
|
//#define ImDrawIdx unsigned int
|
||||||
|
|
||||||
|
//---- Tip: You can add extra functions within the ImGui:: namespace, here or in your own headers files.
|
||||||
|
/*
|
||||||
|
namespace ImGui
|
||||||
|
{
|
||||||
|
void MyFunction(const char* name, const MyMatrix44& v);
|
||||||
|
}
|
||||||
|
*/
|
1974
3rdparty/imgui/include/imgui.h
vendored
Normal file
1974
3rdparty/imgui/include/imgui.h
vendored
Normal file
File diff suppressed because it is too large
Load diff
1291
3rdparty/imgui/include/imgui_internal.h
vendored
Normal file
1291
3rdparty/imgui/include/imgui_internal.h
vendored
Normal file
File diff suppressed because it is too large
Load diff
8970
3rdparty/imgui/src/imgui.cpp
vendored
Normal file
8970
3rdparty/imgui/src/imgui.cpp
vendored
Normal file
File diff suppressed because it is too large
Load diff
3589
3rdparty/imgui/src/imgui_demo.cpp
vendored
Normal file
3589
3rdparty/imgui/src/imgui_demo.cpp
vendored
Normal file
File diff suppressed because it is too large
Load diff
3153
3rdparty/imgui/src/imgui_draw.cpp
vendored
Normal file
3153
3rdparty/imgui/src/imgui_draw.cpp
vendored
Normal file
File diff suppressed because it is too large
Load diff
5725
3rdparty/imgui/src/imgui_widgets.cpp
vendored
Normal file
5725
3rdparty/imgui/src/imgui_widgets.cpp
vendored
Normal file
File diff suppressed because it is too large
Load diff
624
3rdparty/stb/include/stb_rect_pack.h
vendored
Normal file
624
3rdparty/stb/include/stb_rect_pack.h
vendored
Normal file
|
@ -0,0 +1,624 @@
|
||||||
|
// stb_rect_pack.h - v0.11 - public domain - rectangle packing
|
||||||
|
// Sean Barrett 2014
|
||||||
|
//
|
||||||
|
// Useful for e.g. packing rectangular textures into an atlas.
|
||||||
|
// Does not do rotation.
|
||||||
|
//
|
||||||
|
// Not necessarily the awesomest packing method, but better than
|
||||||
|
// the totally naive one in stb_truetype (which is primarily what
|
||||||
|
// this is meant to replace).
|
||||||
|
//
|
||||||
|
// Has only had a few tests run, may have issues.
|
||||||
|
//
|
||||||
|
// More docs to come.
|
||||||
|
//
|
||||||
|
// No memory allocations; uses qsort() and assert() from stdlib.
|
||||||
|
// Can override those by defining STBRP_SORT and STBRP_ASSERT.
|
||||||
|
//
|
||||||
|
// This library currently uses the Skyline Bottom-Left algorithm.
|
||||||
|
//
|
||||||
|
// Please note: better rectangle packers are welcome! Please
|
||||||
|
// implement them to the same API, but with a different init
|
||||||
|
// function.
|
||||||
|
//
|
||||||
|
// Credits
|
||||||
|
//
|
||||||
|
// Library
|
||||||
|
// Sean Barrett
|
||||||
|
// Minor features
|
||||||
|
// Martins Mozeiko
|
||||||
|
// github:IntellectualKitty
|
||||||
|
//
|
||||||
|
// Bugfixes / warning fixes
|
||||||
|
// Jeremy Jaussaud
|
||||||
|
//
|
||||||
|
// Version history:
|
||||||
|
//
|
||||||
|
// 0.11 (2017-03-03) return packing success/fail result
|
||||||
|
// 0.10 (2016-10-25) remove cast-away-const to avoid warnings
|
||||||
|
// 0.09 (2016-08-27) fix compiler warnings
|
||||||
|
// 0.08 (2015-09-13) really fix bug with empty rects (w=0 or h=0)
|
||||||
|
// 0.07 (2015-09-13) fix bug with empty rects (w=0 or h=0)
|
||||||
|
// 0.06 (2015-04-15) added STBRP_SORT to allow replacing qsort
|
||||||
|
// 0.05: added STBRP_ASSERT to allow replacing assert
|
||||||
|
// 0.04: fixed minor bug in STBRP_LARGE_RECTS support
|
||||||
|
// 0.01: initial release
|
||||||
|
//
|
||||||
|
// LICENSE
|
||||||
|
//
|
||||||
|
// See end of file for license information.
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// INCLUDE SECTION
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef STB_INCLUDE_STB_RECT_PACK_H
|
||||||
|
#define STB_INCLUDE_STB_RECT_PACK_H
|
||||||
|
|
||||||
|
#define STB_RECT_PACK_VERSION 1
|
||||||
|
|
||||||
|
#ifdef STBRP_STATIC
|
||||||
|
#define STBRP_DEF static
|
||||||
|
#else
|
||||||
|
#define STBRP_DEF extern
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct stbrp_context stbrp_context;
|
||||||
|
typedef struct stbrp_node stbrp_node;
|
||||||
|
typedef struct stbrp_rect stbrp_rect;
|
||||||
|
|
||||||
|
#ifdef STBRP_LARGE_RECTS
|
||||||
|
typedef int stbrp_coord;
|
||||||
|
#else
|
||||||
|
typedef unsigned short stbrp_coord;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
STBRP_DEF int stbrp_pack_rects (stbrp_context *context, stbrp_rect *rects, int num_rects);
|
||||||
|
// Assign packed locations to rectangles. The rectangles are of type
|
||||||
|
// 'stbrp_rect' defined below, stored in the array 'rects', and there
|
||||||
|
// are 'num_rects' many of them.
|
||||||
|
//
|
||||||
|
// Rectangles which are successfully packed have the 'was_packed' flag
|
||||||
|
// set to a non-zero value and 'x' and 'y' store the minimum location
|
||||||
|
// on each axis (i.e. bottom-left in cartesian coordinates, top-left
|
||||||
|
// if you imagine y increasing downwards). Rectangles which do not fit
|
||||||
|
// have the 'was_packed' flag set to 0.
|
||||||
|
//
|
||||||
|
// You should not try to access the 'rects' array from another thread
|
||||||
|
// while this function is running, as the function temporarily reorders
|
||||||
|
// the array while it executes.
|
||||||
|
//
|
||||||
|
// To pack into another rectangle, you need to call stbrp_init_target
|
||||||
|
// again. To continue packing into the same rectangle, you can call
|
||||||
|
// this function again. Calling this multiple times with multiple rect
|
||||||
|
// arrays will probably produce worse packing results than calling it
|
||||||
|
// a single time with the full rectangle array, but the option is
|
||||||
|
// available.
|
||||||
|
//
|
||||||
|
// The function returns 1 if all of the rectangles were successfully
|
||||||
|
// packed and 0 otherwise.
|
||||||
|
|
||||||
|
struct stbrp_rect
|
||||||
|
{
|
||||||
|
// reserved for your use:
|
||||||
|
int id;
|
||||||
|
|
||||||
|
// input:
|
||||||
|
stbrp_coord w, h;
|
||||||
|
|
||||||
|
// output:
|
||||||
|
stbrp_coord x, y;
|
||||||
|
int was_packed; // non-zero if valid packing
|
||||||
|
|
||||||
|
}; // 16 bytes, nominally
|
||||||
|
|
||||||
|
|
||||||
|
STBRP_DEF void stbrp_init_target (stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes);
|
||||||
|
// Initialize a rectangle packer to:
|
||||||
|
// pack a rectangle that is 'width' by 'height' in dimensions
|
||||||
|
// using temporary storage provided by the array 'nodes', which is 'num_nodes' long
|
||||||
|
//
|
||||||
|
// You must call this function every time you start packing into a new target.
|
||||||
|
//
|
||||||
|
// There is no "shutdown" function. The 'nodes' memory must stay valid for
|
||||||
|
// the following stbrp_pack_rects() call (or calls), but can be freed after
|
||||||
|
// the call (or calls) finish.
|
||||||
|
//
|
||||||
|
// Note: to guarantee best results, either:
|
||||||
|
// 1. make sure 'num_nodes' >= 'width'
|
||||||
|
// or 2. call stbrp_allow_out_of_mem() defined below with 'allow_out_of_mem = 1'
|
||||||
|
//
|
||||||
|
// If you don't do either of the above things, widths will be quantized to multiples
|
||||||
|
// of small integers to guarantee the algorithm doesn't run out of temporary storage.
|
||||||
|
//
|
||||||
|
// If you do #2, then the non-quantized algorithm will be used, but the algorithm
|
||||||
|
// may run out of temporary storage and be unable to pack some rectangles.
|
||||||
|
|
||||||
|
STBRP_DEF void stbrp_setup_allow_out_of_mem (stbrp_context *context, int allow_out_of_mem);
|
||||||
|
// Optionally call this function after init but before doing any packing to
|
||||||
|
// change the handling of the out-of-temp-memory scenario, described above.
|
||||||
|
// If you call init again, this will be reset to the default (false).
|
||||||
|
|
||||||
|
|
||||||
|
STBRP_DEF void stbrp_setup_heuristic (stbrp_context *context, int heuristic);
|
||||||
|
// Optionally select which packing heuristic the library should use. Different
|
||||||
|
// heuristics will produce better/worse results for different data sets.
|
||||||
|
// If you call init again, this will be reset to the default.
|
||||||
|
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
STBRP_HEURISTIC_Skyline_default=0,
|
||||||
|
STBRP_HEURISTIC_Skyline_BL_sortHeight = STBRP_HEURISTIC_Skyline_default,
|
||||||
|
STBRP_HEURISTIC_Skyline_BF_sortHeight
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// the details of the following structures don't matter to you, but they must
|
||||||
|
// be visible so you can handle the memory allocations for them
|
||||||
|
|
||||||
|
struct stbrp_node
|
||||||
|
{
|
||||||
|
stbrp_coord x,y;
|
||||||
|
stbrp_node *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct stbrp_context
|
||||||
|
{
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
int align;
|
||||||
|
int init_mode;
|
||||||
|
int heuristic;
|
||||||
|
int num_nodes;
|
||||||
|
stbrp_node *active_head;
|
||||||
|
stbrp_node *free_head;
|
||||||
|
stbrp_node extra[2]; // we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2'
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// IMPLEMENTATION SECTION
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifdef STB_RECT_PACK_IMPLEMENTATION
|
||||||
|
#ifndef STBRP_SORT
|
||||||
|
#include <stdlib.h>
|
||||||
|
#define STBRP_SORT qsort
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef STBRP_ASSERT
|
||||||
|
#include <assert.h>
|
||||||
|
#define STBRP_ASSERT assert
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#define STBRP__NOTUSED(v) (void)(v)
|
||||||
|
#else
|
||||||
|
#define STBRP__NOTUSED(v) (void)sizeof(v)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
STBRP__INIT_skyline = 1
|
||||||
|
};
|
||||||
|
|
||||||
|
STBRP_DEF void stbrp_setup_heuristic(stbrp_context *context, int heuristic)
|
||||||
|
{
|
||||||
|
switch (context->init_mode) {
|
||||||
|
case STBRP__INIT_skyline:
|
||||||
|
STBRP_ASSERT(heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight || heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight);
|
||||||
|
context->heuristic = heuristic;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
STBRP_ASSERT(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
STBRP_DEF void stbrp_setup_allow_out_of_mem(stbrp_context *context, int allow_out_of_mem)
|
||||||
|
{
|
||||||
|
if (allow_out_of_mem)
|
||||||
|
// if it's ok to run out of memory, then don't bother aligning them;
|
||||||
|
// this gives better packing, but may fail due to OOM (even though
|
||||||
|
// the rectangles easily fit). @TODO a smarter approach would be to only
|
||||||
|
// quantize once we've hit OOM, then we could get rid of this parameter.
|
||||||
|
context->align = 1;
|
||||||
|
else {
|
||||||
|
// if it's not ok to run out of memory, then quantize the widths
|
||||||
|
// so that num_nodes is always enough nodes.
|
||||||
|
//
|
||||||
|
// I.e. num_nodes * align >= width
|
||||||
|
// align >= width / num_nodes
|
||||||
|
// align = ceil(width/num_nodes)
|
||||||
|
|
||||||
|
context->align = (context->width + context->num_nodes-1) / context->num_nodes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
STBRP_DEF void stbrp_init_target(stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
#ifndef STBRP_LARGE_RECTS
|
||||||
|
STBRP_ASSERT(width <= 0xffff && height <= 0xffff);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
for (i=0; i < num_nodes-1; ++i)
|
||||||
|
nodes[i].next = &nodes[i+1];
|
||||||
|
nodes[i].next = NULL;
|
||||||
|
context->init_mode = STBRP__INIT_skyline;
|
||||||
|
context->heuristic = STBRP_HEURISTIC_Skyline_default;
|
||||||
|
context->free_head = &nodes[0];
|
||||||
|
context->active_head = &context->extra[0];
|
||||||
|
context->width = width;
|
||||||
|
context->height = height;
|
||||||
|
context->num_nodes = num_nodes;
|
||||||
|
stbrp_setup_allow_out_of_mem(context, 0);
|
||||||
|
|
||||||
|
// node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly)
|
||||||
|
context->extra[0].x = 0;
|
||||||
|
context->extra[0].y = 0;
|
||||||
|
context->extra[0].next = &context->extra[1];
|
||||||
|
context->extra[1].x = (stbrp_coord) width;
|
||||||
|
#ifdef STBRP_LARGE_RECTS
|
||||||
|
context->extra[1].y = (1<<30);
|
||||||
|
#else
|
||||||
|
context->extra[1].y = 65535;
|
||||||
|
#endif
|
||||||
|
context->extra[1].next = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// find minimum y position if it starts at x1
|
||||||
|
static int stbrp__skyline_find_min_y(stbrp_context *c, stbrp_node *first, int x0, int width, int *pwaste)
|
||||||
|
{
|
||||||
|
stbrp_node *node = first;
|
||||||
|
int x1 = x0 + width;
|
||||||
|
int min_y, visited_width, waste_area;
|
||||||
|
|
||||||
|
STBRP__NOTUSED(c);
|
||||||
|
|
||||||
|
STBRP_ASSERT(first->x <= x0);
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
// skip in case we're past the node
|
||||||
|
while (node->next->x <= x0)
|
||||||
|
++node;
|
||||||
|
#else
|
||||||
|
STBRP_ASSERT(node->next->x > x0); // we ended up handling this in the caller for efficiency
|
||||||
|
#endif
|
||||||
|
|
||||||
|
STBRP_ASSERT(node->x <= x0);
|
||||||
|
|
||||||
|
min_y = 0;
|
||||||
|
waste_area = 0;
|
||||||
|
visited_width = 0;
|
||||||
|
while (node->x < x1) {
|
||||||
|
if (node->y > min_y) {
|
||||||
|
// raise min_y higher.
|
||||||
|
// we've accounted for all waste up to min_y,
|
||||||
|
// but we'll now add more waste for everything we've visted
|
||||||
|
waste_area += visited_width * (node->y - min_y);
|
||||||
|
min_y = node->y;
|
||||||
|
// the first time through, visited_width might be reduced
|
||||||
|
if (node->x < x0)
|
||||||
|
visited_width += node->next->x - x0;
|
||||||
|
else
|
||||||
|
visited_width += node->next->x - node->x;
|
||||||
|
} else {
|
||||||
|
// add waste area
|
||||||
|
int under_width = node->next->x - node->x;
|
||||||
|
if (under_width + visited_width > width)
|
||||||
|
under_width = width - visited_width;
|
||||||
|
waste_area += under_width * (min_y - node->y);
|
||||||
|
visited_width += under_width;
|
||||||
|
}
|
||||||
|
node = node->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
*pwaste = waste_area;
|
||||||
|
return min_y;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int x,y;
|
||||||
|
stbrp_node **prev_link;
|
||||||
|
} stbrp__findresult;
|
||||||
|
|
||||||
|
static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int width, int height)
|
||||||
|
{
|
||||||
|
int best_waste = (1<<30), best_x, best_y = (1 << 30);
|
||||||
|
stbrp__findresult fr;
|
||||||
|
stbrp_node **prev, *node, *tail, **best = NULL;
|
||||||
|
|
||||||
|
// align to multiple of c->align
|
||||||
|
width = (width + c->align - 1);
|
||||||
|
width -= width % c->align;
|
||||||
|
STBRP_ASSERT(width % c->align == 0);
|
||||||
|
|
||||||
|
node = c->active_head;
|
||||||
|
prev = &c->active_head;
|
||||||
|
while (node->x + width <= c->width) {
|
||||||
|
int y,waste;
|
||||||
|
y = stbrp__skyline_find_min_y(c, node, node->x, width, &waste);
|
||||||
|
if (c->heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight) { // actually just want to test BL
|
||||||
|
// bottom left
|
||||||
|
if (y < best_y) {
|
||||||
|
best_y = y;
|
||||||
|
best = prev;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// best-fit
|
||||||
|
if (y + height <= c->height) {
|
||||||
|
// can only use it if it first vertically
|
||||||
|
if (y < best_y || (y == best_y && waste < best_waste)) {
|
||||||
|
best_y = y;
|
||||||
|
best_waste = waste;
|
||||||
|
best = prev;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
prev = &node->next;
|
||||||
|
node = node->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
best_x = (best == NULL) ? 0 : (*best)->x;
|
||||||
|
|
||||||
|
// if doing best-fit (BF), we also have to try aligning right edge to each node position
|
||||||
|
//
|
||||||
|
// e.g, if fitting
|
||||||
|
//
|
||||||
|
// ____________________
|
||||||
|
// |____________________|
|
||||||
|
//
|
||||||
|
// into
|
||||||
|
//
|
||||||
|
// | |
|
||||||
|
// | ____________|
|
||||||
|
// |____________|
|
||||||
|
//
|
||||||
|
// then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned
|
||||||
|
//
|
||||||
|
// This makes BF take about 2x the time
|
||||||
|
|
||||||
|
if (c->heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight) {
|
||||||
|
tail = c->active_head;
|
||||||
|
node = c->active_head;
|
||||||
|
prev = &c->active_head;
|
||||||
|
// find first node that's admissible
|
||||||
|
while (tail->x < width)
|
||||||
|
tail = tail->next;
|
||||||
|
while (tail) {
|
||||||
|
int xpos = tail->x - width;
|
||||||
|
int y,waste;
|
||||||
|
STBRP_ASSERT(xpos >= 0);
|
||||||
|
// find the left position that matches this
|
||||||
|
while (node->next->x <= xpos) {
|
||||||
|
prev = &node->next;
|
||||||
|
node = node->next;
|
||||||
|
}
|
||||||
|
STBRP_ASSERT(node->next->x > xpos && node->x <= xpos);
|
||||||
|
y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste);
|
||||||
|
if (y + height < c->height) {
|
||||||
|
if (y <= best_y) {
|
||||||
|
if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
|
||||||
|
best_x = xpos;
|
||||||
|
STBRP_ASSERT(y <= best_y);
|
||||||
|
best_y = y;
|
||||||
|
best_waste = waste;
|
||||||
|
best = prev;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tail = tail->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fr.prev_link = best;
|
||||||
|
fr.x = best_x;
|
||||||
|
fr.y = best_y;
|
||||||
|
return fr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context *context, int width, int height)
|
||||||
|
{
|
||||||
|
// find best position according to heuristic
|
||||||
|
stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height);
|
||||||
|
stbrp_node *node, *cur;
|
||||||
|
|
||||||
|
// bail if:
|
||||||
|
// 1. it failed
|
||||||
|
// 2. the best node doesn't fit (we don't always check this)
|
||||||
|
// 3. we're out of memory
|
||||||
|
if (res.prev_link == NULL || res.y + height > context->height || context->free_head == NULL) {
|
||||||
|
res.prev_link = NULL;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
// on success, create new node
|
||||||
|
node = context->free_head;
|
||||||
|
node->x = (stbrp_coord) res.x;
|
||||||
|
node->y = (stbrp_coord) (res.y + height);
|
||||||
|
|
||||||
|
context->free_head = node->next;
|
||||||
|
|
||||||
|
// insert the new node into the right starting point, and
|
||||||
|
// let 'cur' point to the remaining nodes needing to be
|
||||||
|
// stiched back in
|
||||||
|
|
||||||
|
cur = *res.prev_link;
|
||||||
|
if (cur->x < res.x) {
|
||||||
|
// preserve the existing one, so start testing with the next one
|
||||||
|
stbrp_node *next = cur->next;
|
||||||
|
cur->next = node;
|
||||||
|
cur = next;
|
||||||
|
} else {
|
||||||
|
*res.prev_link = node;
|
||||||
|
}
|
||||||
|
|
||||||
|
// from here, traverse cur and free the nodes, until we get to one
|
||||||
|
// that shouldn't be freed
|
||||||
|
while (cur->next && cur->next->x <= res.x + width) {
|
||||||
|
stbrp_node *next = cur->next;
|
||||||
|
// move the current node to the free list
|
||||||
|
cur->next = context->free_head;
|
||||||
|
context->free_head = cur;
|
||||||
|
cur = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
// stitch the list back in
|
||||||
|
node->next = cur;
|
||||||
|
|
||||||
|
if (cur->x < res.x + width)
|
||||||
|
cur->x = (stbrp_coord) (res.x + width);
|
||||||
|
|
||||||
|
#ifdef _DEBUG
|
||||||
|
cur = context->active_head;
|
||||||
|
while (cur->x < context->width) {
|
||||||
|
STBRP_ASSERT(cur->x < cur->next->x);
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
STBRP_ASSERT(cur->next == NULL);
|
||||||
|
|
||||||
|
{
|
||||||
|
stbrp_node *L1 = NULL, *L2 = NULL;
|
||||||
|
int count=0;
|
||||||
|
cur = context->active_head;
|
||||||
|
while (cur) {
|
||||||
|
L1 = cur;
|
||||||
|
cur = cur->next;
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
cur = context->free_head;
|
||||||
|
while (cur) {
|
||||||
|
L2 = cur;
|
||||||
|
cur = cur->next;
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
STBRP_ASSERT(count == context->num_nodes+2);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int rect_height_compare(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
const stbrp_rect *p = (const stbrp_rect *) a;
|
||||||
|
const stbrp_rect *q = (const stbrp_rect *) b;
|
||||||
|
if (p->h > q->h)
|
||||||
|
return -1;
|
||||||
|
if (p->h < q->h)
|
||||||
|
return 1;
|
||||||
|
return (p->w > q->w) ? -1 : (p->w < q->w);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int rect_original_order(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
const stbrp_rect *p = (const stbrp_rect *) a;
|
||||||
|
const stbrp_rect *q = (const stbrp_rect *) b;
|
||||||
|
return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef STBRP_LARGE_RECTS
|
||||||
|
#define STBRP__MAXVAL 0xffffffff
|
||||||
|
#else
|
||||||
|
#define STBRP__MAXVAL 0xffff
|
||||||
|
#endif
|
||||||
|
|
||||||
|
STBRP_DEF int stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects)
|
||||||
|
{
|
||||||
|
int i, all_rects_packed = 1;
|
||||||
|
|
||||||
|
// we use the 'was_packed' field internally to allow sorting/unsorting
|
||||||
|
for (i=0; i < num_rects; ++i) {
|
||||||
|
rects[i].was_packed = i;
|
||||||
|
#ifndef STBRP_LARGE_RECTS
|
||||||
|
STBRP_ASSERT(rects[i].w <= 0xffff && rects[i].h <= 0xffff);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// sort according to heuristic
|
||||||
|
STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_height_compare);
|
||||||
|
|
||||||
|
for (i=0; i < num_rects; ++i) {
|
||||||
|
if (rects[i].w == 0 || rects[i].h == 0) {
|
||||||
|
rects[i].x = rects[i].y = 0; // empty rect needs no space
|
||||||
|
} else {
|
||||||
|
stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h);
|
||||||
|
if (fr.prev_link) {
|
||||||
|
rects[i].x = (stbrp_coord) fr.x;
|
||||||
|
rects[i].y = (stbrp_coord) fr.y;
|
||||||
|
} else {
|
||||||
|
rects[i].x = rects[i].y = STBRP__MAXVAL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// unsort
|
||||||
|
STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_original_order);
|
||||||
|
|
||||||
|
// set was_packed flags and all_rects_packed status
|
||||||
|
for (i=0; i < num_rects; ++i) {
|
||||||
|
rects[i].was_packed = !(rects[i].x == STBRP__MAXVAL && rects[i].y == STBRP__MAXVAL);
|
||||||
|
if (!rects[i].was_packed)
|
||||||
|
all_rects_packed = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// return the all_rects_packed status
|
||||||
|
return all_rects_packed;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
This software is available under 2 licenses -- choose whichever you prefer.
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
ALTERNATIVE A - MIT License
|
||||||
|
Copyright (c) 2017 Sean Barrett
|
||||||
|
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:
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
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.
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
ALTERNATIVE B - Public Domain (www.unlicense.org)
|
||||||
|
This is free and unencumbered software released into the public domain.
|
||||||
|
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
|
||||||
|
software, either in source code form or as a compiled binary, for any purpose,
|
||||||
|
commercial or non-commercial, and by any means.
|
||||||
|
In jurisdictions that recognize copyright laws, the author or authors of this
|
||||||
|
software dedicate any and all copyright interest in the software to the public
|
||||||
|
domain. We make this dedication for the benefit of the public at large and to
|
||||||
|
the detriment of our heirs and successors. We intend this dedication to be an
|
||||||
|
overt act of relinquishment in perpetuity of all present and future rights to
|
||||||
|
this software under copyright law.
|
||||||
|
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 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.
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
*/
|
1404
3rdparty/stb/include/stb_textedit.h
vendored
Normal file
1404
3rdparty/stb/include/stb_textedit.h
vendored
Normal file
File diff suppressed because it is too large
Load diff
4853
3rdparty/stb/include/stb_truetype.h
vendored
Normal file
4853
3rdparty/stb/include/stb_truetype.h
vendored
Normal file
File diff suppressed because it is too large
Load diff
3
3rdparty/stb/src/stb.c
vendored
3
3rdparty/stb/src/stb.c
vendored
|
@ -1,2 +1,5 @@
|
||||||
#define STB_IMAGE_IMPLEMENTATION
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
#include "stb_image.h"
|
#include "stb_image.h"
|
||||||
|
|
||||||
|
#define STB_TRUETYPE_IMPLEMENTATION
|
||||||
|
#include "stb_truetype.h"
|
||||||
|
|
|
@ -26,7 +26,8 @@ add_executable(Graph
|
||||||
src/renderer.cpp
|
src/renderer.cpp
|
||||||
src/worldpass.cpp
|
src/worldpass.cpp
|
||||||
src/postpass.cpp
|
src/postpass.cpp
|
||||||
src/dofpass.cpp)
|
src/dofpass.cpp
|
||||||
|
src/imguipass.cpp)
|
||||||
target_link_libraries(Graph
|
target_link_libraries(Graph
|
||||||
PUBLIC
|
PUBLIC
|
||||||
SDL2::SDL2
|
SDL2::SDL2
|
||||||
|
@ -34,7 +35,8 @@ target_link_libraries(Graph
|
||||||
Vulkan::Vulkan
|
Vulkan::Vulkan
|
||||||
${ASSIMP_LIBRARIES}
|
${ASSIMP_LIBRARIES}
|
||||||
nlohmann::json
|
nlohmann::json
|
||||||
stb::stb)
|
stb::stb
|
||||||
|
imgui::imgui)
|
||||||
target_include_directories(Graph
|
target_include_directories(Graph
|
||||||
PUBLIC
|
PUBLIC
|
||||||
include
|
include
|
||||||
|
@ -48,7 +50,9 @@ if(UNIX)
|
||||||
shaders/post.vert
|
shaders/post.vert
|
||||||
shaders/post.frag
|
shaders/post.frag
|
||||||
shaders/gfield.vert
|
shaders/gfield.vert
|
||||||
shaders/gfield.frag)
|
shaders/gfield.frag
|
||||||
|
shaders/imgui.vert
|
||||||
|
shaders/imgui.frag)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
add_data(Graph
|
add_data(Graph
|
||||||
|
|
35
include/imguipass.h
Normal file
35
include/imguipass.h
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vulkan/vulkan.h>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
class Renderer;
|
||||||
|
struct RenderTarget;
|
||||||
|
|
||||||
|
class ImGuiPass {
|
||||||
|
public:
|
||||||
|
ImGuiPass(Renderer& renderer);
|
||||||
|
~ImGuiPass();
|
||||||
|
|
||||||
|
void render(VkCommandBuffer commandBuffer, RenderTarget* target);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void createDescriptorSetLayout();
|
||||||
|
void createPipeline();
|
||||||
|
void createFontImage();
|
||||||
|
void createBuffer(VkBuffer& buffer, VkDeviceMemory& memory, VkDeviceSize size, VkBufferUsageFlagBits bufferUsage);
|
||||||
|
|
||||||
|
VkDescriptorSetLayout setLayout_ = nullptr;
|
||||||
|
|
||||||
|
VkPipelineLayout pipelineLayout_ = nullptr;
|
||||||
|
VkPipeline pipeline_ = nullptr;
|
||||||
|
|
||||||
|
VkImage fontImage_ = nullptr;
|
||||||
|
VkDeviceMemory fontMemory_ = nullptr;
|
||||||
|
VkImageView fontImageView_ = nullptr;
|
||||||
|
VkSampler fontSampler_ = nullptr;
|
||||||
|
|
||||||
|
std::map<VkImageView, VkDescriptorSet> descriptorSets_ = {};
|
||||||
|
|
||||||
|
Renderer& renderer_;
|
||||||
|
};
|
|
@ -5,6 +5,7 @@
|
||||||
#include "worldpass.h"
|
#include "worldpass.h"
|
||||||
#include "postpass.h"
|
#include "postpass.h"
|
||||||
#include "dofpass.h"
|
#include "dofpass.h"
|
||||||
|
#include "imguipass.h"
|
||||||
|
|
||||||
struct RenderTarget {
|
struct RenderTarget {
|
||||||
VkSurfaceKHR surface = nullptr;
|
VkSurfaceKHR surface = nullptr;
|
||||||
|
@ -49,6 +50,15 @@ struct RenderTarget {
|
||||||
VkSemaphore imageAvailableSemaphore = nullptr;
|
VkSemaphore imageAvailableSemaphore = nullptr;
|
||||||
VkSemaphore renderFinishedSemaphore = nullptr;
|
VkSemaphore renderFinishedSemaphore = nullptr;
|
||||||
VkFence* fences = nullptr;
|
VkFence* fences = nullptr;
|
||||||
|
|
||||||
|
// imgui
|
||||||
|
VkBuffer* imguiVertexBuffers = nullptr;
|
||||||
|
VkDeviceMemory* imguiVertexMemorys = nullptr;
|
||||||
|
size_t* imguiVertexBufferSizes = nullptr;
|
||||||
|
|
||||||
|
VkBuffer* imguiIndexBuffers = nullptr;
|
||||||
|
VkDeviceMemory* imguiIndexMemorys = nullptr;
|
||||||
|
size_t* imguiIndexBufferSizes = nullptr;
|
||||||
|
|
||||||
VkDescriptorSet* postSets = nullptr;
|
VkDescriptorSet* postSets = nullptr;
|
||||||
VkDescriptorSet* dofSets = nullptr;
|
VkDescriptorSet* dofSets = nullptr;
|
||||||
|
@ -72,6 +82,8 @@ public:
|
||||||
|
|
||||||
VkShaderModule createShader(const char* path);
|
VkShaderModule createShader(const char* path);
|
||||||
|
|
||||||
|
void uploadImageData(VkImage image, int width, int height, VkDeviceSize size, void* src);
|
||||||
|
|
||||||
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
|
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
|
||||||
|
|
||||||
void fillMeshBuffers(Mesh* mesh);
|
void fillMeshBuffers(Mesh* mesh);
|
||||||
|
@ -142,4 +154,5 @@ private:
|
||||||
WorldPass* worldPass_ = nullptr;
|
WorldPass* worldPass_ = nullptr;
|
||||||
PostPass* postPass_ = nullptr;
|
PostPass* postPass_ = nullptr;
|
||||||
DoFPass* dofPass_ = nullptr;
|
DoFPass* dofPass_ = nullptr;
|
||||||
|
ImGuiPass* imguiPass_ = nullptr;
|
||||||
};
|
};
|
||||||
|
|
12
shaders/imgui.frag
Normal file
12
shaders/imgui.frag
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#version 460 core
|
||||||
|
|
||||||
|
layout(location = 0) in vec2 inUV;
|
||||||
|
layout(location = 1) in vec4 inColor;
|
||||||
|
|
||||||
|
layout(location = 0) out vec4 outColor;
|
||||||
|
|
||||||
|
layout(binding = 0) uniform sampler2D boundSampler;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
outColor = inColor * texture(boundSampler, inUV);
|
||||||
|
}
|
18
shaders/imgui.vert
Normal file
18
shaders/imgui.vert
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
#version 460 core
|
||||||
|
|
||||||
|
layout(location = 0) in vec2 inPos;
|
||||||
|
layout(location = 1) in vec2 inUV;
|
||||||
|
layout(location = 2) in vec4 inColor;
|
||||||
|
|
||||||
|
layout(push_constant) uniform PushConstants {
|
||||||
|
vec2 scale, translate;
|
||||||
|
} pushConstants;
|
||||||
|
|
||||||
|
layout(location = 0) out vec2 outUV;
|
||||||
|
layout(location = 1) out vec4 outColor;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
gl_Position = vec4(inPos * pushConstants.scale + pushConstants.translate, 0.0, 1.0);
|
||||||
|
outUV = inUV;
|
||||||
|
outColor = inColor;
|
||||||
|
}
|
127
src/dofpass.cpp
127
src/dofpass.cpp
|
@ -332,7 +332,6 @@ void DoFPass::createBokehImage() {
|
||||||
imageCreateInfo.arrayLayers = 1;
|
imageCreateInfo.arrayLayers = 1;
|
||||||
imageCreateInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
|
imageCreateInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
|
||||||
imageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
imageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
||||||
imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
||||||
imageCreateInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
|
imageCreateInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
|
||||||
imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||||
imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||||
|
@ -350,129 +349,15 @@ void DoFPass::createBokehImage() {
|
||||||
vkAllocateMemory(renderer_.getDevice(), &allocInfo, nullptr, &bokehMemory_);
|
vkAllocateMemory(renderer_.getDevice(), &allocInfo, nullptr, &bokehMemory_);
|
||||||
vkBindImageMemory(renderer_.getDevice(), bokehImage_, bokehMemory_, 0);
|
vkBindImageMemory(renderer_.getDevice(), bokehImage_, bokehMemory_, 0);
|
||||||
|
|
||||||
VkBuffer stagingBuffer;
|
renderer_.uploadImageData(
|
||||||
VkDeviceMemory stagingMemory;
|
|
||||||
|
|
||||||
VkBufferCreateInfo bufferInfo = {};
|
|
||||||
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
|
||||||
bufferInfo.size = width * height * 4;
|
|
||||||
bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
|
||||||
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
||||||
|
|
||||||
vkCreateBuffer(renderer_.getDevice(), &bufferInfo, nullptr, &stagingBuffer);
|
|
||||||
|
|
||||||
vkGetBufferMemoryRequirements(renderer_.getDevice(), stagingBuffer, &memRequirements);
|
|
||||||
|
|
||||||
allocInfo = {};
|
|
||||||
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
|
||||||
allocInfo.allocationSize = memRequirements.size;
|
|
||||||
allocInfo.memoryTypeIndex = renderer_.findMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
|
||||||
|
|
||||||
vkAllocateMemory(renderer_.getDevice(), &allocInfo, nullptr, &stagingMemory);
|
|
||||||
vkBindBufferMemory(renderer_.getDevice(), stagingBuffer, stagingMemory, 0);
|
|
||||||
|
|
||||||
void* data;
|
|
||||||
vkMapMemory(renderer_.getDevice(), stagingMemory, 0, width * height * 4, 0, &data);
|
|
||||||
memcpy(data, pixels, width * height * 4);
|
|
||||||
vkUnmapMemory(renderer_.getDevice(), stagingMemory);
|
|
||||||
|
|
||||||
stbi_image_free(pixels);
|
|
||||||
|
|
||||||
VkCommandBufferAllocateInfo bufferAllocateInfo = {};
|
|
||||||
bufferAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
|
||||||
bufferAllocateInfo.commandPool = renderer_.getCommandPool();
|
|
||||||
bufferAllocateInfo.commandBufferCount = 1;
|
|
||||||
|
|
||||||
VkCommandBuffer commandBuffer = nullptr;
|
|
||||||
vkAllocateCommandBuffers(renderer_.getDevice(), &bufferAllocateInfo, &commandBuffer);
|
|
||||||
|
|
||||||
VkCommandBufferBeginInfo beginInfo = {};
|
|
||||||
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
|
||||||
|
|
||||||
vkBeginCommandBuffer(commandBuffer, &beginInfo);
|
|
||||||
|
|
||||||
// change layout to transfer dst
|
|
||||||
{
|
|
||||||
VkImageMemoryBarrier imageMemoryBarrier = {};
|
|
||||||
imageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
|
||||||
imageMemoryBarrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
|
||||||
imageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
|
||||||
imageMemoryBarrier.image = bokehImage_;
|
|
||||||
imageMemoryBarrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
||||||
imageMemoryBarrier.subresourceRange.layerCount = 1;
|
|
||||||
imageMemoryBarrier.subresourceRange.levelCount = 1;
|
|
||||||
|
|
||||||
vkCmdPipelineBarrier(
|
|
||||||
commandBuffer,
|
|
||||||
VK_PIPELINE_STAGE_TRANSFER_BIT,
|
|
||||||
VK_PIPELINE_STAGE_TRANSFER_BIT,
|
|
||||||
0,
|
|
||||||
0, nullptr,
|
|
||||||
0, nullptr,
|
|
||||||
1, &imageMemoryBarrier);
|
|
||||||
}
|
|
||||||
|
|
||||||
VkBufferImageCopy region = {};
|
|
||||||
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
||||||
region.imageSubresource.layerCount = 1;
|
|
||||||
region.imageExtent.width = width;
|
|
||||||
region.imageExtent.height = height;
|
|
||||||
region.imageExtent.depth = 1;
|
|
||||||
|
|
||||||
vkCmdCopyBufferToImage(
|
|
||||||
commandBuffer,
|
|
||||||
stagingBuffer,
|
|
||||||
bokehImage_,
|
bokehImage_,
|
||||||
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
width,
|
||||||
1,
|
height,
|
||||||
®ion
|
width * height * 4,
|
||||||
|
pixels
|
||||||
);
|
);
|
||||||
|
|
||||||
// change layout to shader read only
|
stbi_image_free(pixels);
|
||||||
{
|
|
||||||
VkImageMemoryBarrier imageMemoryBarrier = {};
|
|
||||||
imageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
|
||||||
imageMemoryBarrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
|
||||||
imageMemoryBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
|
||||||
imageMemoryBarrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
|
||||||
imageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
|
||||||
imageMemoryBarrier.image = bokehImage_;
|
|
||||||
imageMemoryBarrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
||||||
imageMemoryBarrier.subresourceRange.layerCount = 1;
|
|
||||||
imageMemoryBarrier.subresourceRange.levelCount = 1;
|
|
||||||
|
|
||||||
vkCmdPipelineBarrier(
|
|
||||||
commandBuffer,
|
|
||||||
VK_PIPELINE_STAGE_TRANSFER_BIT,
|
|
||||||
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
|
|
||||||
0,
|
|
||||||
0, nullptr,
|
|
||||||
0, nullptr,
|
|
||||||
1, &imageMemoryBarrier);
|
|
||||||
}
|
|
||||||
|
|
||||||
vkEndCommandBuffer(commandBuffer);
|
|
||||||
|
|
||||||
VkSubmitInfo submitInfo = {};
|
|
||||||
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
|
||||||
submitInfo.commandBufferCount = 1;
|
|
||||||
submitInfo.pCommandBuffers = &commandBuffer;
|
|
||||||
|
|
||||||
VkFenceCreateInfo fenceCreateInfo = {};
|
|
||||||
fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
|
|
||||||
|
|
||||||
VkFence fence = nullptr;
|
|
||||||
vkCreateFence(renderer_.getDevice(), &fenceCreateInfo, nullptr, &fence);
|
|
||||||
|
|
||||||
vkQueueSubmit(renderer_.getGraphicsQueue(), 1, &submitInfo, fence);
|
|
||||||
|
|
||||||
vkWaitForFences(renderer_.getDevice(), 1, &fence, VK_TRUE, -1);
|
|
||||||
vkDestroyFence(renderer_.getDevice(), fence, nullptr);
|
|
||||||
|
|
||||||
vkFreeCommandBuffers(renderer_.getDevice(), renderer_.getCommandPool(), 1, &commandBuffer);
|
|
||||||
|
|
||||||
vkFreeMemory(renderer_.getDevice(), stagingMemory, nullptr);
|
|
||||||
vkDestroyBuffer(renderer_.getDevice(), stagingBuffer, nullptr);
|
|
||||||
|
|
||||||
VkImageViewCreateInfo createInfo = {};
|
VkImageViewCreateInfo createInfo = {};
|
||||||
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
||||||
|
|
379
src/imguipass.cpp
Normal file
379
src/imguipass.cpp
Normal file
|
@ -0,0 +1,379 @@
|
||||||
|
#include "imguipass.h"
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <imgui.h>
|
||||||
|
|
||||||
|
#include "renderer.h"
|
||||||
|
|
||||||
|
ImGuiPass::ImGuiPass(Renderer& renderer) : renderer_(renderer) {
|
||||||
|
createDescriptorSetLayout();
|
||||||
|
createPipeline();
|
||||||
|
createFontImage();
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGuiPass::~ImGuiPass() {
|
||||||
|
vkDestroySampler(renderer_.getDevice(), fontSampler_, nullptr);
|
||||||
|
vkDestroyImageView(renderer_.getDevice(), fontImageView_, nullptr);
|
||||||
|
vkFreeMemory(renderer_.getDevice(), fontMemory_, nullptr);
|
||||||
|
vkDestroyImage(renderer_.getDevice(), fontImage_, nullptr);
|
||||||
|
|
||||||
|
vkDestroyPipeline(renderer_.getDevice(), pipeline_, nullptr);
|
||||||
|
vkDestroyPipelineLayout(renderer_.getDevice(), pipelineLayout_, nullptr);
|
||||||
|
|
||||||
|
vkDestroyDescriptorSetLayout(renderer_.getDevice(), setLayout_, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGuiPass::render(VkCommandBuffer commandBuffer, RenderTarget* target) {
|
||||||
|
ImDrawData* drawData = ImGui::GetDrawData();
|
||||||
|
|
||||||
|
VkBuffer& vertexBuffer = target->imguiVertexBuffers[target->currentImage];
|
||||||
|
VkDeviceMemory& vertexMemory = target->imguiVertexMemorys[target->currentImage];
|
||||||
|
|
||||||
|
VkBuffer& indexBuffer = target->imguiIndexBuffers[target->currentImage];
|
||||||
|
VkDeviceMemory& indexMemory = target->imguiIndexMemorys[target->currentImage];
|
||||||
|
|
||||||
|
const size_t vertexSize = drawData->TotalVtxCount * sizeof(ImDrawVert);
|
||||||
|
if(vertexSize > target->imguiVertexBufferSizes[target->currentImage]) {
|
||||||
|
createBuffer(vertexBuffer, vertexMemory, vertexSize, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
|
||||||
|
target->imguiVertexBufferSizes[target->currentImage] = vertexSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
const size_t indexSize = drawData->TotalIdxCount * sizeof(ImDrawIdx);
|
||||||
|
if(indexSize > target->imguiIndexBufferSizes[target->currentImage]) {
|
||||||
|
createBuffer(indexBuffer, indexMemory, indexSize, VK_BUFFER_USAGE_INDEX_BUFFER_BIT);
|
||||||
|
target->imguiIndexBufferSizes[target->currentImage] = indexSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(vertexSize == 0 || indexSize == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
ImDrawVert* vertexData = nullptr;
|
||||||
|
ImDrawIdx* indexData = nullptr;
|
||||||
|
vkMapMemory(renderer_.getDevice(), vertexMemory, 0, vertexSize, 0, reinterpret_cast<void**>(&vertexData));
|
||||||
|
vkMapMemory(renderer_.getDevice(), indexMemory, 0, indexSize, 0, reinterpret_cast<void**>(&indexData));
|
||||||
|
|
||||||
|
for(int i = 0; i < drawData->CmdListsCount; i++) {
|
||||||
|
const ImDrawList* cmd_list = drawData->CmdLists[i];
|
||||||
|
|
||||||
|
memcpy(vertexData, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert));
|
||||||
|
vertexData += cmd_list->VtxBuffer.Size;
|
||||||
|
|
||||||
|
memcpy(indexData, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
|
||||||
|
indexData += cmd_list->IdxBuffer.Size;
|
||||||
|
}
|
||||||
|
|
||||||
|
vkUnmapMemory(renderer_.getDevice(), vertexMemory);
|
||||||
|
vkUnmapMemory(renderer_.getDevice(), indexMemory);
|
||||||
|
|
||||||
|
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_);
|
||||||
|
|
||||||
|
VkDeviceSize offset = 0;
|
||||||
|
vkCmdBindVertexBuffers(commandBuffer, 0, 1, &vertexBuffer, &offset);
|
||||||
|
vkCmdBindIndexBuffer(commandBuffer, indexBuffer, 0, VK_INDEX_TYPE_UINT16);
|
||||||
|
|
||||||
|
float scale[2];
|
||||||
|
scale[0] = 2.0f / drawData->DisplaySize.x;
|
||||||
|
scale[1] = 2.0f / drawData->DisplaySize.y;
|
||||||
|
|
||||||
|
float translate[2];
|
||||||
|
translate[0] = -1.0f - drawData->DisplayPos.x * scale[0];
|
||||||
|
translate[1] = -1.0f - drawData->DisplayPos.y * scale[1];
|
||||||
|
|
||||||
|
vkCmdPushConstants(commandBuffer, pipelineLayout_, VK_SHADER_STAGE_VERTEX_BIT, sizeof(float) * 0, sizeof(float) * 2, scale);
|
||||||
|
vkCmdPushConstants(commandBuffer, pipelineLayout_, VK_SHADER_STAGE_VERTEX_BIT, sizeof(float) * 2, sizeof(float) * 2, translate);
|
||||||
|
|
||||||
|
int vertexOffset = 0, indexOffset = 0;
|
||||||
|
const ImVec2 displayPos = drawData->DisplayPos;
|
||||||
|
for(int n = 0; n < drawData->CmdListsCount; n++) {
|
||||||
|
const ImDrawList* cmd_list = drawData->CmdLists[n];
|
||||||
|
for(int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) {
|
||||||
|
const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
|
||||||
|
|
||||||
|
if(descriptorSets_.count((VkImageView)pcmd->TextureId)) {
|
||||||
|
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout_, 0, 1, &descriptorSets_[(VkImageView)pcmd->TextureId], 0, nullptr);
|
||||||
|
} else {
|
||||||
|
VkDescriptorSetAllocateInfo allocInfo = {};
|
||||||
|
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
|
||||||
|
allocInfo.descriptorPool = renderer_.getDescriptorPool();
|
||||||
|
allocInfo.descriptorSetCount = 1;
|
||||||
|
allocInfo.pSetLayouts = &setLayout_;
|
||||||
|
|
||||||
|
VkDescriptorSet set = nullptr;
|
||||||
|
vkAllocateDescriptorSets(renderer_.getDevice(), &allocInfo, &set);
|
||||||
|
|
||||||
|
VkDescriptorImageInfo imageInfo = {};
|
||||||
|
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
||||||
|
imageInfo.imageView = (VkImageView)pcmd->TextureId;
|
||||||
|
imageInfo.sampler = fontSampler_;
|
||||||
|
|
||||||
|
VkWriteDescriptorSet descriptorWrite = {};
|
||||||
|
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||||||
|
descriptorWrite.descriptorCount = 1;
|
||||||
|
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
||||||
|
descriptorWrite.dstSet = set;
|
||||||
|
descriptorWrite.pImageInfo = &imageInfo;
|
||||||
|
|
||||||
|
vkUpdateDescriptorSets(renderer_.getDevice(), 1, &descriptorWrite, 0, nullptr);
|
||||||
|
|
||||||
|
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout_, 0, 1, &set, 0, nullptr);
|
||||||
|
|
||||||
|
descriptorSets_[(VkImageView)pcmd->TextureId] = set;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(pcmd->UserCallback) {
|
||||||
|
pcmd->UserCallback(cmd_list, pcmd);
|
||||||
|
} else {
|
||||||
|
VkRect2D scissor;
|
||||||
|
scissor.offset.x = (int32_t)(pcmd->ClipRect.x - displayPos.x) > 0 ? (int32_t)(pcmd->ClipRect.x - displayPos.x) : 0;
|
||||||
|
scissor.offset.y = (int32_t)(pcmd->ClipRect.y - displayPos.y) > 0 ? (int32_t)(pcmd->ClipRect.y - displayPos.y) : 0;
|
||||||
|
scissor.extent.width = (uint32_t)(pcmd->ClipRect.z - pcmd->ClipRect.x);
|
||||||
|
scissor.extent.height = (uint32_t)(pcmd->ClipRect.w - pcmd->ClipRect.y + 1);
|
||||||
|
|
||||||
|
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
|
||||||
|
|
||||||
|
vkCmdDrawIndexed(commandBuffer, pcmd->ElemCount, 1, indexOffset, vertexOffset, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
indexOffset += pcmd->ElemCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
vertexOffset += cmd_list->VtxBuffer.Size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGuiPass::createDescriptorSetLayout() {
|
||||||
|
VkDescriptorSetLayoutBinding samplerBinding = {};
|
||||||
|
samplerBinding.descriptorCount = 1;
|
||||||
|
samplerBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
||||||
|
samplerBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
|
||||||
|
|
||||||
|
VkDescriptorSetLayoutCreateInfo createInfo = {};
|
||||||
|
createInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
|
||||||
|
createInfo.bindingCount = 1;
|
||||||
|
createInfo.pBindings = &samplerBinding;
|
||||||
|
|
||||||
|
vkCreateDescriptorSetLayout(renderer_.getDevice(), &createInfo, nullptr, &setLayout_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGuiPass::createPipeline() {
|
||||||
|
VkShaderModule vertShaderModule = renderer_.createShader("shaders/imgui.vert.spv");
|
||||||
|
VkShaderModule fragShaderModule = renderer_.createShader("shaders/imgui.frag.spv");
|
||||||
|
|
||||||
|
VkPipelineShaderStageCreateInfo vertShaderStageInfo = {};
|
||||||
|
vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
||||||
|
vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
|
||||||
|
vertShaderStageInfo.module = vertShaderModule;
|
||||||
|
vertShaderStageInfo.pName = "main";
|
||||||
|
|
||||||
|
VkPipelineShaderStageCreateInfo fragShaderStageInfo = {};
|
||||||
|
fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
||||||
|
fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
|
||||||
|
fragShaderStageInfo.module = fragShaderModule;
|
||||||
|
fragShaderStageInfo.pName = "main";
|
||||||
|
|
||||||
|
const std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages = {vertShaderStageInfo, fragShaderStageInfo};
|
||||||
|
|
||||||
|
VkVertexInputBindingDescription vertexBindingDescription = {};
|
||||||
|
vertexBindingDescription.stride = sizeof(ImDrawVert);
|
||||||
|
vertexBindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
|
||||||
|
|
||||||
|
VkVertexInputAttributeDescription positionAttribute = {};
|
||||||
|
positionAttribute.format = VK_FORMAT_R32G32_SFLOAT;
|
||||||
|
positionAttribute.offset = offsetof(ImDrawVert, pos);
|
||||||
|
|
||||||
|
VkVertexInputAttributeDescription uvAttribute = {};
|
||||||
|
uvAttribute.location = 1;
|
||||||
|
uvAttribute.format = VK_FORMAT_R32G32_SFLOAT;
|
||||||
|
uvAttribute.offset = offsetof(ImDrawVert, uv);
|
||||||
|
|
||||||
|
VkVertexInputAttributeDescription colorAttribute = {};
|
||||||
|
colorAttribute.location = 2;
|
||||||
|
colorAttribute.format = VK_FORMAT_R8G8B8A8_UNORM;
|
||||||
|
colorAttribute.offset = offsetof(ImDrawVert, col);
|
||||||
|
|
||||||
|
const std::array<VkVertexInputAttributeDescription, 3> attributes = {
|
||||||
|
positionAttribute,
|
||||||
|
uvAttribute,
|
||||||
|
colorAttribute
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPipelineVertexInputStateCreateInfo vertexInputInfo = {};
|
||||||
|
vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
|
||||||
|
vertexInputInfo.vertexBindingDescriptionCount = 1;
|
||||||
|
vertexInputInfo.pVertexBindingDescriptions = &vertexBindingDescription;
|
||||||
|
vertexInputInfo.vertexAttributeDescriptionCount = attributes.size();
|
||||||
|
vertexInputInfo.pVertexAttributeDescriptions = attributes.data();
|
||||||
|
|
||||||
|
VkPipelineInputAssemblyStateCreateInfo inputAssembly = {};
|
||||||
|
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
|
||||||
|
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
|
||||||
|
|
||||||
|
VkPipelineViewportStateCreateInfo viewportState = {};
|
||||||
|
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
|
||||||
|
viewportState.viewportCount = 1;
|
||||||
|
viewportState.scissorCount = 1;
|
||||||
|
|
||||||
|
VkPipelineRasterizationStateCreateInfo rasterizer = {};
|
||||||
|
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
|
||||||
|
rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
|
||||||
|
rasterizer.cullMode = VK_CULL_MODE_NONE;
|
||||||
|
rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
|
||||||
|
rasterizer.lineWidth = 1.0f;
|
||||||
|
|
||||||
|
VkPipelineMultisampleStateCreateInfo multisampling = {};
|
||||||
|
multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
|
||||||
|
multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
|
||||||
|
|
||||||
|
VkPipelineColorBlendAttachmentState colorBlendAttachment = {};
|
||||||
|
colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
|
||||||
|
colorBlendAttachment.blendEnable = VK_TRUE;
|
||||||
|
colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
|
||||||
|
colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
|
||||||
|
colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
|
||||||
|
colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
|
||||||
|
colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
|
||||||
|
colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
|
||||||
|
|
||||||
|
VkPipelineColorBlendStateCreateInfo colorBlending = {};
|
||||||
|
colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
|
||||||
|
colorBlending.attachmentCount = 1;
|
||||||
|
colorBlending.pAttachments = &colorBlendAttachment;
|
||||||
|
|
||||||
|
const std::array<VkDynamicState, 2> dynamicStates = {
|
||||||
|
VK_DYNAMIC_STATE_VIEWPORT,
|
||||||
|
VK_DYNAMIC_STATE_SCISSOR
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPipelineDynamicStateCreateInfo dynamicState = {};
|
||||||
|
dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
|
||||||
|
dynamicState.dynamicStateCount = dynamicStates.size();
|
||||||
|
dynamicState.pDynamicStates = dynamicStates.data();
|
||||||
|
|
||||||
|
VkPushConstantRange pushConstant = {};
|
||||||
|
pushConstant.size = sizeof(glm::vec4);
|
||||||
|
pushConstant.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
|
||||||
|
|
||||||
|
VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
|
||||||
|
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
||||||
|
pipelineLayoutInfo.setLayoutCount = 1;
|
||||||
|
pipelineLayoutInfo.pSetLayouts = &setLayout_;
|
||||||
|
pipelineLayoutInfo.pushConstantRangeCount = 1;
|
||||||
|
pipelineLayoutInfo.pPushConstantRanges = &pushConstant;
|
||||||
|
|
||||||
|
vkCreatePipelineLayout(renderer_.getDevice(), &pipelineLayoutInfo, nullptr, &pipelineLayout_);
|
||||||
|
|
||||||
|
VkGraphicsPipelineCreateInfo pipelineInfo = {};
|
||||||
|
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
|
||||||
|
pipelineInfo.stageCount = shaderStages.size();
|
||||||
|
pipelineInfo.pStages = shaderStages.data();
|
||||||
|
pipelineInfo.pVertexInputState = &vertexInputInfo;
|
||||||
|
pipelineInfo.pInputAssemblyState = &inputAssembly;
|
||||||
|
pipelineInfo.pViewportState = &viewportState;
|
||||||
|
pipelineInfo.pRasterizationState = &rasterizer;
|
||||||
|
pipelineInfo.pMultisampleState = &multisampling;
|
||||||
|
pipelineInfo.pColorBlendState = &colorBlending;
|
||||||
|
pipelineInfo.pDynamicState = &dynamicState;
|
||||||
|
pipelineInfo.layout = pipelineLayout_;
|
||||||
|
pipelineInfo.renderPass = renderer_.getRenderPass();
|
||||||
|
|
||||||
|
vkCreateGraphicsPipelines(renderer_.getDevice(), nullptr, 1, &pipelineInfo, nullptr, &pipeline_);
|
||||||
|
|
||||||
|
vkDestroyShaderModule(renderer_.getDevice(), fragShaderModule, nullptr);
|
||||||
|
vkDestroyShaderModule(renderer_.getDevice(), vertShaderModule, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGuiPass::createFontImage() {
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
unsigned char* pixels = nullptr;
|
||||||
|
int width = 0, height = 0;
|
||||||
|
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
|
||||||
|
|
||||||
|
VkImageCreateInfo imageCreateInfo = {};
|
||||||
|
imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
|
||||||
|
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
|
||||||
|
imageCreateInfo.extent.width = width;
|
||||||
|
imageCreateInfo.extent.height = height;
|
||||||
|
imageCreateInfo.extent.depth = 1;
|
||||||
|
imageCreateInfo.mipLevels = 1;
|
||||||
|
imageCreateInfo.arrayLayers = 1;
|
||||||
|
imageCreateInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
|
||||||
|
imageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
||||||
|
imageCreateInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
|
||||||
|
imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||||
|
imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||||
|
|
||||||
|
vkCreateImage(renderer_.getDevice(), &imageCreateInfo, nullptr, &fontImage_);
|
||||||
|
|
||||||
|
VkMemoryRequirements memRequirements;
|
||||||
|
vkGetImageMemoryRequirements(renderer_.getDevice(), fontImage_, &memRequirements);
|
||||||
|
|
||||||
|
VkMemoryAllocateInfo allocInfo = {};
|
||||||
|
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
||||||
|
allocInfo.allocationSize = memRequirements.size;
|
||||||
|
allocInfo.memoryTypeIndex = renderer_.findMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
||||||
|
|
||||||
|
vkAllocateMemory(renderer_.getDevice(), &allocInfo, nullptr, &fontMemory_);
|
||||||
|
vkBindImageMemory(renderer_.getDevice(), fontImage_, fontMemory_, 0);
|
||||||
|
|
||||||
|
renderer_.uploadImageData(
|
||||||
|
fontImage_,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
width * height * 4,
|
||||||
|
pixels
|
||||||
|
);
|
||||||
|
|
||||||
|
VkImageViewCreateInfo createInfo = {};
|
||||||
|
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
||||||
|
createInfo.image = fontImage_;
|
||||||
|
createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
||||||
|
createInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
|
||||||
|
createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||||
|
createInfo.subresourceRange.levelCount = 1;
|
||||||
|
createInfo.subresourceRange.layerCount = 1;
|
||||||
|
|
||||||
|
vkCreateImageView(renderer_.getDevice(), &createInfo, nullptr, &fontImageView_);
|
||||||
|
|
||||||
|
io.Fonts->TexID = static_cast<ImTextureID>(fontImageView_);
|
||||||
|
|
||||||
|
VkSamplerCreateInfo samplerInfo = {};
|
||||||
|
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
|
||||||
|
samplerInfo.magFilter = VK_FILTER_LINEAR;
|
||||||
|
samplerInfo.minFilter = VK_FILTER_LINEAR;
|
||||||
|
samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
|
||||||
|
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
|
||||||
|
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
|
||||||
|
samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
|
||||||
|
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
||||||
|
|
||||||
|
vkCreateSampler(renderer_.getDevice(), &samplerInfo, nullptr, &fontSampler_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGuiPass::createBuffer(VkBuffer& buffer, VkDeviceMemory& memory, VkDeviceSize size, VkBufferUsageFlagBits bufferUsage) {
|
||||||
|
if(buffer != nullptr)
|
||||||
|
vkDestroyBuffer(renderer_.getDevice(), buffer, nullptr);
|
||||||
|
|
||||||
|
if(memory != nullptr)
|
||||||
|
vkFreeMemory(renderer_.getDevice(), memory, nullptr);
|
||||||
|
|
||||||
|
VkBufferCreateInfo bufferInfo = {};
|
||||||
|
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||||||
|
bufferInfo.size = size;
|
||||||
|
bufferInfo.usage = bufferUsage;
|
||||||
|
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||||
|
|
||||||
|
vkCreateBuffer(renderer_.getDevice(), &bufferInfo, nullptr, &buffer);
|
||||||
|
|
||||||
|
VkMemoryRequirements memRequirements = {};
|
||||||
|
vkGetBufferMemoryRequirements(renderer_.getDevice(), buffer, &memRequirements);
|
||||||
|
|
||||||
|
VkMemoryAllocateInfo allocInfo = {};
|
||||||
|
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
||||||
|
allocInfo.allocationSize = memRequirements.size;
|
||||||
|
allocInfo.memoryTypeIndex = renderer_.findMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
||||||
|
|
||||||
|
vkAllocateMemory(renderer_.getDevice(), &allocInfo, nullptr, &memory);
|
||||||
|
vkBindBufferMemory(renderer_.getDevice(), buffer, memory, 0);
|
||||||
|
}
|
54
src/main.cpp
54
src/main.cpp
|
@ -7,6 +7,7 @@
|
||||||
#include <assimp/Importer.hpp>
|
#include <assimp/Importer.hpp>
|
||||||
#include <assimp/scene.h>
|
#include <assimp/scene.h>
|
||||||
#include <assimp/postprocess.h>
|
#include <assimp/postprocess.h>
|
||||||
|
#include <imgui.h>
|
||||||
|
|
||||||
#include "renderer.h"
|
#include "renderer.h"
|
||||||
#include "platform.h"
|
#include "platform.h"
|
||||||
|
@ -218,13 +219,20 @@ int main(int argc, char* argv[]) {
|
||||||
|
|
||||||
SDL_SetWindowFullscreen(window, windowFullscreen == 1 ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
|
SDL_SetWindowFullscreen(window, windowFullscreen == 1 ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
|
||||||
|
|
||||||
|
ImGui::CreateContext();
|
||||||
|
|
||||||
|
auto& io = ImGui::GetIO();
|
||||||
|
io.DisplaySize = ImVec2(windowWidth, windowHeight);
|
||||||
|
|
||||||
|
ImGui::StyleColorsDark();
|
||||||
|
|
||||||
renderer = new Renderer();
|
renderer = new Renderer();
|
||||||
|
|
||||||
VkSurfaceKHR surface = nullptr;
|
VkSurfaceKHR surface = nullptr;
|
||||||
SDL_Vulkan_CreateSurface(window, renderer->getInstance(), &surface);
|
SDL_Vulkan_CreateSurface(window, renderer->getInstance(), &surface);
|
||||||
|
|
||||||
RenderTarget* target = renderer->createSurfaceRenderTarget(surface);
|
RenderTarget* target = renderer->createSurfaceRenderTarget(surface);
|
||||||
|
|
||||||
World world;
|
World world;
|
||||||
|
|
||||||
auto light = new Light();
|
auto light = new Light();
|
||||||
|
@ -240,19 +248,22 @@ int main(int argc, char* argv[]) {
|
||||||
else
|
else
|
||||||
world.meshes.push_back(loadMesh("data/scene.obj"));
|
world.meshes.push_back(loadMesh("data/scene.obj"));
|
||||||
|
|
||||||
float currentTime = 0.0f, lastTime = 0.0f;
|
float currentTime = 0.0f, lastTime = 0.0f, animationTime = 0.0f;
|
||||||
Shot* currentShot = nullptr;
|
Shot* currentShot = nullptr;
|
||||||
|
|
||||||
bool running = true;
|
bool running = true;
|
||||||
while(running) {
|
while(running) {
|
||||||
SDL_Event event = {};
|
SDL_Event event = {};
|
||||||
while(SDL_PollEvent(&event) > 0) {
|
while(SDL_PollEvent(&event) > 0) {
|
||||||
if(event.type == SDL_QUIT)
|
if(event.type == SDL_QUIT)
|
||||||
running = false;
|
running = false;
|
||||||
|
|
||||||
if(event.type == SDL_WINDOWEVENT) {
|
if(event.type == SDL_WINDOWEVENT) {
|
||||||
if(event.window.event == SDL_WINDOWEVENT_RESIZED)
|
if(event.window.event == SDL_WINDOWEVENT_RESIZED) {
|
||||||
|
io.DisplaySize = ImVec2(event.window.data1, event.window.data2);
|
||||||
|
|
||||||
target = renderer->createSurfaceRenderTarget(surface, target);
|
target = renderer->createSurfaceRenderTarget(surface, target);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(event.type == SDL_KEYDOWN && event.key.keysym.scancode == SDL_SCANCODE_F11) {
|
if(event.type == SDL_KEYDOWN && event.key.keysym.scancode == SDL_SCANCODE_F11) {
|
||||||
|
@ -278,7 +289,7 @@ int main(int argc, char* argv[]) {
|
||||||
if(shot->end > endTime)
|
if(shot->end > endTime)
|
||||||
endTime = shot->end;
|
endTime = shot->end;
|
||||||
|
|
||||||
if(currentTime >= shot->start && currentTime < shot->end && currentShot != shot) {
|
if(animationTime >= shot->start && animationTime < shot->end && currentShot != shot) {
|
||||||
if(currentShot != nullptr) {
|
if(currentShot != nullptr) {
|
||||||
world.meshes.clear();
|
world.meshes.clear();
|
||||||
}
|
}
|
||||||
|
@ -291,7 +302,7 @@ int main(int argc, char* argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// we have reached the end of the cinematic
|
// we have reached the end of the cinematic
|
||||||
if(currentTime >= endTime) {
|
if(animationTime >= endTime) {
|
||||||
currentShot = nullptr;
|
currentShot = nullptr;
|
||||||
|
|
||||||
world.meshes.clear();
|
world.meshes.clear();
|
||||||
|
@ -301,7 +312,7 @@ int main(int argc, char* argv[]) {
|
||||||
for(auto animation : currentShot->animations) {
|
for(auto animation : currentShot->animations) {
|
||||||
unsigned int frameIndex = 0;
|
unsigned int frameIndex = 0;
|
||||||
for(size_t i = 0; i < animation->keyframes.size(); i++) {
|
for(size_t i = 0; i < animation->keyframes.size(); i++) {
|
||||||
if(currentTime < animation->keyframes[i + 1].time) {
|
if(animationTime < animation->keyframes[i + 1].time) {
|
||||||
frameIndex = i;
|
frameIndex = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -310,7 +321,7 @@ int main(int argc, char* argv[]) {
|
||||||
const auto currentFrame = animation->keyframes[frameIndex];
|
const auto currentFrame = animation->keyframes[frameIndex];
|
||||||
const auto nextFrame = animation->keyframes[(frameIndex + 1) % animation->keyframes.size()];
|
const auto nextFrame = animation->keyframes[(frameIndex + 1) % animation->keyframes.size()];
|
||||||
|
|
||||||
const float delta = (currentTime - currentFrame.time) / (nextFrame.time - currentFrame.time);
|
const float delta = (animationTime - currentFrame.time) / (nextFrame.time - currentFrame.time);
|
||||||
|
|
||||||
glm::vec3 pos = currentFrame.value + delta * (nextFrame.value - currentFrame.value);
|
glm::vec3 pos = currentFrame.value + delta * (nextFrame.value - currentFrame.value);
|
||||||
|
|
||||||
|
@ -321,12 +332,31 @@ int main(int argc, char* argv[]) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
currentTime = SDL_GetTicks() / 1000.0f;
|
||||||
|
const float deltaTime = currentTime - lastTime;
|
||||||
|
lastTime = currentTime;
|
||||||
|
|
||||||
|
ImGui::NewFrame();
|
||||||
|
|
||||||
|
io.DeltaTime = deltaTime;
|
||||||
|
|
||||||
|
int mouseX = 0, mouseY = 0;
|
||||||
|
const Uint32 mouseButtons = SDL_GetMouseState(&mouseX, &mouseY);
|
||||||
|
io.MouseDown[0] = (mouseButtons & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0;
|
||||||
|
io.MouseDown[1] = (mouseButtons & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0;
|
||||||
|
io.MouseDown[2] = (mouseButtons & SDL_BUTTON(SDL_BUTTON_MIDDLE)) != 0;
|
||||||
|
|
||||||
|
if(SDL_GetWindowFlags(window) & SDL_WINDOW_INPUT_FOCUS)
|
||||||
|
io.MousePos = ImVec2(static_cast<float>(mouseX), static_cast<float>(mouseY));
|
||||||
|
|
||||||
|
ImGui::ShowDemoWindow();
|
||||||
|
|
||||||
|
ImGui::Render();
|
||||||
renderer->render(world, camera, target);
|
renderer->render(world, camera, target);
|
||||||
|
|
||||||
if(cinematicMode) {
|
if(cinematicMode) {
|
||||||
currentTime += ((SDL_GetTicks() / 1000.0f) - lastTime);
|
animationTime += deltaTime;
|
||||||
lastTime = currentTime;
|
|
||||||
|
|
||||||
static int frameNum = 0;
|
static int frameNum = 0;
|
||||||
std::string screenshotName = "frame" + std::to_string(frameNum) + ".ppm";
|
std::string screenshotName = "frame" + std::to_string(frameNum) + ".ppm";
|
||||||
|
|
160
src/renderer.cpp
160
src/renderer.cpp
|
@ -26,11 +26,13 @@ Renderer::Renderer() {
|
||||||
worldPass_ = new WorldPass(*this);
|
worldPass_ = new WorldPass(*this);
|
||||||
postPass_ = new PostPass(*this);
|
postPass_ = new PostPass(*this);
|
||||||
dofPass_ = new DoFPass(*this);
|
dofPass_ = new DoFPass(*this);
|
||||||
|
imguiPass_ = new ImGuiPass(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
Renderer::~Renderer() {
|
Renderer::~Renderer() {
|
||||||
vkDeviceWaitIdle(device_);
|
vkDeviceWaitIdle(device_);
|
||||||
|
|
||||||
|
delete imguiPass_;
|
||||||
delete dofPass_;
|
delete dofPass_;
|
||||||
delete postPass_;
|
delete postPass_;
|
||||||
delete worldPass_;
|
delete worldPass_;
|
||||||
|
@ -95,6 +97,7 @@ void Renderer::render(World& world, Camera& camera, RenderTarget* target) {
|
||||||
vkCmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
vkCmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
||||||
|
|
||||||
postPass_->render(commandBuffer, target);
|
postPass_->render(commandBuffer, target);
|
||||||
|
imguiPass_->render(commandBuffer, target);
|
||||||
|
|
||||||
vkCmdEndRenderPass(commandBuffer);
|
vkCmdEndRenderPass(commandBuffer);
|
||||||
|
|
||||||
|
@ -203,6 +206,12 @@ RenderTarget* Renderer::createSurfaceRenderTarget(VkSurfaceKHR surface, RenderTa
|
||||||
target->farFieldMemory = new VkDeviceMemory[swapchainImageCount];
|
target->farFieldMemory = new VkDeviceMemory[swapchainImageCount];
|
||||||
target->farFieldImageViews = new VkImageView[swapchainImageCount];
|
target->farFieldImageViews = new VkImageView[swapchainImageCount];
|
||||||
target->farFieldFramebuffers = new VkFramebuffer[swapchainImageCount];
|
target->farFieldFramebuffers = new VkFramebuffer[swapchainImageCount];
|
||||||
|
target->imguiVertexBuffers = new VkBuffer[swapchainImageCount];
|
||||||
|
target->imguiVertexMemorys = new VkDeviceMemory[swapchainImageCount];
|
||||||
|
target->imguiVertexBufferSizes = new size_t[swapchainImageCount];
|
||||||
|
target->imguiIndexBuffers = new VkBuffer[swapchainImageCount];
|
||||||
|
target->imguiIndexMemorys = new VkDeviceMemory[swapchainImageCount];
|
||||||
|
target->imguiIndexBufferSizes = new size_t[swapchainImageCount];
|
||||||
for(uint32_t i = 0; i < swapchainImageCount; i++) {
|
for(uint32_t i = 0; i < swapchainImageCount; i++) {
|
||||||
// swapchain image view
|
// swapchain image view
|
||||||
{
|
{
|
||||||
|
@ -450,6 +459,17 @@ RenderTarget* Renderer::createSurfaceRenderTarget(VkSurfaceKHR surface, RenderTa
|
||||||
|
|
||||||
vkCreateFramebuffer(device_, &framebufferInfo, nullptr, &target->farFieldFramebuffers[i]);
|
vkCreateFramebuffer(device_, &framebufferInfo, nullptr, &target->farFieldFramebuffers[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// imgui
|
||||||
|
{
|
||||||
|
target->imguiVertexBuffers[i] = nullptr;
|
||||||
|
target->imguiVertexMemorys[i] = nullptr;
|
||||||
|
target->imguiVertexBufferSizes[i] = 0;
|
||||||
|
|
||||||
|
target->imguiIndexBuffers[i] = nullptr;
|
||||||
|
target->imguiIndexMemorys[i] = nullptr;
|
||||||
|
target->imguiIndexBufferSizes[i] = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
postPass_->createDescriptorSet(target);
|
postPass_->createDescriptorSet(target);
|
||||||
|
@ -501,6 +521,12 @@ void Renderer::destroyRenderTarget(RenderTarget* target) {
|
||||||
vkFreeDescriptorSets(device_, descriptorPool_, target->numImages, target->postSets);
|
vkFreeDescriptorSets(device_, descriptorPool_, target->numImages, target->postSets);
|
||||||
|
|
||||||
for(uint32_t i = 0; i < target->numImages; i++) {
|
for(uint32_t i = 0; i < target->numImages; i++) {
|
||||||
|
vkFreeMemory(device_, target->imguiIndexMemorys[i], nullptr);
|
||||||
|
vkDestroyBuffer(device_, target->imguiIndexBuffers[i], nullptr);
|
||||||
|
|
||||||
|
vkFreeMemory(device_, target->imguiVertexMemorys[i], nullptr);
|
||||||
|
vkDestroyBuffer(device_, target->imguiVertexBuffers[i], nullptr);
|
||||||
|
|
||||||
vkDestroyFramebuffer(device_, target->nearFieldFramebuffers[i], nullptr);
|
vkDestroyFramebuffer(device_, target->nearFieldFramebuffers[i], nullptr);
|
||||||
|
|
||||||
vkDestroyImageView(device_, target->nearFieldImageViews[i], nullptr);
|
vkDestroyImageView(device_, target->nearFieldImageViews[i], nullptr);
|
||||||
|
@ -527,6 +553,14 @@ void Renderer::destroyRenderTarget(RenderTarget* target) {
|
||||||
vkDestroyImageView(device_, target->swapchainImageViews[i], nullptr);
|
vkDestroyImageView(device_, target->swapchainImageViews[i], nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
delete[] target->imguiIndexBufferSizes;
|
||||||
|
delete[] target->imguiIndexMemorys;
|
||||||
|
delete[] target->imguiIndexBuffers;
|
||||||
|
|
||||||
|
delete[] target->imguiVertexBufferSizes;
|
||||||
|
delete[] target->imguiVertexMemorys;
|
||||||
|
delete[] target->imguiVertexBuffers;
|
||||||
|
|
||||||
delete[] target->nearFieldFramebuffers;
|
delete[] target->nearFieldFramebuffers;
|
||||||
delete[] target->nearFieldImageViews;
|
delete[] target->nearFieldImageViews;
|
||||||
delete[] target->nearFieldMemory;
|
delete[] target->nearFieldMemory;
|
||||||
|
@ -783,6 +817,132 @@ VkShaderModule Renderer::createShader(const char* path) {
|
||||||
return shaderModule;
|
return shaderModule;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Renderer::uploadImageData(VkImage image, int width, int height, VkDeviceSize size, void* src) {
|
||||||
|
VkBuffer stagingBuffer;
|
||||||
|
VkDeviceMemory stagingMemory;
|
||||||
|
|
||||||
|
VkBufferCreateInfo bufferInfo = {};
|
||||||
|
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||||||
|
bufferInfo.size = size;
|
||||||
|
bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
||||||
|
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||||
|
|
||||||
|
vkCreateBuffer(device_, &bufferInfo, nullptr, &stagingBuffer);
|
||||||
|
|
||||||
|
VkMemoryRequirements memRequirements = {};
|
||||||
|
vkGetBufferMemoryRequirements(device_, stagingBuffer, &memRequirements);
|
||||||
|
|
||||||
|
VkMemoryAllocateInfo allocInfo = {};
|
||||||
|
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
||||||
|
allocInfo.allocationSize = memRequirements.size;
|
||||||
|
allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
||||||
|
|
||||||
|
vkAllocateMemory(device_, &allocInfo, nullptr, &stagingMemory);
|
||||||
|
vkBindBufferMemory(device_, stagingBuffer, stagingMemory, 0);
|
||||||
|
|
||||||
|
void* data;
|
||||||
|
vkMapMemory(device_, stagingMemory, 0, size, 0, &data);
|
||||||
|
memcpy(data, src, size);
|
||||||
|
vkUnmapMemory(device_, stagingMemory);
|
||||||
|
|
||||||
|
VkCommandBufferAllocateInfo bufferAllocateInfo = {};
|
||||||
|
bufferAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
||||||
|
bufferAllocateInfo.commandPool = commandPool_;
|
||||||
|
bufferAllocateInfo.commandBufferCount = 1;
|
||||||
|
|
||||||
|
VkCommandBuffer commandBuffer = nullptr;
|
||||||
|
vkAllocateCommandBuffers(device_, &bufferAllocateInfo, &commandBuffer);
|
||||||
|
|
||||||
|
VkCommandBufferBeginInfo beginInfo = {};
|
||||||
|
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
||||||
|
|
||||||
|
vkBeginCommandBuffer(commandBuffer, &beginInfo);
|
||||||
|
|
||||||
|
// change layout to transfer dst
|
||||||
|
{
|
||||||
|
VkImageMemoryBarrier imageMemoryBarrier = {};
|
||||||
|
imageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
||||||
|
imageMemoryBarrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
||||||
|
imageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
||||||
|
imageMemoryBarrier.image = image;
|
||||||
|
imageMemoryBarrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||||
|
imageMemoryBarrier.subresourceRange.layerCount = 1;
|
||||||
|
imageMemoryBarrier.subresourceRange.levelCount = 1;
|
||||||
|
|
||||||
|
vkCmdPipelineBarrier(
|
||||||
|
commandBuffer,
|
||||||
|
VK_PIPELINE_STAGE_TRANSFER_BIT,
|
||||||
|
VK_PIPELINE_STAGE_TRANSFER_BIT,
|
||||||
|
0,
|
||||||
|
0, nullptr,
|
||||||
|
0, nullptr,
|
||||||
|
1, &imageMemoryBarrier);
|
||||||
|
}
|
||||||
|
|
||||||
|
VkBufferImageCopy region = {};
|
||||||
|
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||||
|
region.imageSubresource.layerCount = 1;
|
||||||
|
region.imageExtent.width = width;
|
||||||
|
region.imageExtent.height = height;
|
||||||
|
region.imageExtent.depth = 1;
|
||||||
|
|
||||||
|
vkCmdCopyBufferToImage(
|
||||||
|
commandBuffer,
|
||||||
|
stagingBuffer,
|
||||||
|
image,
|
||||||
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
||||||
|
1,
|
||||||
|
®ion
|
||||||
|
);
|
||||||
|
|
||||||
|
// change layout to shader read only
|
||||||
|
{
|
||||||
|
VkImageMemoryBarrier imageMemoryBarrier = {};
|
||||||
|
imageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
||||||
|
imageMemoryBarrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
||||||
|
imageMemoryBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
||||||
|
imageMemoryBarrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
||||||
|
imageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
||||||
|
imageMemoryBarrier.image = image;
|
||||||
|
imageMemoryBarrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||||
|
imageMemoryBarrier.subresourceRange.layerCount = 1;
|
||||||
|
imageMemoryBarrier.subresourceRange.levelCount = 1;
|
||||||
|
|
||||||
|
vkCmdPipelineBarrier(
|
||||||
|
commandBuffer,
|
||||||
|
VK_PIPELINE_STAGE_TRANSFER_BIT,
|
||||||
|
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
|
||||||
|
0,
|
||||||
|
0, nullptr,
|
||||||
|
0, nullptr,
|
||||||
|
1, &imageMemoryBarrier);
|
||||||
|
}
|
||||||
|
|
||||||
|
vkEndCommandBuffer(commandBuffer);
|
||||||
|
|
||||||
|
VkSubmitInfo submitInfo = {};
|
||||||
|
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
||||||
|
submitInfo.commandBufferCount = 1;
|
||||||
|
submitInfo.pCommandBuffers = &commandBuffer;
|
||||||
|
|
||||||
|
VkFenceCreateInfo fenceCreateInfo = {};
|
||||||
|
fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
|
||||||
|
|
||||||
|
VkFence fence = nullptr;
|
||||||
|
vkCreateFence(device_, &fenceCreateInfo, nullptr, &fence);
|
||||||
|
|
||||||
|
vkQueueSubmit(graphicsQueue_, 1, &submitInfo, fence);
|
||||||
|
|
||||||
|
vkWaitForFences(device_, 1, &fence, VK_TRUE, -1);
|
||||||
|
vkDestroyFence(device_, fence, nullptr);
|
||||||
|
|
||||||
|
vkFreeCommandBuffers(device_, commandPool_, 1, &commandBuffer);
|
||||||
|
|
||||||
|
vkFreeMemory(device_, stagingMemory, nullptr);
|
||||||
|
vkDestroyBuffer(device_, stagingBuffer, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
uint32_t Renderer::findMemoryType(const uint32_t typeFilter, const VkMemoryPropertyFlags properties) {
|
uint32_t Renderer::findMemoryType(const uint32_t typeFilter, const VkMemoryPropertyFlags properties) {
|
||||||
for(uint32_t i = 0; i < deviceMemoryProperties_.memoryTypeCount; i++) {
|
for(uint32_t i = 0; i < deviceMemoryProperties_.memoryTypeCount; i++) {
|
||||||
if((typeFilter & (1 << i)) && (deviceMemoryProperties_.memoryTypes[i].propertyFlags & properties) == properties) {
|
if((typeFilter & (1 << i)) && (deviceMemoryProperties_.memoryTypes[i].propertyFlags & properties) == properties) {
|
||||||
|
|
Reference in a new issue