37 glGenTextures(1, &tex_id_);
46 if (ctx !=
nullptr && is_bound_) {
67 if (!this->proxy_check(0)) {
78 glTexStorage1D(target_,
mipmaps_, internal_format,
w_);
81 glTexStorage2D(target_,
mipmaps_, internal_format,
w_,
h_);
91 glTextureParameteri(tex_id_, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
94 glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
112 glTextureBuffer(tex_id_, internal_format, gl_vbo->vbo_id_);
115 glTexBuffer(target_, internal_format, gl_vbo->vbo_id_);
129 glTextureView(tex_id_,
136 this->layer_count());
142 stencil_texture_mode_set(use_stencil);
154void GLTexture::update_sub_direct_state_access(
155 int mip,
int offset[3],
int extent[3], GLenum
format, GLenum type,
const void *
data)
162 glCompressedTextureSubImage1D(tex_id_, mip, offset[0], extent[0],
format,
size,
data);
165 glCompressedTextureSubImage2D(
169 glCompressedTextureSubImage3D(
178 glTextureSubImage1D(tex_id_, mip, offset[0], extent[0],
format, type,
data);
205 GLenum gl_type =
to_gl(type);
209 this->update_sub_direct_state_access(mip, offset, extent, gl_format, gl_type,
data);
215 for (
int i = 0;
i < extent[2];
i++) {
216 GLenum target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + offset[2] +
i;
217 glTexSubImage2D(target, mip,
UNPACK2(offset),
UNPACK2(extent), gl_format, gl_type,
data);
222 switch (dimensions) {
225 glCompressedTexSubImage1D(target_, mip, offset[0], extent[0], gl_format,
size,
data);
228 glCompressedTexSubImage2D(
232 glCompressedTexSubImage3D(
238 switch (dimensions) {
241 glTexSubImage1D(target_, mip, offset[0], extent[0], gl_format, gl_type,
data);
244 glTexSubImage2D(target_, mip,
UNPACK2(offset),
UNPACK2(extent), gl_format, gl_type,
data);
247 glTexSubImage3D(target_, mip,
UNPACK3(offset),
UNPACK3(extent), gl_format, gl_type,
data);
258 GPUPixelBuffer *pixbuf)
273 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pix_buf_handle);
275 switch (dimensions) {
278 glTexSubImage1D(target_, 0, offset[0], extent[0], gl_format, gl_type,
nullptr);
281 glTexSubImage2D(target_, 0,
UNPACK2(offset),
UNPACK2(extent), gl_format, gl_type,
nullptr);
284 glTexSubImage3D(target_, 0,
UNPACK3(offset),
UNPACK3(extent), gl_format, gl_type,
nullptr);
288 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
319 glGenerateTextureMipmap(tex_id_);
323 glGenerateMipmap(target_);
358 int extent[3] = {1, 1, 1};
361 src->tex_id_, target_, mip, 0, 0, 0, dst->tex_id_, target_, mip, 0, 0, 0,
UNPACK3(extent));
373 int extent[3] = {1, 1, 1};
376 size_t sample_len = extent[0] * extent[1] * extent[2];
378 size_t texture_size = sample_len * sample_size;
385 GLenum gl_type =
to_gl(type);
388 glGetTextureImage(tex_id_, mip, gl_format, gl_type, texture_size,
data);
393 size_t cube_face_size = texture_size / 6;
394 char *pdata = (
char *)
data;
395 for (
int i = 0;
i < 6;
i++, pdata += cube_face_size) {
396 glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X +
i, mip, gl_format, gl_type, pdata);
400 glGetTexImage(target_, mip, gl_format, gl_type,
data);
419 glTextureParameteriv(tex_id_, GL_TEXTURE_SWIZZLE_RGBA, gl_swizzle);
423 glTexParameteriv(target_, GL_TEXTURE_SWIZZLE_RGBA, gl_swizzle);
427void GLTexture::stencil_texture_mode_set(
bool use_stencil)
430 GLint value = use_stencil ? GL_STENCIL_INDEX : GL_DEPTH_COMPONENT;
432 glTextureParameteri(tex_id_, GL_DEPTH_STENCIL_TEXTURE_MODE, value);
436 glTexParameteri(target_, GL_DEPTH_STENCIL_TEXTURE_MODE, value);
446 glTextureParameteri(tex_id_, GL_TEXTURE_BASE_LEVEL,
min);
447 glTextureParameteri(tex_id_, GL_TEXTURE_MAX_LEVEL,
max);
451 glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL,
min);
452 glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL,
max);
477 switch (extend_mode) {
479 return GL_CLAMP_TO_EDGE;
483 return GL_MIRRORED_REPEAT;
485 return GL_CLAMP_TO_BORDER;
488 return GL_CLAMP_TO_EDGE;
499 glGenSamplers(samplers_state_cache_count_, &samplers_state_cache_[0][0][0]);
503 const GLenum extend_t =
to_gl(extend_yz);
507 const GLenum extend_s =
to_gl(extend_x);
515 GL_LINEAR_MIPMAP_LINEAR :
518 GL_NEAREST_MIPMAP_LINEAR :
523 GLuint sampler = samplers_state_cache_[extend_yz_i][extend_x_i][filtering_i];
524 glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, extend_s);
525 glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, extend_t);
526 glSamplerParameteri(sampler, GL_TEXTURE_WRAP_R, extend_t);
527 glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, min_filter);
528 glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, mag_filter);
549 glSamplerParameteri(compare_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
550 glSamplerParameteri(compare_sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
551 glSamplerParameteri(compare_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
552 glSamplerParameteri(compare_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
553 glSamplerParameteri(compare_sampler, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
554 glSamplerParameteri(compare_sampler, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
555 glSamplerParameteri(compare_sampler, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
562 glSamplerParameteri(icon_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
563 glSamplerParameteri(icon_sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
574 float max_anisotropy = 1.0f;
575 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_anisotropy);
577 const float anisotropic_filter =
min_ff(max_anisotropy,
U.anisotropic_filter);
587 glSamplerParameterf(samplers_state_cache_[extend_yz_i][extend_x_i][filtering_i],
588 GL_TEXTURE_MAX_ANISOTROPY_EXT,
598 glDeleteSamplers(samplers_state_cache_count_, &samplers_state_cache_[0][0][0]);
608 return custom_samplers_state_cache_[
sampler_state.custom_type];
623bool GLTexture::proxy_check(
int mip)
632 int size[3] = {1, 1, 1};
642 if (
size[0] > max_3d_size ||
size[1] > max_3d_size ||
size[2] > max_3d_size) {
647 if (
size[0] > max_size ||
size[1] > max_size) {
652 if (
size[0] > max_size) {
657 if (
size[0] > max_cube_size) {
683 switch (dimensions) {
686 glCompressedTexImage1D(gl_proxy, mip,
size[0], 0, gl_format, img_size,
nullptr);
689 glCompressedTexImage2D(gl_proxy, mip,
UNPACK2(
size), 0, gl_format, img_size,
nullptr);
692 glCompressedTexImage3D(gl_proxy, mip,
UNPACK3(
size), 0, gl_format, img_size,
nullptr);
697 switch (dimensions) {
700 glTexImage1D(gl_proxy, mip, internal_format,
size[0], 0, gl_format, gl_type,
nullptr);
704 gl_proxy, mip, internal_format,
UNPACK2(
size), 0, gl_format, gl_type,
nullptr);
708 gl_proxy, mip, internal_format,
UNPACK3(
size), 0, gl_format, gl_type,
nullptr);
714 glGetTexLevelParameteriv(gl_proxy, 0, GL_TEXTURE_WIDTH, &width);
740 attachment_read.
tex ==
nullptr)
744 "Feedback loop: Trying to bind a texture (%s) with mip range %d-%d but mip %d is "
745 "attached to the active framebuffer (%s)",
771 glGenBuffers(1, &gl_id_);
781 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_id_);
782 glBufferData(GL_PIXEL_UNPACK_BUFFER,
size,
nullptr, GL_DYNAMIC_DRAW);
783 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
791 glDeleteBuffers(1, &gl_id_);
801 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_id_);
802 void *
ptr = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
809 glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
810 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
818 return native_handle;
#define BLI_assert_unreachable()
MINLINE float min_ff(float a, float b)
MINLINE int max_ii(int a, int b)
#define SNPRINTF(dst, format,...)
int GPU_max_texture_3d_size()
int GPU_max_texture_layers()
bool GPU_depth_blitting_workaround()
bool GPU_mip_render_workaround()
int GPU_max_texture_size()
GPUFrameBuffer * GPU_framebuffer_create(const char *name)
#define GPU_ATTACHMENT_TEXTURE(_texture)
GPUFrameBuffer * GPU_framebuffer_active_get()
void GPU_framebuffer_free(GPUFrameBuffer *fb)
void GPU_framebuffer_bind(GPUFrameBuffer *fb)
@ GPU_SAMPLER_CUSTOM_ICON
@ GPU_SAMPLER_CUSTOM_COMPARE
GPUPixelBufferNativeHandle GPU_pixel_buffer_get_native_handle(GPUPixelBuffer *pixel_buf)
@ GPU_SAMPLER_STATE_TYPE_CUSTOM
@ GPU_SAMPLER_STATE_TYPE_INTERNAL
static const int GPU_SAMPLER_FILTERING_TYPES_COUNT
@ GPU_SAMPLER_EXTEND_MODE_MIRRORED_REPEAT
@ GPU_SAMPLER_EXTEND_MODE_REPEAT
@ GPU_SAMPLER_EXTEND_MODE_EXTEND
@ GPU_SAMPLER_EXTEND_MODE_CLAMP_TO_BORDER
#define GPU_SAMPLER_CUSTOM_TYPES_COUNT
@ GPU_SAMPLER_FILTERING_MIPMAP
@ GPU_SAMPLER_FILTERING_ANISOTROPIC
@ GPU_SAMPLER_FILTERING_LINEAR
#define GPU_SAMPLER_EXTEND_MODES_COUNT
BMesh const char void * data
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
StateManager * state_manager
void attachment_set(GPUAttachmentType type, const GPUAttachment &new_attachment)
static bool stencil_texturing_support
static GLStateManager * state_manager_active_get()
static bool direct_state_access_support
static void tex_free(GLuint tex_id)
static bool texture_filter_anisotropic_support
static GLint max_cubemap_size
static bool generate_mipmap_workaround
GPUPixelBufferNativeHandle get_native_handle() override
GLPixelBuffer(size_t size)
size_t get_size() override
void texture_bind_temp(GLTexture *tex)
GLTexture(const char *name)
void copy_to(Texture *dst) override
void mip_range_set(int min, int max) override
friend class GLFrameBuffer
static void samplers_init()
static GLuint get_sampler(const GPUSamplerState &sampler_state)
static void samplers_update()
uint gl_bindcode_get() const override
void * read(int mip, eGPUDataFormat type) override
static void samplers_free()
bool init_internal() override
void check_feedback_loop()
void generate_mipmap() override
void update_sub(int mip, int offset[3], int extent[3], eGPUDataFormat type, const void *data) override
void clear(eGPUDataFormat format, const void *data) override
void swizzle_set(const char swizzle_mask[4]) override
virtual void texture_unbind(Texture *tex)=0
virtual void image_unbind(Texture *tex)=0
GPUSamplerState sampler_state
eGPUTextureFormat format_
eGPUTextureFormatFlag format_flag_
char name_[DEBUG_NAME_LEN]
FrameBuffer * fb_[GPU_TEX_MAX_FBO_ATTACHED]
int dimensions_count() const
GPUAttachmentType fb_attachment_[GPU_TEX_MAX_FBO_ATTACHED]
GPUAttachmentType attachment_type(int slot) const
void mip_size_get(int mip, int r_size[3]) const
Texture(const char *name)
BLI_INLINE float fb(float length, float L)
void * MEM_mallocN(size_t len, const char *str)
void raise_gl_error(const char *info)
void object_label(GLenum type, GLuint object, const char *name)
static Context * unwrap(GPUContext *ctx)
GLenum to_gl_target(eGPUTextureType type)
size_t to_block_size(eGPUTextureFormat data_type)
static GPUContext * wrap(Context *ctx)
GLenum to_gl_internal_format(eGPUTextureFormat format)
constexpr bool validate_data_format(eGPUTextureFormat tex_format, eGPUDataFormat data_format)
int to_bytesize(const DataFormat format)
GLenum to_gl_proxy(eGPUTextureType type)
GLenum to_gl_data_format(eGPUTextureFormat format)
GLenum swizzle_to_gl(const char swizzle)
constexpr DataFormat to_data_format(TextureFormat format)
static GLenum to_gl(const GPUAttachmentType type)