gs_texture_2d
2024-06-19
133
0
纹理(Texture) 是计算机图形学中用于存储图像数据的资源,主要用途包括:
- 为 3D 模型表面提供细节(如颜色、法线、高光等)
- 作为 2D 渲染的目标(如离屏渲染、后处理)
- 存储视频帧数据(如 NV12 格式用于视频编码)
在 OBS Studio 中, gs_texture_2d 类封装了 DirectX 11 的 ID3D11Texture2D 接口,提供了统一的纹理管理功能。
纹理的核心属性
从代码中可以看到,纹理具有以下核心属性:
- 尺寸(Width/Height) :纹理的宽高,如 1920x1080
- 格式(Format) :存储像素数据的格式,如 RGBA、BGRA、NV12 等
- 类型(Type) :如 2D 纹理、立方体纹理
- Mip 级别(Mip Levels) :多级渐远纹理的数量,用于不同距离的渲染优化
- 标志(Flags) :控制纹理的行为,如是否为渲染目标、是否可共享等
二、资源视图的概念与类型
什么是资源视图?
资源视图(Resource View) 是 DirectX 中用于访问纹理资源特定部分或特定格式的接口。由于纹理可能有多种用途(如同时作为渲染目标和着色器输入),需要通过不同的视图来访问。
SRC(着色器资源视图)是着色器和纹理资源之间的”桥梁”,它定义了着色器如何读取纹理数据的方式
纹理 -> 着色器资源视图 -> 着色器使用
| 视图类型 | 必需标志 | 用途 | 操作向 | 创建函数 | DirectX 接口 |
|---|---|---|---|---|---|
| SRV | 无(默认创建) | 着色器读取 | GPU读 | InitResourceView() | ID3D11ShaderResourceView |
| RTV | GS_RENDER_TARGET | 着色器写入 | GPU写 | InitRenderTargets() | ID3D11RenderTargetView |
| DSV | GS_ZSTENCIL_BUFFER | 深度测试 | GPU读写 | 由 gs_zstencil_buffer 创建 I | D3D11DepthStencilVie |
1. 着色器资源视图(Shader Resource View, SRV)
在着色器(顶点着色器、像素着色器等)中读取纹理数据.
创建条件 :所有纹理默认创建 SRV(无需特殊标志)
使用场景 :
- 在着色器中作为采样器输入
- 用于纹理数据的读取和处理
deviceContext->PSSetShaderResources(slot, 1, &shaderRes);1:是shaderRes数量
shaderRes:PS的着色器资源视图数量,可以是2D纹理对应资源视图
2. 渲染目标视图(Render Target View, RTV)
将渲染结果写入纹理(作为离屏渲染目标)
deviceContext->OMSetRenderTargets(1, &renderTarget, depthStencilView);
3. 深度模板视图(Depth Stencil View, DSV)
用于深度测试和模板操作,对应 gs_zstencil_buffer 类
4. 随机访问视图(Unordered Access View, UAV)
用于着色器随机读写纹理,支持并发访问
OBS-2D纹理文件总结
- 原始数据
- 2D纹理(1.通过数据构造,2.根据NV12纹理构造,3根据共享句柄构造,4根据纹理构造)
- GDI兼容Surface-gdiSurface
- 共享句柄
- 资源视图
- 着色器资源视图(不需要伽马校正的场景)
- shaderResLinear(需要伽马校正)
- RTV 渲染目标视图
- 着色器资源视图(不需要伽马校正的场景)
OBS创建纹理
普通纹理创建
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 = new gs_texture_2d(device, width, height, color_format,
levels, data, flags, GS_TEXTURE_2D,
false);
return texture;
}
创建GID兼容
gs_texture_t *device_texture_create_gdi(gs_device_t *device, uint32_t width, uint32_t height)
{
gs_texture* texture = new gs_texture_2d(device, width, height,
GS_BGRA_UNORM, 1, nullptr,
GS_RENDER_TARGET, GS_TEXTURE_2D,
true);
return texture;
}
//共享句柄
extern "C" EXPORT gs_texture_t *device_texture_open_shared(gs_device_t *device,
uint32_t handle)
{
gs_texture *texture = texture = new gs_texture_2d(device, handle);;
return texture;
}
//NV12纹理创建
bool device_texture_create_nv12(gs_device_t *device, gs_texture_t **p_tex_y,
gs_texture_t **p_tex_uv, uint32_t width,
uint32_t height, uint32_t flags)
{
gs_texture_2d *tex_y;
gs_texture_2d *tex_uv;
tex_y = new gs_texture_2d(device, width, height, GS_R8, 1,
nullptr, flags, GS_TEXTURE_2D, false, true);
tex_uv = new gs_texture_2d(device, tex_y->texture, flags);
}
OBS-Direct11数据结构





