summaryrefslogtreecommitdiff
path: root/chromium/gpu/vulkan/vulkan_render_pass.h
blob: 326e99b235cfe77a1fccf6a74434248fb924078a (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef GPU_VULKAN_VULKAN_RENDER_PASS_H_
#define GPU_VULKAN_VULKAN_RENDER_PASS_H_

#include <vector>
#include <vulkan/vulkan.h>

#include "base/macros.h"
#include "gpu/vulkan/vulkan_export.h"

namespace gpu {

class CommandBufferRecorderBase;
class VulkanDeviceQueue;
class VulkanImageView;
class VulkanSwapChain;

class VULKAN_EXPORT VulkanRenderPass {
 public:
  enum class AttachmentType {
    // Use image view of the swap chain image.
    ATTACHMENT_TYPE_SWAP_IMAGE,

    // Use image view of the attachment data.
    ATTACHMENT_TYPE_ATTACHMENT_VIEW,
  };

  enum class ImageLayoutType {
    // Undefined image layout.
    IMAGE_LAYOUT_UNDEFINED,

    // Image layout whiches matches the image view.
    IMAGE_LAYOUT_TYPE_IMAGE_VIEW,

    // Image layout for presenting.
    IMAGE_LAYOUT_TYPE_PRESENT,
  };

  struct AttachmentData {
    AttachmentType attachment_type;
    VkSampleCountFlagBits sample_count;
    VkAttachmentLoadOp load_op;
    VkAttachmentStoreOp store_op;
    // The stencil ops are only used for IMAGE_TYPE_STENCIL and
    // IMAGE_TYPE_DEPTH_STENCIL image views.
    VkAttachmentLoadOp stencil_load_op;
    VkAttachmentStoreOp stencil_store_op;
    ImageLayoutType start_layout;
    ImageLayoutType end_layout;
    VulkanImageView* image_view;  // used for ATTACHMENT_TYPE_ATTACHMENT_VIEW.
    VkClearValue clear_value;     // used for VK_ATTACHMENT_LOAD_OP_CLEAR.

    bool ValidateData(const VulkanSwapChain* swap_chain) const;
  };

  struct SubpassAttachment {
    uint32_t attachment_index;
    ImageLayoutType subpass_layout;
  };

  struct SubpassData {
    SubpassData();
    ~SubpassData();

    std::vector<SubpassAttachment> subpass_attachments;

    bool ValidateData(uint32_t num_attachments) const;
  };

  struct RenderPassData {
    RenderPassData();
    ~RenderPassData();

    std::vector<AttachmentData> attachments;
    std::vector<SubpassData> subpass_datas;

    bool ValidateData(const VulkanSwapChain* swap_chain) const;
  };

  explicit VulkanRenderPass(VulkanDeviceQueue* device_queue);
  ~VulkanRenderPass();

  bool Initialize(const VulkanSwapChain* swap_chain,
                  const RenderPassData& render_pass_data);
  void Destroy();

  // Begins render pass to command_buffer. The variable exec_inline signifies
  // whether or not the subpass commands will be executed inline (within a
  // primary command buffer) or whether it will be executed through a secondary
  // command buffer.
  void BeginRenderPass(const CommandBufferRecorderBase& recorder,
                       bool exec_inline);

  // Begins the next subpass after BeginRenderPass has been called.
  void NextSubPass(const CommandBufferRecorderBase& recorder);

  // Ends the render passes.
  void EndRenderPass(const CommandBufferRecorderBase& recorder);

  void SetClearValue(uint32_t attachment_index, VkClearValue clear_value);

 private:
  VulkanDeviceQueue* device_queue_ = nullptr;
  const VulkanSwapChain* swap_chain_ = nullptr;
  uint32_t num_sub_passes_ = 0;
  uint32_t current_sub_pass_ = 0;
  bool executing_ = false;
  VkSubpassContents execution_type_ = VK_SUBPASS_CONTENTS_INLINE;
  VkRenderPass render_pass_ = VK_NULL_HANDLE;

  // There is 1 clear color for every attachment which needs a clear.
  std::vector<VkClearValue> attachment_clear_values_;

  // There is 1 clear index for every attachment which needs a clear. This is
  // kept in a separate array since it is only used setting clear values.
  std::vector<uint32_t> attachment_clear_indexes_;

  // There is 1 frame buffer for every swap chain image.
  std::vector<VkFramebuffer> frame_buffers_;

  DISALLOW_COPY_AND_ASSIGN(VulkanRenderPass);
};

}  // namespace gpu

#endif  // GPU_VULKAN_VULKAN_RENDER_PASS_H_