纹理创建
2024-07-01
9
0
接上一节,纹理创建使用的函数是gs_texture_create。
//必须的2的整数倍,并且大于1
static inline bool is_pow2(uint32_t size)
{
return size >= 2 && (size & (size - 1)) == 0;
}
gs_texture_t *gs_texture_create(uint32_t width, uint32_t height,
enum gs_color_format color_format,
uint32_t levels, const uint8_t **data,
uint32_t flags)
{
graphics_t *graphics = thread_graphics;
bool pow2tex = is_pow2(width) && is_pow2(height);
bool uses_mipmaps = (flags & GS_BUILD_MIPMAPS || levels != 1);
if (!gs_valid("gs_texture_create"))
return NULL;
if (uses_mipmaps && !pow2tex)
{
uses_mipmaps = false;
flags &= ~GS_BUILD_MIPMAPS;
levels = 1;
}
if (uses_mipmaps && flags & GS_RENDER_TARGET)
{
blog(LOG_WARNING, "Cannot use mipmaps with render targets. "
"Disabling mipmaps for this texture.");
flags &= ~GS_BUILD_MIPMAPS;
levels = 1;
}
return graphics->exports.device_texture_create(graphics->device, width, height,
color_format, levels,
data, flags);
}
device_texture_create对应的其实是D3D11的device_texture_create函数。该函数就是创建一个2D纹理。
gs_texture_t *device_texture_create(gs_device_t *device, uint32_t width,
uint32_t height,
enum gs_color_format color_format,
uint32_t levels, const uint8_t **data,
uint32_t flags)
{
gs_texture *texture = NULL;
try {
texture = new gs_texture_2d(device, width, height, color_format,
levels, data, flags, GS_TEXTURE_2D,
false);
} catch (const HRError &error) {
blog(LOG_ERROR, "device_texture_create (D3D11): %s (%08lX)",
error.str, error.hr);
LogD3D11ErrorDetails(error, device);
} catch (const char *error) {
blog(LOG_ERROR, "device_texture_create (D3D11): %s", error);
}
return texture;
}
2d纹理,使用结构体gs_texture_2d。其基类为gs_texture。更多细节可见:https://www.vaczh.com/article/detail-93.html