OBS-D3D11渲染梳理
+ -

D3D11顶点缓冲区创建

2024-07-30 32 0

顶点缓冲区使用结构体gs_vertex_buffer来表示。

结构体

struct gs_tvertarray {
    size_t width; //=2其实代表的是2个坐标xy
    void *array;
};
struct vec3 {
    union {
        struct {
            float x, y, z, w;
        };
        float ptr[4];
        __m128 m;
    };
};

struct gs_vb_data {
    size_t num;
    struct vec3 *points;
    struct vec3 *normals;
    struct vec3 *tangents;
    uint32_t *colors;

    size_t num_tex;
    struct gs_tvertarray *tvarray;
};

立即顶点缓冲区

立顶点数量为512

#define IMMEDIATE_COUNT 512

    vbd = gs_vbdata_create();
    vbd->num = IMMEDIATE_COUNT;
    vbd->points = bmalloc(sizeof(struct vec3) * IMMEDIATE_COUNT);
    vbd->normals = bmalloc(sizeof(struct vec3) * IMMEDIATE_COUNT);
    vbd->colors = bmalloc(sizeof(uint32_t) * IMMEDIATE_COUNT);
    vbd->num_tex = 1;
    vbd->tvarray = bmalloc(sizeof(struct gs_tvertarray));
    vbd->tvarray[0].width = 2;
    vbd->tvarray[0].array = bmalloc(sizeof(struct vec2) * IMMEDIATE_COUNT);

    graphics->immediate_vertbuffer = graphics->exports.device_vertexbuffer_create(graphics->device, vbd, GS_DYNAMIC);

sprite缓冲区

  • graphics_init_sprite_vb
    精灵顶点缓冲区就是各个插件如色源,图片,视频MP4等绘制需要的顶点缓冲区。

顶点缓冲区是4个坐标,每个坐标xyz确定其位置。
项点缓冲区也包括了2D纹理的坐标UV.每个顶点一个UVC坐标,一一对应。

    struct gs_vb_data *vbd;
    vbd = gs_vbdata_create();
    vbd->num = 4;
    vbd->points = bmalloc(sizeof(struct vec3) * 4);
    vbd->num_tex = 1;
    vbd->tvarray = bmalloc(sizeof(struct gs_tvertarray));
    vbd->tvarray[0].width = 2;
    vbd->tvarray[0].array = bmalloc(sizeof(struct vec2) * 4);

    memset(vbd->points, 0, sizeof(struct vec3) * 4);
    memset(vbd->tvarray[0].array, 0, sizeof(struct vec2) * 4);

    graphics->sprite_buffer = graphics->exports.device_vertexbuffer_create(    graphics->device, vbd, GS_DYNAMIC);

通用创建函数device_vertexbuffer_create

    D3D11_BUFFER_DESC bd;
    D3D11_SUBRESOURCE_DATA srd;
    HRESULT hr;

    memset(&bd, 0, sizeof(bd));
    memset(&srd, 0, sizeof(srd));

    size_t elementSize = sizeof(vec3);
    size_t numVerts = vbd.data->num;
    void *array = vbd.data->points;
    uint32_t flags = GS_DYNAMIC;
    bool dynamic = (flags & GS_DYNAMIC) != 0;

    bd.Usage = dynamic ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_DEFAULT;//GS_DYNAMIC
    bd.CPUAccessFlags = dynamic ? D3D11_CPU_ACCESS_WRITE : 0;
    bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    bd.ByteWidth = UINT(elementSize * numVerts);
    srd.pSysMem = array;

    ID3D11Buffer *buffer;
    hr = device->device->CreateBuffer(&bd, &srd, &buffer);
    if (FAILED(hr))
        throw HRError("Failed to create buffer", hr);

0 篇笔记 写笔记

作者信息
我爱内核
Windows驱动开发,网站开发
好好学习,天天向上。
取消
感谢您的支持,我会继续努力的!
扫码支持
扫码打赏,你说多少就多少

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

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