2012-12-11 01:15:13 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// Copyright 2013 Pixar
|
2012-12-11 01:15:13 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
|
|
|
// with the following modification; you may not use this file except in
|
|
|
|
// compliance with the Apache License and the following modification to it:
|
|
|
|
// Section 6. Trademarks. is deleted and replaced with:
|
2012-12-11 01:15:13 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// 6. Trademarks. This License does not grant permission to use the trade
|
|
|
|
// names, trademarks, service marks, or product names of the Licensor
|
|
|
|
// and its affiliates, except as required to comply with Section 4(c) of
|
|
|
|
// the License and to reproduce the content of the NOTICE file.
|
2012-12-11 01:15:13 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// You may obtain a copy of the Apache License at
|
2013-07-18 21:19:50 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2013-07-18 21:19:50 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the Apache License with the above modification is
|
|
|
|
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
// KIND, either express or implied. See the Apache License for the specific
|
|
|
|
// language governing permissions and limitations under the Apache License.
|
2012-12-11 01:15:13 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "../far/dispatcher.h"
|
|
|
|
#include "../osd/d3d11DrawContext.h"
|
|
|
|
|
|
|
|
#include <D3D11.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
namespace OpenSubdiv {
|
|
|
|
namespace OPENSUBDIV_VERSION {
|
|
|
|
|
|
|
|
OsdD3D11DrawContext::OsdD3D11DrawContext() :
|
|
|
|
patchIndexBuffer(NULL),
|
|
|
|
ptexCoordinateBuffer(NULL),
|
|
|
|
ptexCoordinateBufferSRV(NULL),
|
|
|
|
fvarDataBuffer(NULL),
|
|
|
|
fvarDataBufferSRV(NULL),
|
|
|
|
vertexBufferSRV(NULL),
|
|
|
|
vertexValenceBuffer(NULL),
|
|
|
|
vertexValenceBufferSRV(NULL),
|
|
|
|
quadOffsetBuffer(NULL),
|
2013-05-14 01:43:05 +00:00
|
|
|
quadOffsetBufferSRV(NULL)
|
2012-12-11 01:15:13 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
OsdD3D11DrawContext::~OsdD3D11DrawContext()
|
|
|
|
{
|
|
|
|
if (patchIndexBuffer) patchIndexBuffer->Release();
|
|
|
|
if (ptexCoordinateBuffer) ptexCoordinateBuffer->Release();
|
|
|
|
if (ptexCoordinateBufferSRV) ptexCoordinateBufferSRV->Release();
|
|
|
|
if (fvarDataBuffer) fvarDataBuffer->Release();
|
|
|
|
if (fvarDataBufferSRV) fvarDataBufferSRV->Release();
|
|
|
|
if (vertexBufferSRV) vertexBufferSRV->Release();
|
|
|
|
if (vertexValenceBuffer) vertexValenceBuffer->Release();
|
|
|
|
if (vertexValenceBufferSRV) vertexValenceBufferSRV->Release();
|
|
|
|
if (quadOffsetBuffer) quadOffsetBuffer->Release();
|
|
|
|
if (quadOffsetBufferSRV) quadOffsetBufferSRV->Release();
|
|
|
|
};
|
|
|
|
|
2013-05-14 01:43:05 +00:00
|
|
|
OsdD3D11DrawContext *
|
|
|
|
OsdD3D11DrawContext::Create(FarPatchTables const *patchTables,
|
|
|
|
ID3D11DeviceContext *pd3d11DeviceContext,
|
2014-04-17 23:47:48 +00:00
|
|
|
int numVertexElements,
|
2013-05-14 01:43:05 +00:00
|
|
|
bool requireFVarData)
|
|
|
|
{
|
2013-05-17 20:30:43 +00:00
|
|
|
OsdD3D11DrawContext * result = new OsdD3D11DrawContext();
|
2014-04-17 23:47:48 +00:00
|
|
|
if (result->create(patchTables, pd3d11DeviceContext, numVertexElements, requireFVarData))
|
2013-05-17 20:30:43 +00:00
|
|
|
return result;
|
2013-05-14 01:43:05 +00:00
|
|
|
|
2013-05-17 20:30:43 +00:00
|
|
|
delete result;
|
2013-05-14 01:43:05 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
bool
|
2013-05-17 20:30:43 +00:00
|
|
|
OsdD3D11DrawContext::create(FarPatchTables const *patchTables,
|
|
|
|
ID3D11DeviceContext *pd3d11DeviceContext,
|
2014-04-17 23:47:48 +00:00
|
|
|
int numVertexElements,
|
2013-05-17 20:30:43 +00:00
|
|
|
bool requireFVarData)
|
2013-05-14 01:43:05 +00:00
|
|
|
{
|
2012-12-11 01:15:13 +00:00
|
|
|
// adaptive patches
|
2013-05-14 01:43:05 +00:00
|
|
|
_isAdaptive = true;
|
2012-12-11 01:15:13 +00:00
|
|
|
|
2013-05-14 01:43:05 +00:00
|
|
|
ID3D11Device *pd3d11Device = NULL;
|
|
|
|
pd3d11DeviceContext->GetDevice(&pd3d11Device);
|
|
|
|
assert(pd3d11Device);
|
|
|
|
|
2014-04-17 23:47:48 +00:00
|
|
|
ConvertPatchArrays(patchTables->GetPatchArrayVector(), patchArrays, patchTables->GetMaxValence(), numVertexElements);
|
2013-05-14 01:43:05 +00:00
|
|
|
|
|
|
|
FarPatchTables::PTable const & ptables = patchTables->GetPatchTable();
|
2013-05-17 16:47:44 +00:00
|
|
|
FarPatchTables::PatchParamTable const & ptexCoordTables = patchTables->GetPatchParamTable();
|
2013-05-14 01:43:05 +00:00
|
|
|
int totalPatchIndices = (int)ptables.size();
|
|
|
|
int totalPatches = (int)ptexCoordTables.size();
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
// Allocate and fill index buffer.
|
|
|
|
D3D11_BUFFER_DESC bd;
|
|
|
|
bd.ByteWidth = totalPatchIndices * sizeof(int);
|
|
|
|
bd.Usage = D3D11_USAGE_DYNAMIC;
|
|
|
|
bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
|
|
|
|
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
|
|
|
bd.MiscFlags = 0;
|
|
|
|
bd.StructureByteStride = sizeof(int);
|
|
|
|
HRESULT hr = pd3d11Device->CreateBuffer(&bd, NULL, &patchIndexBuffer);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
|
|
|
hr = pd3d11DeviceContext->Map(patchIndexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
unsigned int * indexBuffer = (unsigned int *) mappedResource.pData;
|
2013-05-14 01:43:05 +00:00
|
|
|
memcpy(indexBuffer, &ptables[0], totalPatchIndices * sizeof(unsigned int));
|
|
|
|
|
|
|
|
pd3d11DeviceContext->Unmap(patchIndexBuffer, 0);
|
2012-12-11 01:15:13 +00:00
|
|
|
|
2013-10-05 01:26:22 +00:00
|
|
|
// create patch param buffer
|
|
|
|
bd.ByteWidth = totalPatches * sizeof(FarPatchParam);
|
2012-12-11 01:15:13 +00:00
|
|
|
bd.Usage = D3D11_USAGE_DYNAMIC;
|
|
|
|
bd.BindFlags = D3D11_BIND_SHADER_RESOURCE;
|
|
|
|
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
|
|
|
bd.MiscFlags = 0;
|
|
|
|
bd.StructureByteStride = sizeof(unsigned int);
|
2013-05-14 01:43:05 +00:00
|
|
|
hr = pd3d11Device->CreateBuffer(&bd, NULL, &ptexCoordinateBuffer);
|
2012-12-11 01:15:13 +00:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
D3D11_SHADER_RESOURCE_VIEW_DESC srvd;
|
|
|
|
ZeroMemory(&srvd, sizeof(srvd));
|
2013-10-05 01:26:22 +00:00
|
|
|
srvd.Format = DXGI_FORMAT_R32G32_UINT;
|
2012-12-11 01:15:13 +00:00
|
|
|
srvd.ViewDimension = D3D11_SRV_DIMENSION_BUFFER;
|
|
|
|
srvd.Buffer.FirstElement = 0;
|
2013-05-14 01:43:05 +00:00
|
|
|
srvd.Buffer.NumElements = totalPatches;
|
|
|
|
hr = pd3d11Device->CreateShaderResourceView(ptexCoordinateBuffer, &srvd, &ptexCoordinateBufferSRV);
|
2012-12-11 01:15:13 +00:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-14 01:43:05 +00:00
|
|
|
hr = pd3d11DeviceContext->Map(ptexCoordinateBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
|
2012-12-11 01:15:13 +00:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-14 01:43:05 +00:00
|
|
|
unsigned int * ptexBuffer = (unsigned int *) mappedResource.pData;
|
2013-05-17 20:30:43 +00:00
|
|
|
memcpy(ptexBuffer, &ptexCoordTables[0], totalPatches * sizeof(FarPatchParam));
|
2013-05-14 01:43:05 +00:00
|
|
|
pd3d11DeviceContext->Unmap(ptexCoordinateBuffer, 0);
|
2012-12-11 01:15:13 +00:00
|
|
|
|
2013-05-14 01:43:05 +00:00
|
|
|
// create vertex valence buffer and vertex texture
|
2012-12-11 01:15:13 +00:00
|
|
|
FarPatchTables::VertexValenceTable const &
|
|
|
|
valenceTable = patchTables->GetVertexValenceTable();
|
|
|
|
|
|
|
|
if (not valenceTable.empty()) {
|
|
|
|
D3D11_BUFFER_DESC bd;
|
|
|
|
bd.ByteWidth = UINT(valenceTable.size() * sizeof(unsigned int));
|
|
|
|
bd.Usage = D3D11_USAGE_DEFAULT;
|
|
|
|
bd.BindFlags = D3D11_BIND_SHADER_RESOURCE;
|
|
|
|
bd.CPUAccessFlags = 0;
|
|
|
|
bd.MiscFlags = 0;
|
|
|
|
bd.StructureByteStride = sizeof(unsigned int);
|
|
|
|
D3D11_SUBRESOURCE_DATA initData;
|
|
|
|
initData.pSysMem = &valenceTable[0];
|
|
|
|
HRESULT hr = pd3d11Device->CreateBuffer(&bd, &initData, &vertexValenceBuffer);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
D3D11_SHADER_RESOURCE_VIEW_DESC srvd;
|
|
|
|
ZeroMemory(&srvd, sizeof(srvd));
|
2012-12-13 18:00:46 +00:00
|
|
|
srvd.Format = DXGI_FORMAT_R32_SINT;
|
2012-12-11 01:15:13 +00:00
|
|
|
srvd.ViewDimension = D3D11_SRV_DIMENSION_BUFFER;
|
|
|
|
srvd.Buffer.FirstElement = 0;
|
|
|
|
srvd.Buffer.NumElements = UINT(valenceTable.size());
|
|
|
|
hr = pd3d11Device->CreateShaderResourceView(vertexValenceBuffer, &srvd, &vertexValenceBufferSRV);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FarPatchTables::QuadOffsetTable const &
|
|
|
|
quadOffsetTable = patchTables->GetQuadOffsetTable();
|
|
|
|
|
|
|
|
if (not quadOffsetTable.empty()) {
|
|
|
|
D3D11_BUFFER_DESC bd;
|
|
|
|
bd.ByteWidth = UINT(quadOffsetTable.size() * sizeof(unsigned int));
|
|
|
|
bd.Usage = D3D11_USAGE_DEFAULT;
|
|
|
|
bd.BindFlags = D3D11_BIND_SHADER_RESOURCE;
|
|
|
|
bd.CPUAccessFlags = 0;
|
|
|
|
bd.MiscFlags = 0;
|
|
|
|
bd.StructureByteStride = sizeof(unsigned int);
|
|
|
|
D3D11_SUBRESOURCE_DATA initData;
|
|
|
|
initData.pSysMem = &quadOffsetTable[0];
|
|
|
|
HRESULT hr = pd3d11Device->CreateBuffer(&bd, &initData, &quadOffsetBuffer);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
D3D11_SHADER_RESOURCE_VIEW_DESC srvd;
|
|
|
|
ZeroMemory(&srvd, sizeof(srvd));
|
2012-12-13 18:00:46 +00:00
|
|
|
srvd.Format = DXGI_FORMAT_R32_SINT;
|
2012-12-11 01:15:13 +00:00
|
|
|
srvd.ViewDimension = D3D11_SRV_DIMENSION_BUFFER;
|
|
|
|
srvd.Buffer.FirstElement = 0;
|
|
|
|
srvd.Buffer.NumElements = UINT(quadOffsetTable.size());
|
|
|
|
hr = pd3d11Device->CreateShaderResourceView(quadOffsetBuffer, &srvd, &quadOffsetBufferSRV);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-14 01:43:05 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
void
|
2013-05-14 01:43:05 +00:00
|
|
|
OsdD3D11DrawContext::updateVertexTexture(ID3D11Buffer *vbo,
|
|
|
|
ID3D11DeviceContext *pd3d11DeviceContext,
|
|
|
|
int numVertices,
|
|
|
|
int numVertexElements)
|
2012-12-11 01:15:13 +00:00
|
|
|
{
|
2013-05-14 01:43:05 +00:00
|
|
|
ID3D11Device *pd3d11Device = NULL;
|
|
|
|
pd3d11DeviceContext->GetDevice(&pd3d11Device);
|
|
|
|
assert(pd3d11Device);
|
2012-12-11 01:15:13 +00:00
|
|
|
|
2013-05-14 01:43:05 +00:00
|
|
|
D3D11_SHADER_RESOURCE_VIEW_DESC srvd;
|
|
|
|
ZeroMemory(&srvd, sizeof(srvd));
|
|
|
|
srvd.Format = DXGI_FORMAT_R32_FLOAT;
|
|
|
|
srvd.ViewDimension = D3D11_SRV_DIMENSION_BUFFER;
|
|
|
|
srvd.Buffer.FirstElement = 0;
|
|
|
|
srvd.Buffer.NumElements = numVertices * numVertexElements;
|
|
|
|
HRESULT hr = pd3d11Device->CreateShaderResourceView(vbo, &srvd, &vertexBufferSRV);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return;
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace OPENSUBDIV_VERSION
|
|
|
|
} // end namespace OpenSubdiv
|