46#ifndef GPU_NO_USE_PY_REFERENCES
58 int mip_len_max = 1 +
floorf(log2f(
w));
92 int mip_len_max = 1 +
floorf(log2f(std::max({
w, h, d})));
109 int mip_len_max = 1 +
floorf(log2f(
w));
148 int view_extent[3]{0, 0, 0};
194 if (current_type != type) {
195 fb->attachment_remove(current_type);
203 if (type.has_value()) {
204 fb->attachment_remove(*type);
207 BLI_assert_msg(0,
"GPU: Error: Texture: Framebuffer is not attached");
214 int extent[3] = {1, 1, 1};
215 int offset[3] = {0, 0, 0};
253 tex->usage_set(usage);
255 bool success =
false;
259 success = tex->init_1D(
w, h, mip_len, tex_format);
263 success = tex->init_2D(
w, h, d, mip_len, tex_format);
266 success = tex->init_3D(
w, h, d, mip_len, tex_format);
270 success = tex->init_cubemap(
w, d, mip_len, tex_format);
281 tex->update(data_format, pixels);
376 tex->usage_set(usage);
377 bool success = tex->init_2D(width, height, 0, mip_len, tex_format);
385 for (
int mip = 0; mip < mip_len; mip++) {
386 int extent[3] = {1, 1, 1};
387 int offset[3] = {0, 0, 0};
388 tex->mip_size_get(mip, extent);
390 size_t size = ((extent[0] + 3) / 4) * ((extent[1] + 3) / 4) *
to_block_size(tex_format);
395 static_cast<uchar *
>(
const_cast<void *
>(
data)) + ofs);
409 "Vertex Buffers used for textures should have usage flag "
410 "GPU_USAGE_FLAG_BUFFER_TEXTURE_ONLY.");
415 bool success = tex->init_buffer(vert, tex_format);
425 const float pixel[4] = {1.0f, 0.0f, 1.0f, 1.0f};
427 int h = (dimension < 2 && !is_array) ? 0 : 1;
428 int d = (dimension < 3 && !is_array) ? 0 : 1;
440 TextureFormat::UNORM_8_8_8_8,
459 "Source texture of TextureView must have GPU_TEXTURE_USAGE_FORMAT_VIEW usage "
460 "flag if view texture uses stencil texturing.");
463 "Source texture of TextureView must have GPU_TEXTURE_USAGE_FORMAT_VIEW usage "
464 "flag if view texture format is different.");
466 view->init_view(source_texture,
500 uint unpack_row_length)
502 int extent[3] = {1, 1, 1}, offset[3] = {0, 0, 0};
503 texture->mip_size_get(mip_level, extent);
504 texture->update_sub(mip_level, offset, extent, data_format, pixels, unpack_row_length);
516 uint unpack_row_length)
518 int offset[3] = {offset_x, offset_y, offset_z};
519 int extent[3] = {width, height, depth};
520 tex->
update_sub(0, offset, extent, data_format, pixels, unpack_row_length);
525 GPUPixelBuffer *pixel_buf,
533 int offset[3] = {offset_x, offset_y, offset_z};
534 int extent[3] = {width, height, depth};
535 texture->update_sub(offset, extent, data_format, pixel_buf);
541 size_t size =
texture->read_size_get(mip_level, data_format);
553 "The host-read usage flag must be specified up-front. Only textures which require data "
554 "reads should be flagged, allowing the backend to make certain optimizations.");
555 texture->read(mip_level, data_format, dst);
563 int extent[3] = {1, 1, 1};
566 size_t sample_len = extent[0] * std::max(1, extent[1]) * std::max(1, extent[2]);
568 size_t size = sample_len * sample_size;
582 return texture->read_size_get(mip_level, data_format);
591 double4 clear_data(0xFFFFFFFFu);
593 switch (data_format) {
596 clear_data[i] =
static_cast<const float *
>(
data)[i];
601 clear_data[i] =
static_cast<const int *
>(
data)[i];
606 clear_data[i] =
static_cast<const uint *
>(
data)[i];
613 tex->
clear(clear_data);
702 int samples = use_aniso ?
U.anisotropic_filter : 1;
708 tex->sampler_state.enable_filtering_flag(filtering);
713 texture->sampler_state.extend_x = extend_mode;
718 texture->sampler_state.extend_yz = extend_mode;
723 texture->sampler_state.extend_x = extend_mode;
724 texture->sampler_state.extend_yz = extend_mode;
737 if (tex->refcount < 0) {
738 fprintf(stderr,
"gpu::Texture: negative refcount\n");
741 if (tex->refcount == 0) {
818 switch (texture_format) {
820 case TextureFormat::UINT_8_8_8_8:
822 case TextureFormat::SINT_8_8_8_8:
824 case TextureFormat::UNORM_8_8_8_8:
826 case TextureFormat::UINT_32_32_32_32:
828 case TextureFormat::SINT_32_32_32_32:
830 case TextureFormat::SFLOAT_32_32_32_32:
832 case TextureFormat::UINT_16_16_16_16:
834 case TextureFormat::SINT_16_16_16_16:
836 case TextureFormat::SFLOAT_16_16_16_16:
838 case TextureFormat::UNORM_16_16_16_16:
840 case TextureFormat::UINT_8_8:
842 case TextureFormat::SINT_8_8:
844 case TextureFormat::UNORM_8_8:
846 case TextureFormat::UINT_32_32:
848 case TextureFormat::SINT_32_32:
850 case TextureFormat::SFLOAT_32_32:
852 case TextureFormat::UINT_16_16:
854 case TextureFormat::SINT_16_16:
856 case TextureFormat::SFLOAT_16_16:
858 case TextureFormat::UNORM_16_16:
860 case TextureFormat::UINT_8:
862 case TextureFormat::SINT_8:
864 case TextureFormat::UNORM_8:
866 case TextureFormat::UINT_32:
868 case TextureFormat::SINT_32:
870 case TextureFormat::SFLOAT_32:
872 case TextureFormat::UINT_16:
874 case TextureFormat::SINT_16:
876 case TextureFormat::SFLOAT_16:
878 case TextureFormat::UNORM_16:
881 case TextureFormat::UNORM_10_10_10_2:
883 case TextureFormat::UINT_10_10_10_2:
885 case TextureFormat::UFLOAT_11_11_10:
886 return "R11F_G11F_B10F";
887 case TextureFormat::SFLOAT_32_DEPTH_UINT_8:
888 return "DEPTH32F_STENCIL8";
889 case TextureFormat::SRGBA_8_8_8_8:
892 case TextureFormat::SFLOAT_16_16_16:
894 case TextureFormat::SNORM_16_16_16:
895 return "RGB16_SNORM";
896 case TextureFormat::SINT_16_16_16:
898 case TextureFormat::UINT_16_16_16:
900 case TextureFormat::UNORM_16_16_16:
902 case TextureFormat::SNORM_16_16_16_16:
903 return "RGBA16_SNORM";
904 case TextureFormat::SNORM_8_8_8_8:
905 return "RGBA8_SNORM";
906 case TextureFormat::SFLOAT_32_32_32:
908 case TextureFormat::SINT_32_32_32:
910 case TextureFormat::UINT_32_32_32:
912 case TextureFormat::SNORM_8_8_8:
914 case TextureFormat::UNORM_8_8_8:
916 case TextureFormat::SINT_8_8_8:
918 case TextureFormat::UINT_8_8_8:
920 case TextureFormat::SNORM_16_16:
922 case TextureFormat::SNORM_8_8:
924 case TextureFormat::SNORM_16:
926 case TextureFormat::SNORM_8:
929 case TextureFormat::SRGB_DXT1:
930 return "SRGB8_A8_DXT1";
931 case TextureFormat::SRGB_DXT3:
932 return "SRGB8_A8_DXT3";
933 case TextureFormat::SRGB_DXT5:
934 return "SRGB8_A8_DXT5";
935 case TextureFormat::SNORM_DXT1:
937 case TextureFormat::SNORM_DXT3:
939 case TextureFormat::SNORM_DXT5:
941 case TextureFormat::SRGBA_8_8_8:
943 case TextureFormat::UFLOAT_9_9_9_EXP_5:
946 case TextureFormat::SFLOAT_32_DEPTH:
947 return "DEPTH_COMPONENT32F";
948 case TextureFormat::UNORM_16_DEPTH:
949 return "DEPTH_COMPONENT16";
1001 return texture->is_texture_view();
1009#ifndef GPU_NO_USE_PY_REFERENCES
1024 texture->mip_size_get(mip_level, r_size);
1045 return wrap(pixbuf);
1056 return unwrap(pixel_buf)->map();
1061 unwrap(pixel_buf)->unmap();
1066 return unwrap(pixel_buf)->get_size();
1071 return unwrap(pixel_buf)->get_native_handle();
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
constexpr IndexRange()=default
int3 size(int miplvl=0) const
StateManager * state_manager
void attachment_remove(GPUAttachmentType type)
virtual PixelBuffer * pixelbuf_alloc(size_t size)=0
virtual Texture * texture_alloc(const char *name)=0
static GPUBackend * get()
virtual void texture_unbind_all()=0
virtual void texture_unbind(Texture *tex)=0
virtual void image_unbind_all()=0
virtual void image_unbind(Texture *tex)=0
virtual void image_bind(Texture *tex, int unit)=0
virtual void texture_bind(Texture *tex, GPUSamplerState sampler, int unit)=0
GPUSamplerState sampler_state
void attach_to(FrameBuffer *fb, GPUAttachmentType type)
Texture * source_texture_
bool init_1D(int w, int layers, int mip_len, TextureFormat format)
GPUTextureType type_get() const
void update(eGPUDataFormat format, const void *data)
eGPUTextureUsage gpu_image_usage_flags_
size_t read_size_get(int mip, eGPUDataFormat format) const
bool init_cubemap(int w, int layers, int mip_len, TextureFormat format)
bool init_view(Texture *src, TextureFormat format, GPUTextureType type, int mip_start, int mip_len, int layer_start, int layer_len, bool cube_as_array, bool use_stencil)
bool init_2D(int w, int h, int layers, int mip_len, TextureFormat format)
int dimensions_count() const
virtual void clear(const double4 data)=0
bool init_3D(int w, int h, int d, int mip_len, TextureFormat format)
bool init_buffer(VertBuf *vbo, TextureFormat format)
Map< FrameBuffer *, GPUAttachmentType > fb_attachments_
virtual void update_sub(int mip, int offset[3], int extent[3], eGPUDataFormat format, const void *data, uint unpack_row_length=0)=0
virtual bool init_internal()=0
void mip_size_get(int mip, int r_size[3]) const
TextureFormat format_get() const
void usage_set(eGPUTextureUsage usage_flags)
Texture(const char *name)
void detach_from(FrameBuffer *fb)
GPUTextureFormatFlag format_flag_
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
eGPUDataFormat to_texture_data_format(TextureFormat tex_format)
@ GPU_FORMAT_NORMALIZED_INTEGER
@ GPU_FORMAT_DEPTH_STENCIL
BLI_INLINE MTLTexture * unwrap(Texture *tex)
size_t to_block_size(TextureFormat data_type)
int to_bytesize(const DataFormat format)
int to_component_len(TextureFormat format)
constexpr bool validate_data_format(TextureFormat tex_format, eGPUDataFormat data_format)
BLI_INLINE Texture * wrap(MTLTexture *texture)
GPUTextureFormatFlag to_format_flag(TextureFormat format)
constexpr TextureFormat to_texture_format(TextureTargetFormat format)
void GPU_texture_compare_mode(gpu::Texture *texture, bool use_compare)
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
size_t GPU_texture_read_size_get(const gpu::Texture *texture, eGPUDataFormat data_format, int mip_level)
gpu::Texture * GPU_texture_create_cube_array(const char *name, int width, int layer_len, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
bool GPU_texture_has_depth_format(const gpu::Texture *texture)
gpu::Texture * GPU_texture_create_1d(const char *name, int width, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
int GPU_texture_depth(const gpu::Texture *texture)
bool GPU_texture_has_integer_format(const gpu::Texture *texture)
void GPU_texture_update(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
gpu::TextureFormat GPU_texture_format(const gpu::Texture *texture)
gpu::Texture * GPU_texture_create_compressed_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const void *data)
static gpu::Texture * gpu_texture_create(const char *name, const int w, const int h, const int d, const GPUTextureType type, int mip_len, TextureFormat tex_format, eGPUTextureUsage usage, const void *pixels, eGPUDataFormat data_format=GPU_DATA_FLOAT)
bool GPU_texture_is_view(const gpu::Texture *texture)
eGPUTextureUsage GPU_texture_usage(const gpu::Texture *texture)
BLI_INLINE float fb(float length, float L)
static constexpr GPUSamplerFiltering GPU_SAMPLER_FILTERING_ANISOTROPIC_MASK
void GPU_texture_image_unbind(gpu::Texture *texture)
void GPU_texture_copy(gpu::Texture *dst, gpu::Texture *src)
void GPU_texture_unbind_all()
bool GPU_texture_has_signed_format(const gpu::Texture *texture)
gpu::Texture * GPU_texture_create_2d_array(const char *name, int width, int height, int layer_len, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
@ GPU_SAMPLER_FILTERING_LINEAR
@ GPU_SAMPLER_FILTERING_MIPMAP
MINLINE int max_ii(int a, int b)
void GPU_pixel_buffer_unmap(GPUPixelBuffer *pixel_buf)
void * GPU_pixel_buffer_map(GPUPixelBuffer *pixel_buf)
const GPUVertFormat * GPU_vertbuf_get_format(const gpu::VertBuf *verts)
void GPU_texture_anisotropic_filter(gpu::Texture *texture, bool use_aniso)
void ** GPU_texture_py_reference_get(gpu::Texture *texture)
bool GPU_texture_has_normalized_format(const gpu::Texture *texture)
int GPU_texture_dimensions(const gpu::Texture *texture)
void GPU_texture_update_sub(gpu::Texture *texture, eGPUDataFormat data_format, const void *pixels, int offset_x, int offset_y, int offset_z, int width, int height, int depth, uint unpack_row_length=0)
void GPU_texture_mipmap_mode(gpu::Texture *texture, bool use_mipmap, bool use_filter)
static GPUSamplerFiltering GPU_anisotropic_filtering_flags(int anisotropic_samples)
MINLINE uint64_t ceil_to_multiple_ul(uint64_t a, uint64_t b)
size_t GPU_texture_dataformat_size(eGPUDataFormat data_format)
@ GPU_TEXTURE_USAGE_FORMAT_VIEW
@ GPU_TEXTURE_USAGE_GENERAL
@ GPU_TEXTURE_USAGE_HOST_READ
int GPU_texture_mip_count(const gpu::Texture *texture)
gpu::Texture * GPU_texture_create_from_vertbuf(const char *name, gpu::VertBuf *vertex_buf)
BMesh const char void * data
void GPU_texture_swizzle_set(gpu::Texture *texture, const char swizzle[4])
void GPU_texture_filter_mode(gpu::Texture *texture, bool use_filter)
bool GPU_texture_has_float_format(const gpu::Texture *texture)
gpu::Texture * GPU_texture_create_3d(const char *name, int width, int height, int depth, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const void *data)
void GPU_texture_original_size_set(gpu::Texture *texture, int width, int height)
int GPU_texture_width(const gpu::Texture *texture)
bool GPU_texture_is_cube(const gpu::Texture *texture)
void GPU_texture_update_mipmap(gpu::Texture *texture, int mip_level, eGPUDataFormat data_format, const void *pixels, uint unpack_row_length=0)
size_t GPU_texture_component_len(gpu::TextureFormat format)
void GPU_texture_bind_ex(gpu::Texture *texture, GPUSamplerState state, int unit)
bool GPU_texture_is_array(const gpu::Texture *texture)
void GPU_texture_extend_mode_x(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
void GPU_texture_extend_mode(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
gpu::Texture * GPU_texture_create_cube(const char *name, int width, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
bool GPU_texture_has_compressed_format(const gpu::Texture *texture)
void GPU_pixel_buffer_free(GPUPixelBuffer *pixel_buf)
@ GPU_SAMPLER_CUSTOM_COMPARE
size_t GPU_pixel_buffer_size(GPUPixelBuffer *pixel_buf)
void GPU_texture_extend_mode_y(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
void GPU_texture_clear(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
void GPU_texture_bind(gpu::Texture *texture, int unit)
int GPU_texture_height(const gpu::Texture *texture)
void GPU_texture_free(gpu::Texture *texture)
@ GPU_SAMPLER_STATE_TYPE_PARAMETERS
@ GPU_SAMPLER_STATE_TYPE_CUSTOM
@ GPU_SAMPLER_STATE_TYPE_INTERNAL
GPUPixelBuffer * GPU_pixel_buffer_create(size_t byte_size)
const char * GPU_texture_format_name(gpu::TextureFormat format)
void GPU_texture_image_unbind_all()
void GPU_texture_image_bind(gpu::Texture *texture, int unit)
uint GPU_vertbuf_get_vertex_len(const gpu::VertBuf *verts)
gpu::Texture * GPU_texture_create_error(int dimension, bool array)
void GPU_texture_unbind(gpu::Texture *texture)
void * GPU_texture_read(gpu::Texture *texture, eGPUDataFormat data_format, int mip_level)
int GPU_texture_layer_count(const gpu::Texture *texture)
int GPU_texture_original_height(const gpu::Texture *texture)
gpu::Texture * GPU_texture_create_view(const char *name, gpu::Texture *source_texture, gpu::TextureFormat view_format, int mip_start, int mip_len, int layer_start, int layer_len, bool cube_as_array, bool use_stencil)
MINLINE int min_ii(int a, int b)
void GPU_texture_py_reference_set(gpu::Texture *texture, void **py_ref)
int GPU_texture_original_width(const gpu::Texture *texture)
bool GPU_texture_has_stencil_format(const gpu::Texture *texture)
@ GPU_USAGE_FLAG_BUFFER_TEXTURE_ONLY
unsigned int GPU_texture_memory_usage_get()
VecBase< double, 4 > double4
void GPU_texture_update_sub_from_pixel_buffer(gpu::Texture *texture, eGPUDataFormat data_format, GPUPixelBuffer *pixel_buf, int offset_x, int offset_y, int offset_z, int width, int height, int depth)
gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
void GPU_texture_get_mipmap_size(gpu::Texture *texture, int mip_level, int *r_size)
gpu::Texture * GPU_texture_create_1d_array(const char *name, int width, int layer_len, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
void GPU_texture_ref(gpu::Texture *texture)
GPUPixelBufferNativeHandle GPU_pixel_buffer_get_native_handle(GPUPixelBuffer *pixel_buf)
GPUSamplerFiltering filtering