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);