Blender  V2.93
gpu_framebuffer_private.hh
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2020 Blender Foundation.
17  * All rights reserved.
18  */
19 
26 #pragma once
27 
28 #include "BLI_math_vector.h"
29 #include "BLI_span.hh"
30 
31 #include "MEM_guardedalloc.h"
32 
33 #include "GPU_framebuffer.h"
34 
35 struct GPUTexture;
36 
37 typedef enum GPUAttachmentType : int {
46  /* Number of maximum output slots.
47  * We support 6 outputs for now (usually we wouldn't need more to preserve fill rate). */
48  /* Keep in mind that GL max is GL_MAX_DRAW_BUFFERS and is at least 8, corresponding to
49  * the maximum number of COLOR attachments specified by glDrawBuffers. */
51 
53 
54 #define GPU_FB_MAX_COLOR_ATTACHMENT (GPU_FB_MAX_ATTACHMENT - GPU_FB_COLOR_ATTACHMENT0)
55 
57 {
58  return static_cast<GPUAttachmentType>(static_cast<int>(a) - b);
59 }
60 
62 {
63  return static_cast<GPUAttachmentType>(static_cast<int>(a) + b);
64 }
65 
67 {
68  a = a + 1;
69  return a;
70 }
71 
73 {
74  a = a - 1;
75  return a;
76 }
77 
78 namespace blender {
79 namespace gpu {
80 
81 #ifdef DEBUG
82 # define DEBUG_NAME_LEN 64
83 #else
84 # define DEBUG_NAME_LEN 16
85 #endif
86 
87 class FrameBuffer {
88  protected:
98  int viewport_[4] = {0};
99  int scissor_[4] = {0};
100  bool scissor_test_ = false;
101  bool dirty_state_ = true;
102 
103  public:
104  FrameBuffer(const char *name);
105  virtual ~FrameBuffer();
106 
107  virtual void bind(bool enabled_srgb) = 0;
108  virtual bool check(char err_out[256]) = 0;
109  virtual void clear(eGPUFrameBufferBits buffers,
110  const float clear_col[4],
111  float clear_depth,
112  uint clear_stencil) = 0;
113  virtual void clear_multi(const float (*clear_col)[4]) = 0;
115  eGPUDataFormat data_format,
116  const void *clear_value) = 0;
117 
118  virtual void read(eGPUFrameBufferBits planes,
120  const int area[4],
121  int channel_len,
122  int slot,
123  void *r_data) = 0;
124 
125  virtual void blit_to(eGPUFrameBufferBits planes,
126  int src_slot,
127  FrameBuffer *dst,
128  int dst_slot,
129  int dst_offset_x,
130  int dst_offset_y) = 0;
131 
132  void attachment_set(GPUAttachmentType type, const GPUAttachment &new_attachment);
134 
135  void recursive_downsample(int max_lvl,
136  void (*callback)(void *userData, int level),
137  void *userData);
138 
139  inline void size_set(int width, int height)
140  {
141  width_ = width;
142  height_ = height;
143  dirty_state_ = true;
144  }
145 
146  inline void viewport_set(const int viewport[4])
147  {
148  if (!equals_v4v4_int(viewport_, viewport)) {
149  copy_v4_v4_int(viewport_, viewport);
150  dirty_state_ = true;
151  }
152  }
153 
154  inline void scissor_set(const int scissor[4])
155  {
156  if (!equals_v4v4_int(scissor_, scissor)) {
157  copy_v4_v4_int(scissor_, scissor);
158  dirty_state_ = true;
159  }
160  }
161 
162  inline void scissor_test_set(bool test)
163  {
164  scissor_test_ = test;
165  }
166 
167  inline void viewport_get(int r_viewport[4]) const
168  {
169  copy_v4_v4_int(r_viewport, viewport_);
170  }
171 
172  inline void scissor_get(int r_scissor[4]) const
173  {
174  copy_v4_v4_int(r_scissor, scissor_);
175  }
176 
177  inline bool scissor_test_get(void) const
178  {
179  return scissor_test_;
180  }
181 
182  inline void viewport_reset(void)
183  {
184  int viewport_rect[4] = {0, 0, width_, height_};
185  viewport_set(viewport_rect);
186  }
187 
188  inline void scissor_reset(void)
189  {
190  int scissor_rect[4] = {0, 0, width_, height_};
191  scissor_set(scissor_rect);
192  }
193 
194  inline GPUTexture *depth_tex(void) const
195  {
198  }
200  };
201 
202  inline GPUTexture *color_tex(int slot) const
203  {
205  };
206 };
207 
208 /* Syntactic sugar. */
209 static inline GPUFrameBuffer *wrap(FrameBuffer *vert)
210 {
211  return reinterpret_cast<GPUFrameBuffer *>(vert);
212 }
213 static inline FrameBuffer *unwrap(GPUFrameBuffer *vert)
214 {
215  return reinterpret_cast<FrameBuffer *>(vert);
216 }
217 static inline const FrameBuffer *unwrap(const GPUFrameBuffer *vert)
218 {
219  return reinterpret_cast<const FrameBuffer *>(vert);
220 }
221 
222 #undef DEBUG_NAME_LEN
223 
224 } // namespace gpu
225 } // namespace blender
MINLINE bool equals_v4v4_int(const int v1[4], const int v2[4]) ATTR_WARN_UNUSED_RESULT
MINLINE void copy_v4_v4_int(int r[4], const int a[4])
unsigned int uint
Definition: BLI_sys_types.h:83
struct GPUFrameBuffer GPUFrameBuffer
eGPUFrameBufferBits
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei width
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei height
struct GPUTexture GPUTexture
Definition: GPU_texture.h:33
eGPUDataFormat
Definition: GPU_texture.h:171
Read Guarded memory(de)allocation.
void attachment_remove(GPUAttachmentType type)
GPUTexture * depth_tex(void) const
void size_set(int width, int height)
virtual void read(eGPUFrameBufferBits planes, eGPUDataFormat format, const int area[4], int channel_len, int slot, void *r_data)=0
void viewport_set(const int viewport[4])
void scissor_set(const int scissor[4])
virtual void clear_attachment(GPUAttachmentType type, eGPUDataFormat data_format, const void *clear_value)=0
virtual void bind(bool enabled_srgb)=0
void scissor_get(int r_scissor[4]) const
virtual void clear_multi(const float(*clear_col)[4])=0
FrameBuffer(const char *name)
virtual bool check(char err_out[256])=0
GPUTexture * color_tex(int slot) const
virtual void clear(eGPUFrameBufferBits buffers, const float clear_col[4], float clear_depth, uint clear_stencil)=0
void recursive_downsample(int max_lvl, void(*callback)(void *userData, int level), void *userData)
void viewport_get(int r_viewport[4]) const
GPUAttachment attachments_[GPU_FB_MAX_ATTACHMENT]
virtual void blit_to(eGPUFrameBufferBits planes, int src_slot, FrameBuffer *dst, int dst_slot, int dst_offset_x, int dst_offset_y)=0
void attachment_set(GPUAttachmentType type, const GPUAttachment &new_attachment)
DEGForeachIDComponentCallback callback
@ GPU_FB_DEPTH_STENCIL_ATTACHMENT
@ GPU_FB_COLOR_ATTACHMENT5
@ GPU_FB_COLOR_ATTACHMENT2
@ GPU_FB_COLOR_ATTACHMENT3
@ GPU_FB_MAX_ATTACHMENT
@ GPU_FB_COLOR_ATTACHMENT4
@ GPU_FB_COLOR_ATTACHMENT1
@ GPU_FB_COLOR_ATTACHMENT0
@ GPU_FB_DEPTH_ATTACHMENT
#define DEBUG_NAME_LEN
GPUAttachmentType & operator--(GPUAttachmentType &a)
GPUAttachmentType & operator++(GPUAttachmentType &a)
constexpr GPUAttachmentType operator-(GPUAttachmentType a, int b)
constexpr GPUAttachmentType operator+(GPUAttachmentType a, int b)
format
Definition: logImageCore.h:47
static unsigned a[3]
Definition: RandGen.cpp:92
static void area(int d1, int d2, int e1, int e2, float weights[2])
static GPUContext * wrap(Context *ctx)
static Context * unwrap(GPUContext *ctx)
struct GPUTexture * tex