Texture stores 2D-texture data. See Introduction To Textures in Direct3D 11.


class Texture
Texture(path[, size=(0, 0, 1, 0), format=0, bind= BIND_SHADER_RESOURCE, usage= USAGE_DEFAULT, cpuaccess=0, miscflags=0]) → Texture
  • path – File to be loaded. If this is None a new empty texture is created (with undefined content).
  • size – A tuple which should hold dimensions for the texture (width, height[, arraysize, mipmapcount]). If the texture is loaded from a file arraysize is ignored. If mipmapcount is 0 a full chain is created.
  • format – One of the Format-values.
  • bind – A combination of Bind-values.
  • usage – One of the Usage-values.
  • cpuaccess – A combination of CPU access-values.
  • miscflags – A combination of Resource misc flags.

Creates a new Texture.

copy(Texture source) → None


This function does not report certain errors unless Direct3D debug runtime is used.

Performs a hardware copy. Both textures must be very similar (size, format and flags) for this to work. If the source texture is multisampled use resolve().

MSDN - CopyResource()

copyRegion(int destx, int desty, int width, int height, int scrx, int srcy, Texture source, int destindex, int srcindex) → None
  • destx – X-coordinate of the destination area.
  • desty – Y-coordinate of the destination area.
  • width – Width of the region to be copied.
  • height – Height of the region to be copied.
  • scrx – X-coordinate of the source area.
  • srcy – Y-coordinate of the source area.
  • source – The source Texture.
  • destindex – Array index for the destination texture. If texture is not an array, use 0.
  • srcindex – Array index for the source texture. If texture is not an array, use 0.


This function does not report certain errors unless Direct3D debug runtime is used.

Performs a hardware copy to a region. Only the first mipmap level will be copied so it is usually neccesarily to use filter() on the destination texture.

MSDN - CopySubresourceRegion()

filter(int filtertype) → None

Filters the first mipmap level to lower levels. If you have modified the texture this is often required to apply changes to other mipmap levels.

getDesc() → TextureDesc<size, format, bind, usage, cpuAccess, miscFlags>

Returns Texture’s description as a named tuple. size is a normal tuple of four elements: (width, height, arraysize, mipmapcount).

getItemFormat() → str

Returns a format string describing one pixel. Compatible with the struct-module. Note that some formats (DXGI_FORMAT_R10G10B10A2_UINT, DXGI_FORMAT_D24_UNORM_S8_UINT etc.) cannot be represented 100% accurately so they are treated as a bunch on bytes.

getItemSize() → int

Returns the size of one pixel in bytes.

map(int maptype[, int arrayindex=0]) → None
  • maptype – One of the Map-flags.
  • arrayindex – Index for texture arrays.

Maps the Texture for CPU-access. Some Texture types can’t be mapped or can be mapped only for reading or writing.

resolve(Texture source) → None


This function does not report certain errors unless Direct3D debug runtime is used.

Resolves (copies) a multisampled source texture into a normal texture.

MSDN - ResolveSubresource()

save(str filepath, int imageformat) → None
  • filepath – Path to the target file.
  • imageformat – One of Image format-values.

Saves the texture to a file. Only IFF_DDS image format is capable of supporting all texture formats. All .dds-files can also include an extra DDS_HEADER_DXT10-header which is a relatively new feature and not supported by all image readers.

size() → int width, int height

Returns the width and height of a texture.

unmap() → None

Unmaps a mapped texture.


Texture supports 2D-slicing to read or write data in byte form. All formats are not supported. Slicing methods do not support negative parameters. All operations use only the first mipmap level.

class d3d11.Texture
Texture[startx:stopx:stepx, starty:stopy:stepy] -> bytearray/list

Slices the given area and returns a 1D-bytearray (Python >= 2.6) or a list (Python < 2.6) in a row-major order (C-style). Because returned values are in a 1D-form as bytes it is often easier to use this data through special adapter classes. d3d11x.Accessor is one example.

Texture[startx:stopx:stepx, starty:stopy:stepy] = sequence -> None

Slice assign.

This example creates a grid pattern by zeroing the red channel:

class Black:
    def __getitem__(self, index):
        return 0

t = Texture(...) #Assume some RGB(A)-compatible format
with Mapper(t, MAP_WRITE):
    t[::3, ::3] = Black()

Next example “brightens” a 75x50-sized area at (x=100, y=200, x2=175, y2=250). This also modifies alpha values which is not usually wanted.

t = Texture(...) #Assume some RGB(A)-compatible format
with Mapper(t, MAP_READ_WRITE):
    t[100:175, 200:250] = [min(int(x * 1.5), 255) for x in t[100:175, 200:250]]

This example retrieves one horizontal line (row) and one vertical line (column).

row = t[::, 55:56]
column = t[23:24, ::]

Buffer protocol


Care should be used when using buffer protocol as it is the responsibility of the programmer to ensure that the memory stays mapped as long as neccesarily and that it was mapped for correct access (read/write). Also the buffer protocol has no concept of a write-only buffer.

Texture supports Python’s buffer protocol. For example it is possible to directly manipulate textures’s mapped memory using memoryview or any other similar object.

Table Of Contents

Get DirectPython 11 at SourceForge.net. Fast, secure and Free Open Source software downloads