GteDX11VertexBuffer.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 DX11VertexBuffer::DX11VertexBuffer(ID3D11Device* device, VertexBuffer const* vbuffer)
13  :
14  DX11Buffer(vbuffer)
15 {
16  // Specify the buffer description.
17  D3D11_BUFFER_DESC desc;
18  desc.ByteWidth = vbuffer->GetNumBytes();
19  desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
20  desc.MiscFlags = D3D11_RESOURCE_MISC_NONE;
21  desc.StructureByteStride = 0;
22  Resource::Usage usage = vbuffer->GetUsage();
23  if (usage == Resource::IMMUTABLE)
24  {
25  desc.Usage = D3D11_USAGE_IMMUTABLE;
26  desc.CPUAccessFlags = D3D11_CPU_ACCESS_NONE;
27  }
28  else if (usage == Resource::DYNAMIC_UPDATE)
29  {
30  desc.Usage = D3D11_USAGE_DYNAMIC;
31  desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
32  }
33  else // usage == Resource::SHADER_OUTPUT
34  {
35  // TODO: Write a sample application to test this case.
36  LogError("Vertex output streams are not yet tested.");
37  desc.Usage = D3D11_USAGE_DEFAULT;
38  desc.BindFlags |= D3D11_BIND_STREAM_OUTPUT;
39  desc.CPUAccessFlags = D3D11_CPU_ACCESS_NONE;
40  }
41 
42  // Create the buffer.
43  ID3D11Buffer* buffer = nullptr;
44  HRESULT hr;
45  if (vbuffer->GetData())
46  {
47  D3D11_SUBRESOURCE_DATA data;
48  data.pSysMem = vbuffer->GetData();
49  data.SysMemPitch = 0;
50  data.SysMemSlicePitch = 0;
51  hr = device->CreateBuffer(&desc, &data, &buffer);
52  }
53  else
54  {
55  hr = device->CreateBuffer(&desc, nullptr, & buffer);
56  }
57  CHECK_HR_RETURN_NONE("Failed to create vertex buffer");
58  mDXObject = buffer;
59 
60  // Create a staging buffer if requested.
61  if (vbuffer->GetCopyType() != Resource::COPY_NONE)
62  {
63  CreateStaging(device, desc);
64  }
65 }
66 
67 std::shared_ptr<GEObject> DX11VertexBuffer::Create(void* device, GraphicsObject const* object)
68 {
69  if (object->GetType() == GT_VERTEX_BUFFER)
70  {
71  return std::make_shared<DX11VertexBuffer>(reinterpret_cast<ID3D11Device*>(device),
72  static_cast<VertexBuffer const*>(object));
73  }
74 
75  LogError("Invalid object type.");
76  return nullptr;
77 }
78 
79 void DX11VertexBuffer::Enable(ID3D11DeviceContext* context)
80 {
81  if (mDXObject)
82  {
83  // The MSDN documentation for ID3D11DeviceContext::IASetVertexBuffers
84  // and ID3D11DeviceContext::Draw(numVertices, startVertex) appears
85  // not to mention that startVertex is relative to the offsets[]
86  // passed to IASetVertexBuffers. If you set the offsets[0] here, you
87  // should call Draw(numVertices,0). If you instead call
88  // Draw(numVertices, startVertex), then you should set offsets[0]
89  // to 0. The latter choice is made for GTEngine. TODO: Is there a
90  // performance issue by setting offsets[0] to zero? This depends on
91  // what the input assembly stage does with the buffers when you
92  // enable them using IASetVertexBuffers.
93  ID3D11Buffer* buffers[1] = { GetDXBuffer() };
94  VertexBuffer* vbuffer = GetVertexBuffer();
95  UINT strides[1] = { vbuffer->GetElementSize() };
96  UINT offsets[1] = { 0 };
97  context->IASetVertexBuffers(0, 1, buffers, strides, offsets);
98  }
99 }
100 
101 void DX11VertexBuffer::Disable(ID3D11DeviceContext* context)
102 {
103  if (mDXObject)
104  {
105  ID3D11Buffer* buffers[1] = { nullptr };
106  UINT strides[1] = { 0 };
107  UINT offsets[1] = { 0 };
108  context->IASetVertexBuffers(0, 1, buffers, strides, offsets);
109  }
110 }
DYNAMIC_UPDATE
Definition: GteResource.h:42
COPY_NONE
Definition: GteResource.h:55
GT_VERTEX_BUFFER
#define D3D11_RESOURCE_MISC_NONE
DX11VertexBuffer(ID3D11Device *device, VertexBuffer const *vbuffer)
#define CHECK_HR_RETURN_NONE(msg)
IMMUTABLE
Definition: GteResource.h:42
ID3D11Buffer * GetDXBuffer() const
Definition: GteDX11Buffer.h:74
Usage GetUsage() const
Definition: GteResource.h:126
const GLuint * buffers
Definition: glcorearb.h:656
GraphicsObjectType GetType() const
GLuint GLsizei const GLuint const GLintptr * offsets
Definition: glcorearb.h:2616
unsigned int GetElementSize() const
Definition: GteResource.h:111
#define LogError(message)
Definition: GteLogger.h:92
GLboolean * data
Definition: glcorearb.h:126
ID3D11DeviceChild * mDXObject
void Enable(ID3D11DeviceContext *context)
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
void Disable(ID3D11DeviceContext *context)
static std::shared_ptr< GEObject > Create(void *device, GraphicsObject const *object)
GLsizeiptr const void GLenum usage
Definition: glcorearb.h:659
#define D3D11_CPU_ACCESS_NONE
CopyType GetCopyType() const
Definition: GteResource.h:136
typedef UINT(WINAPI *PFNWGLGETGPUIDSAMDPROC)(UINT maxCount
GLuint buffer
Definition: glcorearb.h:655
VertexBuffer * GetVertexBuffer() const
GLsizei const GLuint const GLintptr const GLsizei * strides
Definition: glcorearb.h:2620


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