forked from Themaister/Granite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conservative_raster_test.cpp
116 lines (99 loc) · 4.15 KB
/
conservative_raster_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* Copyright (c) 2017-2024 Hans-Kristian Arntzen
*
* 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.
*/
#include "application.hpp"
#include "command_buffer.hpp"
#include "device.hpp"
#include "os_filesystem.hpp"
#include "math.hpp"
#include <string.h>
using namespace Granite;
using namespace Vulkan;
struct ConservativeRasterApplication : Granite::Application, Granite::EventHandler
{
void render_frame(double, double)
{
auto &wsi = get_wsi();
auto &device = wsi.get_device();
auto info = ImageCreateInfo::render_target(4, 4, VK_FORMAT_R8G8B8A8_UNORM);
info.initial_layout = VK_IMAGE_LAYOUT_UNDEFINED;
info.usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
auto image = device.create_image(info);
auto cmd = device.request_command_buffer();
cmd->image_barrier(*image, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, 0,
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT);
RenderPassInfo rp;
rp.num_color_attachments = 1;
rp.color_attachments[0] = &image->get_view();
rp.store_attachments = 1;
rp.clear_attachments = 1;
cmd->begin_render_pass(rp);
cmd->set_opaque_state();
cmd->set_conservative_rasterization(true);
cmd->set_program("assets://shaders/triangle.vert", "assets://shaders/triangle.frag");
auto *pos = static_cast<vec2 *>(cmd->allocate_vertex_data(0, sizeof(vec2) * 3, sizeof(vec2)));
auto *color = static_cast<vec4 *>(cmd->allocate_vertex_data(1, sizeof(vec4) * 3, sizeof(vec4)));
pos[0] = vec2(-1.0f, -1.0f);
pos[1] = vec2(-1.0f, -0.95f);
pos[2] = vec2(-0.95, -1.0f);
color[0] = vec4(1.0f, 0.0f, 0.0f, 1.0f);
color[1] = vec4(0.0f, 1.0f, 0.0f, 1.0f);
color[2] = vec4(0.0f, 0.0f, 1.0f, 1.0f);
cmd->set_vertex_attrib(0, 0, VK_FORMAT_R32G32_SFLOAT, 0);
cmd->set_vertex_attrib(1, 1, VK_FORMAT_R32G32B32A32_SFLOAT, 0);
cmd->set_specialization_constant_mask(0xf);
cmd->set_specialization_constant(0, 1.0f);
cmd->set_specialization_constant(1, 1.0f);
cmd->set_specialization_constant(2, 1.0f);
cmd->set_specialization_constant(3, 1.0f);
cmd->draw(3);
cmd->end_render_pass();
cmd->image_barrier(*image, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, VK_ACCESS_SHADER_READ_BIT);
rp = device.get_swapchain_render_pass(SwapchainRenderPass::ColorOnly);
cmd->begin_render_pass(rp);
cmd->set_texture(0, 0, image->get_view(), StockSampler::LinearClamp);
CommandBufferUtil::draw_fullscreen_quad(*cmd, "builtin://shaders/quad.vert", "builtin://shaders/blit.frag");
cmd->end_render_pass();
device.submit(cmd);
}
ImageHandle images[4];
};
namespace Granite
{
Application *application_create(int, char **)
{
GRANITE_APPLICATION_SETUP_FILESYSTEM();
try
{
auto *app = new ConservativeRasterApplication();
return app;
}
catch (const std::exception &e)
{
LOGE("application_create() threw exception: %s\n", e.what());
return nullptr;
}
}
}