GteDX11IndexBuffer.cpp
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #include <GTEnginePCH.h>
10 using namespace gte;
11 
12 DX11IndexBuffer::DX11IndexBuffer(ID3D11Device* device, IndexBuffer const* ibuffer)
13  :
14  DX11Buffer(ibuffer),
15  mFormat(ibuffer->GetElementSize() == sizeof(unsigned int) ?
16  DXGI_FORMAT_R32_UINT : DXGI_FORMAT_R16_UINT)
17 {
18  // Specify the buffer description.
19  D3D11_BUFFER_DESC desc;
20  desc.ByteWidth = ibuffer->GetNumBytes();
21  desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
22  desc.MiscFlags = D3D11_RESOURCE_MISC_NONE;
23  desc.StructureByteStride = 0;
24  Resource::Usage usage = ibuffer->GetUsage();
25  if (usage == Resource::IMMUTABLE)
26  {
27  desc.Usage = D3D11_USAGE_IMMUTABLE;
28  desc.CPUAccessFlags = D3D11_CPU_ACCESS_NONE;
29  }
30  else if (usage == Resource::DYNAMIC_UPDATE)
31  {
32  desc.Usage = D3D11_USAGE_DYNAMIC;
33  desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
34  }
35  else
36  {
37  desc.Usage = D3D11_USAGE_DEFAULT;
38  desc.CPUAccessFlags = D3D11_CPU_ACCESS_NONE;
39  }
40 
41  // Create the buffer.
42  ID3D11Buffer* buffer = nullptr;
43  HRESULT hr;
44  if (ibuffer->GetData())
45  {
46  D3D11_SUBRESOURCE_DATA data;
47  data.pSysMem = ibuffer->GetData();
48  data.SysMemPitch = 0;
49  data.SysMemSlicePitch = 0;
50  hr = device->CreateBuffer(&desc, &data, &buffer);
51  }
52  else
53  {
54  hr = device->CreateBuffer(&desc, nullptr, &buffer);
55  }
56  CHECK_HR_RETURN_NONE("Failed to create index buffer");
57  mDXObject = buffer;
58 
59  // Create a staging buffer if requested.
60  if (ibuffer->GetCopyType() != Resource::COPY_NONE)
61  {
62  CreateStaging(device, desc);
63  }
64 }
65 
66 std::shared_ptr<GEObject> DX11IndexBuffer::Create(void* device, GraphicsObject const* object)
67 {
68  if (object->GetType() == GT_INDEX_BUFFER)
69  {
70  return std::make_shared<DX11IndexBuffer>(
71  reinterpret_cast<ID3D11Device*>(device),
72  static_cast<IndexBuffer const*>(object));
73  }
74 
75  LogError("Invalid object type.");
76  return nullptr;
77 }
78 
79 void DX11IndexBuffer::Enable(ID3D11DeviceContext* context)
80 {
81  if (mDXObject)
82  {
83  ID3D11Buffer* dxBuffer = static_cast<ID3D11Buffer*>(mDXObject);
84  context->IASetIndexBuffer(dxBuffer, mFormat, 0);
85  }
86 }
87 
88 void DX11IndexBuffer::Disable(ID3D11DeviceContext* context)
89 {
90  if (mDXObject)
91  {
92  context->IASetIndexBuffer(0, DXGI_FORMAT_UNKNOWN, 0);
93  }
94 }
DYNAMIC_UPDATE
Definition: GteResource.h:42
COPY_NONE
Definition: GteResource.h:55
static std::shared_ptr< GEObject > Create(void *device, GraphicsObject const *object)
#define D3D11_RESOURCE_MISC_NONE
#define CHECK_HR_RETURN_NONE(msg)
IMMUTABLE
Definition: GteResource.h:42
Usage GetUsage() const
Definition: GteResource.h:126
GraphicsObjectType GetType() const
void Enable(ID3D11DeviceContext *context)
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
#define LogError(message)
Definition: GteLogger.h:92
GLboolean * data
Definition: glcorearb.h:126
ID3D11DeviceChild * mDXObject
unsigned int GetNumBytes() const
Definition: GteResource.h:116
void CreateStaging(ID3D11Device *device, D3D11_BUFFER_DESC const &bf)
char const * GetData() const
Definition: GteResource.h:151
GLsizeiptr const void GLenum usage
Definition: glcorearb.h:659
DX11IndexBuffer(ID3D11Device *device, IndexBuffer const *vbuffer)
#define D3D11_CPU_ACCESS_NONE
void Disable(ID3D11DeviceContext *context)
CopyType GetCopyType() const
Definition: GteResource.h:136
GT_INDEX_BUFFER
GLuint buffer
Definition: glcorearb.h:655


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 03:59:59