yolobs-studio/libobs-d3d11/d3d11-vertexbuffer.cpp

147 lines
4.4 KiB
C++
Raw Permalink Normal View History

2016-02-23 23:16:51 +00:00
/******************************************************************************
Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include <util/base.h>
#include <graphics/vec3.h>
#include "d3d11-subsystem.hpp"
2019-09-22 21:19:10 +00:00
static inline void PushBuffer(vector<ID3D11Buffer *> &buffers,
vector<uint32_t> &strides, ID3D11Buffer *buffer,
size_t elementSize, const char *name)
2016-02-23 23:16:51 +00:00
{
if (buffer) {
buffers.push_back(buffer);
strides.push_back((uint32_t)elementSize);
} else {
blog(LOG_ERROR, "This vertex shader requires a %s buffer",
2019-09-22 21:19:10 +00:00
name);
2016-02-23 23:16:51 +00:00
}
}
void gs_vertex_buffer::FlushBuffer(ID3D11Buffer *buffer, void *array,
2019-09-22 21:19:10 +00:00
size_t elementSize)
2016-02-23 23:16:51 +00:00
{
D3D11_MAPPED_SUBRESOURCE msr;
HRESULT hr;
2019-09-22 21:19:10 +00:00
if (FAILED(hr = device->context->Map(buffer, 0, D3D11_MAP_WRITE_DISCARD,
0, &msr)))
2016-02-23 23:16:51 +00:00
throw HRError("Failed to map buffer", hr);
memcpy(msr.pData, array, elementSize * vbd.data->num);
device->context->Unmap(buffer, 0);
}
void gs_vertex_buffer::MakeBufferList(gs_vertex_shader *shader,
2019-09-22 21:19:10 +00:00
vector<ID3D11Buffer *> &buffers,
vector<uint32_t> &strides)
2016-02-23 23:16:51 +00:00
{
PushBuffer(buffers, strides, vertexBuffer, sizeof(vec3), "point");
if (shader->hasNormals)
PushBuffer(buffers, strides, normalBuffer, sizeof(vec3),
2019-09-22 21:19:10 +00:00
"normal");
2016-02-23 23:16:51 +00:00
if (shader->hasColors)
PushBuffer(buffers, strides, colorBuffer, sizeof(uint32_t),
2019-09-22 21:19:10 +00:00
"color");
2016-02-23 23:16:51 +00:00
if (shader->hasTangents)
PushBuffer(buffers, strides, tangentBuffer, sizeof(vec3),
2019-09-22 21:19:10 +00:00
"tangent");
2016-02-23 23:16:51 +00:00
if (shader->nTexUnits <= uvBuffers.size()) {
for (size_t i = 0; i < shader->nTexUnits; i++) {
buffers.push_back(uvBuffers[i]);
strides.push_back((uint32_t)uvSizes[i]);
}
} else {
2019-09-22 21:19:10 +00:00
blog(LOG_ERROR,
"This vertex shader requires at least %u "
"texture buffers.",
(uint32_t)shader->nTexUnits);
2016-02-23 23:16:51 +00:00
}
}
2017-04-19 19:54:15 +00:00
void gs_vertex_buffer::InitBuffer(const size_t elementSize,
2019-09-22 21:19:10 +00:00
const size_t numVerts, void *array,
ID3D11Buffer **buffer)
2016-02-23 23:16:51 +00:00
{
D3D11_BUFFER_DESC bd;
D3D11_SUBRESOURCE_DATA srd;
HRESULT hr;
2019-09-22 21:19:10 +00:00
memset(&bd, 0, sizeof(bd));
2016-02-23 23:16:51 +00:00
memset(&srd, 0, sizeof(srd));
2019-09-22 21:19:10 +00:00
bd.Usage = dynamic ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_DEFAULT;
2016-02-23 23:16:51 +00:00
bd.CPUAccessFlags = dynamic ? D3D11_CPU_ACCESS_WRITE : 0;
2019-09-22 21:19:10 +00:00
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.ByteWidth = UINT(elementSize * numVerts);
srd.pSysMem = array;
2016-02-23 23:16:51 +00:00
hr = device->device->CreateBuffer(&bd, &srd, buffer);
if (FAILED(hr))
throw HRError("Failed to create buffer", hr);
}
2017-04-19 19:54:15 +00:00
void gs_vertex_buffer::BuildBuffers()
2016-02-23 23:16:51 +00:00
{
2017-04-19 19:54:15 +00:00
InitBuffer(sizeof(vec3), vbd.data->num, vbd.data->points,
2019-09-22 21:19:10 +00:00
&vertexBuffer);
2016-02-23 23:16:51 +00:00
2017-04-19 19:54:15 +00:00
if (vbd.data->normals)
InitBuffer(sizeof(vec3), vbd.data->num, vbd.data->normals,
2019-09-22 21:19:10 +00:00
&normalBuffer);
2016-02-23 23:16:51 +00:00
2017-04-19 19:54:15 +00:00
if (vbd.data->tangents)
InitBuffer(sizeof(vec3), vbd.data->num, vbd.data->tangents,
2019-09-22 21:19:10 +00:00
&tangentBuffer);
2016-02-23 23:16:51 +00:00
2017-04-19 19:54:15 +00:00
if (vbd.data->colors)
InitBuffer(sizeof(uint32_t), vbd.data->num, vbd.data->colors,
2019-09-22 21:19:10 +00:00
&colorBuffer);
2016-02-23 23:16:51 +00:00
2017-04-19 19:54:15 +00:00
for (size_t i = 0; i < vbd.data->num_tex; i++) {
2019-09-22 21:19:10 +00:00
struct gs_tvertarray *tverts = vbd.data->tvarray + i;
2016-02-23 23:16:51 +00:00
if (tverts->width != 2 && tverts->width != 4)
throw "Invalid texture vertex size specified";
if (!tverts->array)
throw "No texture vertices specified";
ComPtr<ID3D11Buffer> buffer;
2017-04-19 19:54:15 +00:00
InitBuffer(tverts->width * sizeof(float), vbd.data->num,
2019-09-22 21:19:10 +00:00
tverts->array, &buffer);
2016-02-23 23:16:51 +00:00
uvBuffers.push_back(buffer);
uvSizes.push_back(tverts->width * sizeof(float));
}
2017-04-19 19:54:15 +00:00
}
gs_vertex_buffer::gs_vertex_buffer(gs_device_t *device, struct gs_vb_data *data,
2019-09-22 21:19:10 +00:00
uint32_t flags)
: gs_obj(device, gs_type::gs_vertex_buffer),
dynamic((flags & GS_DYNAMIC) != 0),
vbd(data),
numVerts(data->num)
2017-04-19 19:54:15 +00:00
{
if (!data->num)
throw "Cannot initialize vertex buffer with 0 vertices";
if (!data->points)
throw "No points specified for vertex buffer";
2016-02-23 23:16:51 +00:00
2017-04-19 19:54:15 +00:00
BuildBuffers();
2016-02-23 23:16:51 +00:00
}