MCPcopy Create free account
hub / github.com/KhronosGroup/Vulkan-Samples / prepare_offscreen_buffer

Method prepare_offscreen_buffer

samples/api/hdr/hdr.cpp:288–527  ·  view source on GitHub ↗

Prepare a new framebuffer and attachments for offscreen rendering (G-Buffer)

Source from the content-addressed store, hash-verified

286
287// Prepare a new framebuffer and attachments for offscreen rendering (G-Buffer)
288void HDR::prepare_offscreen_buffer()
289{
290 // We need to select a format that supports the color attachment blending flag, so we iterate over multiple formats to find one that supports this flag
291 const std::vector<VkFormat> float_format_priority_list = {
292 VK_FORMAT_R32G32B32A32_SFLOAT,
293 VK_FORMAT_R16G16B16A16_SFLOAT // Guaranteed blend support for this
294 };
295
296 VkFormat color_format = vkb::choose_blendable_format(get_device().get_gpu().get_handle(), float_format_priority_list);
297
298 {
299 offscreen.width = width;
300 offscreen.height = height;
301
302 // Color attachments
303
304 // We are using two 128-Bit RGBA floating point color buffers for this sample
305 // In a performance or bandwidth-limited scenario you should consider using a format with lower precision
306 create_attachment(color_format, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, &offscreen.color[0]);
307 create_attachment(color_format, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, &offscreen.color[1]);
308 // Depth attachment
309 create_attachment(depth_format, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, &offscreen.depth);
310
311 // Set up separate renderpass with references to the color and depth attachments
312 std::array<VkAttachmentDescription, 3> attachment_descriptions = {};
313
314 // Init attachment properties
315 for (uint32_t i = 0; i < 3; ++i)
316 {
317 attachment_descriptions[i].samples = VK_SAMPLE_COUNT_1_BIT;
318 attachment_descriptions[i].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
319 attachment_descriptions[i].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
320 attachment_descriptions[i].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
321 attachment_descriptions[i].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
322 if (i == 2)
323 {
324 attachment_descriptions[i].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
325 attachment_descriptions[i].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
326 }
327 else
328 {
329 attachment_descriptions[i].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
330 attachment_descriptions[i].finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
331 }
332 }
333
334 // Formats
335 attachment_descriptions[0].format = offscreen.color[0].format;
336 attachment_descriptions[1].format = offscreen.color[1].format;
337 attachment_descriptions[2].format = offscreen.depth.format;
338
339 std::vector<VkAttachmentReference> color_references;
340 color_references.push_back({0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL});
341 color_references.push_back({1, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL});
342
343 VkAttachmentReference depth_reference = {};
344 depth_reference.attachment = 2;
345 depth_reference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;

Callers

nothing calls this directly

Calls 5

sampler_create_infoFunction · 0.85
sizeMethod · 0.80
choose_blendable_formatFunction · 0.50
make_filters_validFunction · 0.50
get_handleMethod · 0.45

Tested by

no test coverage detected