Unverified Commit caa44f65 authored by Laszlo Agocs's avatar Laszlo Agocs Committed by GitHub
Browse files

Merge pull request #3 from alpqr/nativeres2

parents 9e046d43 276dbaa8
......@@ -293,7 +293,7 @@ void Window::init()
m_ds = m_r->newRenderBuffer(QRhiRenderBuffer::DepthStencil,
QSize(), // no need to set the size yet
1,
QRhiRenderBuffer::ToBeUsedWithSwapChainOnly);
QRhiRenderBuffer::UsedWithSwapChainOnly);
releasePool << m_ds;
m_sc->setWindow(this);
m_sc->setDepthStencil(m_ds);
......
qsb --glsl "100 es,120" --hlsl 50 --msl 12 imgui.vert -o imgui.vert.qsb
qsb --glsl "100 es,120" --hlsl 50 --msl 12 imgui.frag -o imgui.frag.qsb
#version 440
layout(location = 0) in vec2 v_texcoord;
layout(location = 1) in vec4 v_color;
layout(location = 0) out vec4 fragColor;
layout(std140, binding = 0) uniform buf {
mat4 mvp;
int flip;
float opacity;
} ubuf;
layout(binding = 1) uniform sampler2D tex;
void main()
{
vec4 c = texture(tex, v_texcoord);
fragColor = vec4(c.rgb * c.a, c.a);
vec4 c = v_color * texture(tex, v_texcoord);
fragColor = vec4(c.rgb, c.a * ubuf.opacity);
}
......@@ -2,20 +2,21 @@
layout(location = 0) in vec4 position;
layout(location = 1) in vec2 texcoord;
layout(location = 2) in vec4 color;
layout(location = 0) out vec2 v_texcoord;
layout(location = 1) out vec4 v_color;
layout(std140, binding = 0) uniform buf {
mat4 mvp;
int flip;
float opacity;
} ubuf;
out gl_PerVertex { vec4 gl_Position; };
void main()
{
v_texcoord = vec2(texcoord.x, texcoord.y);
if (ubuf.flip != 0)
v_texcoord.y = 1.0 - v_texcoord.y;
gl_Position = ubuf.mvp * position;
v_texcoord = texcoord;
v_color = color;
gl_Position = ubuf.mvp * vec4(position.xy, 0.0, 1.0);
}
The MIT License (MIT)
Copyright (c) 2014-2018 Omar Cornut
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.
//-----------------------------------------------------------------------------
// 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_WIN32_FUNCTIONS // [Win32] Won't use and link with any Win32 function.
//#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);
}
*/
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
[
{
"Id": "imgui",
"Name": "Dear ImGui",
"QDocModule": "qtrhi",
"Description": "Dear ImGui",
"QtUsage": "Bloat-free GUI library for in-scene visualization of profiling and debugging data",
"Homepage": "https://github.com/ocornut/imgui",
"Version": "v1.66b",
"License": "MIT License",
"LicenseId": "MIT",
"LicenseFile": "LICENSE.txt",
"Copyright": "Copyright (c) 2014-2018 Omar Cornut"
}
]
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
......@@ -48,73 +48,118 @@
**
****************************************************************************/
#include "renderer.h"
#include <QVulkanFunctions>
#include <QRhiVulkanInitParams>
const int SAMPLES = 1;
Renderer::Renderer(QVulkanWindow *w)
: m_window(w)
#include "../shared/examplefw.h"
#include "qrhiimgui.h"
static float vertexData[] = {
0.0f, 0.5f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
};
struct {
QVector<QRhiResource *> releasePool;
QRhiBuffer *vbuf = nullptr;
QRhiBuffer *ubuf = nullptr;
QRhiShaderResourceBindings *srb = nullptr;
QRhiGraphicsPipeline *ps = nullptr;
QRhiResourceUpdateBatch *initialUpdates = nullptr;
float rotation = 0;
QRhiImgui imgui;
} d;
void Window::customInit()
{
m_window->setSampleCount(SAMPLES);
d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData));
d.vbuf->build();
d.releasePool << d.vbuf;
d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64 + 4);
d.ubuf->build();
d.releasePool << d.ubuf;
d.initialUpdates = m_r->nextResourceUpdateBatch();
d.initialUpdates->uploadStaticBuffer(d.vbuf, vertexData);
float opacity = 1.0f;
d.initialUpdates->updateDynamicBuffer(d.ubuf, 64, 4, &opacity);
d.srb = m_r->newShaderResourceBindings();
d.releasePool << d.srb;
d.srb->setBindings({
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf),
});
d.srb->build();
d.ps = m_r->newGraphicsPipeline();
d.releasePool << d.ps;
QBakedShader vs = getShader(QLatin1String(":/color.vert.qsb"));
Q_ASSERT(vs.isValid());
QBakedShader fs = getShader(QLatin1String(":/color.frag.qsb"));
Q_ASSERT(fs.isValid());
d.ps->setShaderStages({
{ QRhiGraphicsShaderStage::Vertex, vs },
{ QRhiGraphicsShaderStage::Fragment, fs }
});
QRhiVertexInputLayout inputLayout;
inputLayout.bindings = {
{ 5 * sizeof(float) }
};
inputLayout.attributes = {
{ 0, 0, QRhiVertexInputLayout::Attribute::Float2, 0 },
{ 0, 1, QRhiVertexInputLayout::Attribute::Float3, 2 * sizeof(float) }
};
d.ps->setVertexInputLayout(inputLayout);
d.ps->setShaderResourceBindings(d.srb);
d.ps->setRenderPassDescriptor(m_rp);
d.ps->build();
d.imgui.initialize(m_r);
d.imgui.setInputEventSource(this);
d.imgui.setFrameFunc([] {
d.imgui.demoWindow();
});
}
void Renderer::initResources()
void Window::customRelease()
{
QRhiVulkanInitParams params;
params.inst = m_window->vulkanInstance();
params.importExistingDevice = true;
params.physDev = m_window->physicalDevice();
params.dev = m_window->device();
params.cmdPool = m_window->graphicsCommandPool();
params.gfxQueue = m_window->graphicsQueue();
m_r = QRhi::create(QRhi::Vulkan, &params);
m_triRenderer.setRhi(m_r);
m_triRenderer.setSampleCount(SAMPLES);
m_triRenderer.initResources();
m_sc = m_r->createSwapChain();
}
d.imgui.releaseResources();
void Renderer::initSwapChainResources()
{
m_sc->build(m_window); // this just wraps the window's swapchain
m_triRenderer.initOutputDependentResources(m_sc->defaultRenderPass(), m_sc->effectiveSizeInPixels());
}
void Renderer::releaseSwapChainResources()
{
m_triRenderer.releaseOutputDependentResources();
m_sc->release(); // no-op, the real work is done by QVulkanWindow
for (QRhiResource *r : d.releasePool)
r->releaseAndDestroy();
d.releasePool.clear();
}
void Renderer::releaseResources()
void Window::customRender()
{
m_triRenderer.releaseResources();
const QSize outputSizeInPixels = m_sc->currentPixelSize();
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
QRhiRenderTarget *rt = m_sc->currentFrameRenderTarget();
QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch();
delete m_sc;
m_sc = nullptr;
if (d.initialUpdates) {
u->merge(d.initialUpdates);
d.initialUpdates->release();
d.initialUpdates = nullptr;
}
delete m_r;
m_r = nullptr;
}
QMatrix4x4 mvp = m_proj;
d.rotation += 1;
mvp.rotate(d.rotation, 0, 1, 0);
u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData());
void Renderer::startNextFrame()
{
m_r->beginFrame(m_sc);
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch();
m_triRenderer.queueResourceUpdates(u);
d.imgui.prepareFrame(rt, m_rp, u);
m_r->beginPass(m_sc->currentFrameRenderTarget(), cb, { 0.4f, 0.7f, 0.0f, 1.0f }, { 1.0f, 0 }, u);
m_triRenderer.queueDraw(cb, m_sc->effectiveSizeInPixels());
m_r->endPass(cb);
cb->beginPass(rt, { 0.4f, 0.7f, 0.0f, 1.0f }, { 1.0f, 0 }, u);
m_r->endFrame(m_sc);
cb->setGraphicsPipeline(d.ps);
cb->setViewport(QRhiViewport(0, 0, outputSizeInPixels.width(), outputSizeInPixels.height()));
cb->setVertexInput(0, { { d.vbuf, 0 } });
cb->draw(3);
m_window->frameReady();
m_window->requestUpdate(); // render continuously, throttled by the presentation rate
d.imgui.queueFrame(cb);
cb->endPass();
}
TEMPLATE = app
QT += shadertools rhi
SOURCES = \
imguidemo.cpp \
qrhiimgui.cpp \
imgui/imgui.cpp \
imgui/imgui_draw.cpp \
imgui/imgui_widgets.cpp \
imgui/imgui_demo.cpp
HEADERS = \
qrhiimgui.h \
qrhiimgui_p.h
INCLUDEPATH += imgui
RESOURCES = \
imguidemo.qrc
target.path = $$[QT_INSTALL_EXAMPLES]/rhi/imguidemo
INSTALLS += target
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment