41 glGenTextures(1, &tex_id_);
50 if (ctx !=
nullptr && (is_bound_ || is_bound_image_)) {
65 if (!this->proxy_check(0)) {
76 glTexStorage1D(target_,
mipmaps_, internal_format,
w_);
79 glTexStorage2D(target_,
mipmaps_, internal_format,
w_,
h_);
85 this->mip_range_set(0,
mipmaps_ - 1);
89 glTextureParameteri(tex_id_, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
92 glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
110 glTextureBuffer(tex_id_, internal_format, gl_vbo->vbo_id_);
113 glTexBuffer(target_, internal_format, gl_vbo->vbo_id_);
127 glTextureView(tex_id_,
139 if (
ELEM(
format_, TextureFormat::SFLOAT_32_DEPTH_UINT_8)) {
140 stencil_texture_mode_set(use_stencil);
152void GLTexture::update_sub_direct_state_access(
153 int mip,
int offset[3],
int extent[3], GLenum format, GLenum type,
const void *
data)
160 glCompressedTextureSubImage1D(tex_id_, mip, offset[0], extent[0], format, size,
data);
163 glCompressedTextureSubImage2D(
167 glCompressedTextureSubImage3D(
176 glTextureSubImage1D(tex_id_, mip, offset[0], extent[0], format, type,
data);
179 glTextureSubImage2D(tex_id_, mip,
UNPACK2(offset),
UNPACK2(extent), format, type,
data);
182 glTextureSubImage3D(tex_id_, mip,
UNPACK3(offset),
UNPACK3(extent), format, type,
data);
195 const uint unpack_row_length)
207 const bool do_texture_unpack = !
ELEM(unpack_row_length, 0, extent[0]);
210 std::unique_ptr<uint8_t, MEM_smart_ptr_deleter<uint8_t>> unpack_buffer =
nullptr;
211 if (do_texture_unpack) {
213 "Compressed data with unpack_row_length != 0 is not supported.");
215 "3D texture data with unpack_row_length != 0 is not supported.");
219 size_t dst_total_count = dst_row_stride *
max_ii(extent[1], 1) *
max_ii(extent[2], 1);
227 uint8_t *dst_ptr = unpack_buffer.get();
228 for (
int y = 0;
y <
max_ii(extent[1], 1); ++
y) {
229 std::memcpy(dst_ptr, src_ptr, dst_row_stride);
230 src_ptr += src_row_stride;
231 dst_ptr += dst_row_stride;
236 data = unpack_buffer.get();
239 GLenum gl_type =
to_gl(type);
242 std::unique_ptr<uint16_t, MEM_smart_ptr_deleter<uint16_t>> clamped_half_buffer =
nullptr;
244 size_t dst_pixel_count =
max_ii(extent[0], 1) *
max_ii(extent[1], 1) *
max_ii(extent[2], 1);
248 clamped_half_buffer.reset(
static_cast<uint16_t *
>(
254 constexpr int64_t chunk_size = 4 * 1024 * 1024;
266 data = clamped_half_buffer.get();
271 if (do_texture_unpack) {
272 unpack_buffer.reset(
nullptr);
278 std::unique_ptr<int16_t, MEM_smart_ptr_deleter<int16_t>> short_buffer =
nullptr;
280 size_t dst_pixel_count =
max_ii(extent[0], 1) *
max_ii(extent[1], 1) *
max_ii(extent[2], 1);
283 short_buffer.reset(
static_cast<int16_t *
>(
289 constexpr int64_t chunk_size = 4 * 1024 * 1024;
291 for (
const int64_t i : range) {
297 data = short_buffer.get();
302 if (do_texture_unpack) {
303 unpack_buffer.reset(
nullptr);
312 this->update_sub_direct_state_access(mip, offset, extent, gl_format, gl_type,
data);
318 for (
int i = 0; i < extent[2]; i++) {
319 GLenum target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + offset[2] + i;
320 glTexSubImage2D(target, mip,
UNPACK2(offset),
UNPACK2(extent), gl_format, gl_type,
data);
325 switch (dimensions) {
328 glCompressedTexSubImage1D(target_, mip, offset[0], extent[0], gl_format, size,
data);
331 glCompressedTexSubImage2D(
335 glCompressedTexSubImage3D(
341 switch (dimensions) {
344 glTexSubImage1D(target_, mip, offset[0], extent[0], gl_format, gl_type,
data);
347 glTexSubImage2D(target_, mip,
UNPACK2(offset),
UNPACK2(extent), gl_format, gl_type,
data);
350 glTexSubImage3D(target_, mip,
UNPACK3(offset),
UNPACK3(extent), gl_format, gl_type,
data);
361 GPUPixelBuffer *pixbuf)
369 GLenum gl_type =
to_gl(format);
376 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pix_buf_handle);
378 switch (dimensions) {
381 glTexSubImage1D(target_, 0, offset[0], extent[0], gl_format, gl_type,
nullptr);
384 glTexSubImage2D(target_, 0,
UNPACK2(offset),
UNPACK2(extent), gl_format, gl_type,
nullptr);
387 glTexSubImage3D(target_, 0,
UNPACK3(offset),
UNPACK3(extent), gl_format, gl_type,
nullptr);
391 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
411 glGenerateTextureMipmap(tex_id_);
415 glGenerateMipmap(target_);
432 for (
int mip =
mipmaps_ - 1; mip >= 0; mip--) {
447 std::max(src->
d_, 1) == std::max(dst->
d_, 1));
449 (src->
format_ == TextureFormat::SRGBA_8_8_8_8 &&
450 dst->
format_ == TextureFormat::UNORM_8_8_8_8) ||
451 (src->
format_ == TextureFormat::UNORM_8_8_8_8 &&
452 dst->
format_ == TextureFormat::SRGBA_8_8_8_8));
461 for (
int mip : mip_levels) {
463 int extent[3] = {1, 1, 1};
465 glCopyImageSubData(src_parent->tex_id_,
491 format_ == TextureFormat::SFLOAT_32_DEPTH_UINT_8 ? TextureFormat::SFLOAT_32_DEPTH :
format_);
492 GLenum gl_type =
to_gl(type);
513 glGetTextureSubImage(tex_id_,
541 glTextureParameteriv(tex_id_, GL_TEXTURE_SWIZZLE_RGBA, gl_swizzle);
545 glTexParameteriv(target_, GL_TEXTURE_SWIZZLE_RGBA, gl_swizzle);
549void GLTexture::stencil_texture_mode_set(
bool use_stencil)
552 GLint value = use_stencil ? GL_STENCIL_INDEX : GL_DEPTH_COMPONENT;
554 glTextureParameteri(tex_id_, GL_DEPTH_STENCIL_TEXTURE_MODE, value);
558 glTexParameteri(target_, GL_DEPTH_STENCIL_TEXTURE_MODE, value);
562void GLTexture::mip_range_set(
int min,
int max)
568 glTextureParameteri(tex_id_, GL_TEXTURE_BASE_LEVEL,
min);
569 glTextureParameteri(tex_id_, GL_TEXTURE_MAX_LEVEL,
max);
573 glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL,
min);
574 glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL,
max);
583 if (framebuffer_mip_ != mip) {
586 framebuffer_mip_ = mip;
593 framebuffer_ =
nullptr;
598 framebuffer_mip_ = mip;
612 switch (extend_mode) {
614 return GL_CLAMP_TO_EDGE;
618 return GL_MIRRORED_REPEAT;
620 return GL_CLAMP_TO_BORDER;
623 return GL_CLAMP_TO_EDGE;
634 glGenSamplers(samplers_state_cache_count_, &samplers_state_cache_[0][0][0]);
636 float max_anisotropy = 1.0f;
638 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_anisotropy);
643 const GLenum extend_t =
to_gl(extend_yz);
647 const GLenum extend_s =
to_gl(extend_x);
649 for (
int filtering_i = 0; filtering_i < GPU_SAMPLER_FILTERING_TYPES_COUNT; filtering_i++) {
655 GL_LINEAR_MIPMAP_LINEAR :
658 GL_NEAREST_MIPMAP_LINEAR :
663 GLuint
sampler = samplers_state_cache_[extend_yz_i][extend_x_i][filtering_i];
664 glSamplerParameteri(
sampler, GL_TEXTURE_WRAP_S, extend_s);
665 glSamplerParameteri(
sampler, GL_TEXTURE_WRAP_T, extend_t);
666 glSamplerParameteri(
sampler, GL_TEXTURE_WRAP_R, extend_t);
667 glSamplerParameteri(
sampler, GL_TEXTURE_MIN_FILTER, min_filter);
668 glSamplerParameteri(
sampler, GL_TEXTURE_MAG_FILTER, mag_filter);
672 (filtering & GPU_SAMPLER_FILTERING_ANISOTROPIC_MASK))
676 GL_TEXTURE_MAX_ANISOTROPY_EXT,
677 min_ff(
float(GPU_anisotropic_samples_get(filtering)), max_anisotropy));
698 glSamplerParameteri(compare_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
699 glSamplerParameteri(compare_sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
700 glSamplerParameteri(compare_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
701 glSamplerParameteri(compare_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
702 glSamplerParameteri(compare_sampler, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
703 glSamplerParameteri(compare_sampler, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
704 glSamplerParameteri(compare_sampler, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
711 glSamplerParameteri(icon_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
712 glSamplerParameteri(icon_sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
719 glDeleteSamplers(samplers_state_cache_count_, &samplers_state_cache_[0][0][0]);
744bool GLTexture::proxy_check(
int mip)
753 int size[3] = {1, 1, 1};
763 if (size[0] > max_3d_size || size[1] > max_3d_size || size[2] > max_3d_size) {
768 if (size[0] > max_size || size[1] > max_size) {
773 if (size[0] > max_size) {
778 if (size[0] > max_cube_size) {
804 switch (dimensions) {
807 glCompressedTexImage1D(gl_proxy, mip, size[0], 0, gl_format, img_size,
nullptr);
810 glCompressedTexImage2D(gl_proxy, mip,
UNPACK2(size), 0, gl_format, img_size,
nullptr);
813 glCompressedTexImage3D(gl_proxy, mip,
UNPACK3(size), 0, gl_format, img_size,
nullptr);
818 switch (dimensions) {
821 glTexImage1D(gl_proxy, mip, internal_format, size[0], 0, gl_format, gl_type,
nullptr);
825 gl_proxy, mip, internal_format,
UNPACK2(size), 0, gl_format, gl_type,
nullptr);
829 gl_proxy, mip, internal_format,
UNPACK3(size), 0, gl_format, gl_type,
nullptr);
835 glGetTexLevelParameteriv(gl_proxy, 0, GL_TEXTURE_WIDTH, &width);
850 if (type.has_value()) {
858 "Feedback loop: Trying to bind a texture (%s) with mip range %d-%d but mip %d is "
859 "attached to the active framebuffer (%s)",
876 glGenBuffers(1, &gl_id_);
886 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_id_);
887 glBufferData(GL_PIXEL_UNPACK_BUFFER, size,
nullptr, GL_DYNAMIC_DRAW);
888 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
896 glDeleteBuffers(1, &gl_id_);
906 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_id_);
907 void *ptr = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
914 glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
915 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
923 return native_handle;
#define SNPRINTF(dst, format,...)
#define GPU_ATTACHMENT_TEXTURE_MIP(_texture, _mip)
#define GPU_SAMPLER_CUSTOM_TYPES_COUNT
#define GPU_SAMPLER_EXTEND_MODES_COUNT
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
constexpr T * data() const
constexpr Span slice(int64_t start, int64_t size) const
constexpr const T * data() const
StateManager * state_manager
static bool stencil_texturing_support
static GLStateManager * state_manager_active_get()
static void texture_free(GLuint tex_id)
static bool direct_state_access_support
static bool texture_filter_anisotropic_support
static GLint max_cubemap_size
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 update_sub(int mip, int offset[3], int extent[3], eGPUDataFormat type, const void *data, const uint unpack_row_length=0) override
friend class GLFrameBuffer
void read(int mip, eGPUDataFormat type, void *data) override
static void samplers_init()
static GLuint get_sampler(const GPUSamplerState &sampler_state)
void clear(const double4 data) override
static void samplers_free()
bool init_internal() override
void copy_to(Texture *dst, IndexRange mip_levels) override
void check_feedback_loop()
void generate_mipmap() 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
Texture * source_texture_
bool is_texture_view() const
size_t read_size_get(int mip, eGPUDataFormat format) const
int dimensions_count() const
GPUAttachmentType attachment_type(int slot) const
Map< FrameBuffer *, GPUAttachmentType > fb_attachments_
void mip_size_get(int mip, int r_size[3]) const
TextureFormat format_get() const
Texture(const char *name)
GPUTextureFormatFlag format_flag_
int size(const AssetLibraryReference *library_reference)
void raise_gl_error(const char *info)
void object_label(GLenum type, GLuint object, const char *name)
static GLenum to_gl(GPUSamplerExtendMode extend_mode)
GLenum to_gl_internal_format(TextureFormat format)
GLenum to_gl_target(GPUTextureType type)
bool is_half_float(TextureFormat format)
eGPUDataFormat to_texture_data_format(TextureFormat tex_format)
GLenum to_gl_data_format(TextureFormat format)
GLenum to_gl_proxy(GPUTextureType type)
bool is_half_integer(TextureFormat format)
size_t to_block_size(TextureFormat data_type)
int to_bytesize(const DataFormat format)
GLenum swizzle_to_gl(const char swizzle)
int to_component_len(TextureFormat format)
constexpr bool validate_data_format(TextureFormat tex_format, eGPUDataFormat data_format)
GLenum to_gl(eGPUDataFormat format)
void float_to_half_make_finite_array(const float *src, uint16_t *dst, size_t length)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
MINLINE float min_ff(float a, float b)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
gpu::FrameBuffer * GPU_framebuffer_active_get()
void GPU_framebuffer_free(gpu::FrameBuffer *fb)
BLI_INLINE float fb(float length, float L)
@ GPU_SAMPLER_EXTEND_MODE_MIRRORED_REPEAT
@ GPU_SAMPLER_EXTEND_MODE_CLAMP_TO_BORDER
@ GPU_SAMPLER_EXTEND_MODE_REPEAT
@ GPU_SAMPLER_EXTEND_MODE_EXTEND
@ GPU_SAMPLER_FILTERING_LINEAR
@ GPU_SAMPLER_FILTERING_MIPMAP
MINLINE int max_ii(int a, int b)
int GPU_max_texture_size()
void * MEM_new_uninitialized_aligned(size_t len, size_t alignment, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(3)
bool GPU_type_matches(GPUDeviceType device, GPUOSType os, GPUDriverType driver)
VecBase< int32_t, 3 > int3
BMesh const char void * data
static const int GPU_SAMPLER_FILTERING_TYPES_COUNT
int GPU_max_texture_layers()
gpu::FrameBuffer * GPU_framebuffer_create(const char *name)
@ GPU_SAMPLER_CUSTOM_ICON
@ GPU_SAMPLER_CUSTOM_COMPARE
@ GPU_SAMPLER_STATE_TYPE_CUSTOM
@ GPU_SAMPLER_STATE_TYPE_INTERNAL
int GPU_max_texture_3d_size()
VecBase< double, 4 > double4
GPUPixelBufferNativeHandle GPU_pixel_buffer_get_native_handle(GPUPixelBuffer *pixel_buf)
GPUSamplerExtendMode extend_yz
GPUSamplerFiltering filtering
GPUSamplerCustomType custom_type
std::string to_string() const
GPUSamplerExtendMode extend_x