OBS-Direct11数据结构
+ -

gs_texture_2d

2024-06-19 16 0

按照上节,gs_texture_2d有两种类型的纹理,分别为2D和2D-CUBE纹理类型。其从数据组织上来讲,一个是一拖一,一个是一拖6。

struct gs_texture_2d : gs_texture {
    ComPtr<ID3D11Texture2D> texture;  //纹理指针
    ComPtr<ID3D11RenderTargetView> renderTarget[6]; //纹理需要一个纹理视图显示,CUBE得有6个
    ComPtr<ID3D11RenderTargetView> renderTargetLinear[6];
    ComPtr<IDXGISurface1> gdiSurface;

    uint32_t width = 0, height = 0;
    uint32_t flags = 0;
    DXGI_FORMAT dxgiFormatResource = DXGI_FORMAT_UNKNOWN;
    DXGI_FORMAT dxgiFormatView = DXGI_FORMAT_UNKNOWN;
    DXGI_FORMAT dxgiFormatViewLinear = DXGI_FORMAT_UNKNOWN;
    bool isRenderTarget = false;
    bool isGDICompatible = false;
    bool isDynamic = false;
    bool isShared = false;
    bool genMipmaps = false;
    uint32_t sharedHandle = GS_INVALID_HANDLE;

    gs_texture_2d *pairedTexture = nullptr;
    bool twoPlane = false;
    bool chroma = false;
    bool acquired = false;

    vector<vector<uint8_t>> data; //纹理数据
    vector<D3D11_SUBRESOURCE_DATA> srd;//纹理数据描述符(创建纹理时用)
    D3D11_TEXTURE2D_DESC td = {}; //纹理描述符(创建纹理时用)

    void InitSRD(vector<D3D11_SUBRESOURCE_DATA> &srd);
    void InitTexture(const uint8_t *const *data);
    void InitResourceView();
    void InitRenderTargets();
    void BackupTexture(const uint8_t *const *data);
    void GetSharedHandle(IDXGIResource *dxgi_res);

    void RebuildSharedTextureFallback();
    void Rebuild(ID3D11Device *dev);
    void RebuildPaired_Y(ID3D11Device *dev);
    void RebuildPaired_UV(ID3D11Device *dev);

    inline void Release()
    {
        texture.Release();
        for (ComPtr<ID3D11RenderTargetView> &rt : renderTarget)
            rt.Release();
        for (ComPtr<ID3D11RenderTargetView> &rt : renderTargetLinear)
            rt.Release();
        gdiSurface.Release();
        shaderRes.Release();
        shaderResLinear.Release();
    }

    inline gs_texture_2d() : gs_texture(GS_TEXTURE_2D, 0, GS_UNKNOWN) {}

    gs_texture_2d(gs_device_t *device, uint32_t width, uint32_t height,
              gs_color_format colorFormat, uint32_t levels,
              const uint8_t *const *data, uint32_t flags,
              gs_texture_type type, bool gdiCompatible,
              bool twoPlane = false);

    gs_texture_2d(gs_device_t *device, ID3D11Texture2D *nv12,uint32_t flags);
    gs_texture_2d(gs_device_t *device, uint32_t handle, bool ntHandle = false);
    gs_texture_2d(gs_device_t *device, ID3D11Texture2D *obj);
};

提供了5个构造函数。第一个比较简单,其余4个才是实质。

#define SHARED_FLAGS (GS_SHARED_TEX | GS_SHARED_KM_TEX)

//例用原始数据构造
gs_texture_2d::gs_texture_2d(gs_device_t *device, uint32_t width,
                 uint32_t height, gs_color_format colorFormat,
                 uint32_t levels, const uint8_t *const *data,
                 uint32_t flags_, gs_texture_type type,
                 bool gdiCompatible, bool twoPlane_)
    : gs_texture(device, gs_type::gs_texture_2d, type, levels, colorFormat),
      width(width),
      height(height),
      flags(flags_),
      dxgiFormatResource(ConvertGSTextureFormatResource(format)),
      dxgiFormatView(ConvertGSTextureFormatView(format)),
      dxgiFormatViewLinear(ConvertGSTextureFormatViewLinear(format)),
      isRenderTarget((flags_ & GS_RENDER_TARGET) != 0),
      isGDICompatible(gdiCompatible),
      isDynamic((flags_ & GS_DYNAMIC) != 0),
      isShared((flags_ & SHARED_FLAGS) != 0),
      genMipmaps((flags_ & GS_BUILD_MIPMAPS) != 0),
      sharedHandle(GS_INVALID_HANDLE),
      twoPlane(twoPlane_)
{
    InitTexture(data);
    InitResourceView();

    if (isRenderTarget)
        InitRenderTargets();
}

//使用NV12纹理指针构造
gs_texture_2d::gs_texture_2d(gs_device_t *device, ID3D11Texture2D *nv12tex,
                 uint32_t flags_)
    : gs_texture(device, gs_type::gs_texture_2d, GS_TEXTURE_2D),
      isRenderTarget((flags_ & GS_RENDER_TARGET) != 0),
      isDynamic((flags_ & GS_DYNAMIC) != 0),
      isShared((flags_ & SHARED_FLAGS) != 0),
      genMipmaps((flags_ & GS_BUILD_MIPMAPS) != 0),
      twoPlane(true),
      texture(nv12tex)
{
    texture->GetDesc(&td);

    const bool p010 = td.Format == DXGI_FORMAT_P010;
    const DXGI_FORMAT dxgi_format = p010 ? DXGI_FORMAT_R16G16_UNORM: DXGI_FORMAT_R8G8_UNORM;

    this->type = GS_TEXTURE_2D;
    this->format = p010 ? GS_RG16 : GS_R8G8;
    this->flags = flags_;
    this->levels = 1;
    this->device = device;
    this->chroma = true;
    this->width = td.Width / 2;
    this->height = td.Height / 2;
    this->dxgiFormatResource = dxgi_format;
    this->dxgiFormatView = dxgi_format;
    this->dxgiFormatViewLinear = dxgi_format;

    InitResourceView();
    if (isRenderTarget)
        InitRenderTargets();
}

//使用纹理指针构造
gs_texture_2d::gs_texture_2d(gs_device_t *device, ID3D11Texture2D *obj)
    : gs_texture(device, gs_type::gs_texture_2d, GS_TEXTURE_2D)
{
    texture = obj;

    texture->GetDesc(&td);

    const gs_color_format format = ConvertDXGITextureFormat(td.Format);

    this->type = GS_TEXTURE_2D;
    this->format = format;
    this->levels = 1;
    this->device = device;

    this->width = td.Width;
    this->height = td.Height;
    this->dxgiFormatResource = ConvertGSTextureFormatResource(format);
    this->dxgiFormatView = ConvertGSTextureFormatView(format);
    this->dxgiFormatViewLinear = ConvertGSTextureFormatViewLinear(format);

    InitResourceView();
}

//太高级了,不知道
gs_texture_2d::gs_texture_2d(gs_device_t *device, uint32_t handle,
                 bool ntHandle)
    : gs_texture(device, gs_type::gs_texture_2d, GS_TEXTURE_2D),
      isShared(true),
      sharedHandle(handle)
{
    HRESULT hr;
    if (ntHandle) {
        ComQIPtr<ID3D11Device1> dev = device->device;
        hr = dev->OpenSharedResource1((HANDLE)(uintptr_t)handle,
                          __uuidof(ID3D11Texture2D),
                          (void **)texture.Assign());
    } else {
        hr = device->device->OpenSharedResource(
            (HANDLE)(uintptr_t)handle, __uuidof(ID3D11Texture2D),
            (void **)texture.Assign());
    }

    if (FAILED(hr))
        throw HRError("Failed to open shared 2D texture", hr);

    texture->GetDesc(&td);

    const gs_color_format format = ConvertDXGITextureFormat(td.Format);

    this->type = GS_TEXTURE_2D;
    this->format = format;
    this->levels = 1;
    this->device = device;

    this->width = td.Width;
    this->height = td.Height;
    this->dxgiFormatResource = ConvertGSTextureFormatResource(format);
    this->dxgiFormatView = ConvertGSTextureFormatView(format);
    this->dxgiFormatViewLinear = ConvertGSTextureFormatViewLinear(format);

    InitResourceView();
}

纵观这4个构造函数,除过最后一个不认识外。其余三分又分为两种。第一种是由原始数据构造纹理,然后再根据纹理构造渲染视图。第二种是由纹理构造,但又分为由NV12纹理构造和正常的纹理构造。

0 篇笔记 写笔记

11.纹理
本文实现从文件加载纹理并将其映射到几何图形!源码下载地址:https://www.braynzarsoft.net/file/11DX11_Lesson_11_Textures_zip.zip介绍在这里,我们将学习如何将纹理映射到我们的对象。在 Direct3D 中,我们使用 2D (u,v......
2.2.2纹理资源
正如我们在最后几节中所看到的,有无数不同的缓冲区资源,以及为各种用途配置它们的许多不同方法。然而,缓冲区只是资源故事的一半。纹理为基于GPU作为渲染处理器的资源提供了不同的概念。“纹理”一词指的是一种在性质上与图像或类似图像的内存资源。这是一个非常松散的定义,因为有一系列不同的纹理类型,具有不同的维......
2D纹理
我们将探索的第二种纹理类型是2D纹理。由于这种纹理类型与标准2D图像布局密切相关,因此它通常是使用最广泛的纹理类型之一。此资源被组织为二维元素网格,其中每个元素都是DXGI_FORMAT枚举的成员。2D纹理的各种子资源形式如图2.31所示。如图2.31所示,这些纹理以与ID纹理类似的方式支持mip贴......
gs_texture纹理
gs_texture结构体是gs_texture_2d和gs_texture_3d的基类。struct gs_texture : gs_obj { gs_texture_type type; //纹理类型:2d,2d(cube),3d uint32_t levels; //纹理级......
gs_texture_2d
按照上节,gs_texture_2d有两种类型的纹理,分别为2D和2D-CUBE纹理类型。其从数据组织上来讲,一个是一拖一,一个是一拖6。struct gs_texture_2d : gs_texture { ComPtr texture; ......
gs_texture_2d成员函数
2d纹理可创建的类型是多样的。有是2d的,也有的是2d-cube的。有的是需要绑定的是渲染目标视图,有的是渲染着色器资源视图。其它的一些配置参数多样。 InitTexture(data);//根据纹理数据创建纹理 InitResourceView();//创建着色器资源视图 ......
6.4.2 HLSL纹理缓冲区
由于常量缓冲区针对小尺寸和统一的访问模式进行了优化,因此在某些情况下,它们可能具有未知期望的性能特性。一种常见的情况是用于蒙皮的骨骼矩阵阵列。在这种情况下,每个顶点都包含一个或多个索引,指示阵列中的哪个骨骼应用于变换位置和法线。在许多硬件类型上,这种访问模式将导致执行着色器程序的线程序列化对骨骼数据......
利用d3d11将两个rgb数据格式的纹理图像合并到一个新的2D纹理
要利用D3D11将两个RGB数据格式的纹理图像合并到一个新的2D纹理中,你可以按照以下步骤进行操作:首先,创建两个源纹理和一个目标纹理。可以使用D3D11_TEXTURE2D_DESC结构来指定纹理的参数,例如宽度、高度、格式等。对于RGB数据格式,常见的格式包括DXGI_FORMAT_R8G8......
作者信息
站长漫谈
取消
感谢您的支持,我会继续努力的!
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

您的支持,是我们前进的动力!