blob: fe40f38a0ddf8e8ee27e517e9d5cd78d1c1a7025 [file] [log] [blame]
Cooper Partin558f2b52015-06-02 09:34:11 -07001//
2// Copyright 2015 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6// D3D11EmulatedIndexedBufferTest:
7// Tests to validate our D3D11 support for emulating an indexed
8// vertex buffer.
9//
10
11#include "libANGLE/angletypes.h"
12#include "libANGLE/Context.h"
13#include "libANGLE/renderer/d3d/d3d11/Renderer11.h"
14#include "libANGLE/renderer/d3d/d3d11/Buffer11.h"
15#include "libANGLE/renderer/d3d/IndexDataManager.h"
16#include "test_utils/ANGLETest.h"
17#include "test_utils/angle_test_instantiate.h"
18
19using namespace angle;
20
21namespace
22{
23
24class D3D11EmulatedIndexedBufferTest : public ANGLETest
25{
26 protected:
27
28 void SetUp() override
29 {
30 ANGLETest::SetUp();
31 ASSERT_EQ(EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE, GetParam().getRenderer());
32
33 gl::Context *context = reinterpret_cast<gl::Context *>(getEGLWindow()->getContext());
34 mRenderer = rx::GetAs<rx::Renderer11>(context->getRenderer());
35
36 mSourceBuffer = new rx::Buffer11(mRenderer);
37 GLfloat testData[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f };
38 gl::Error error = mSourceBuffer->setData(testData, sizeof(testData), GL_STATIC_DRAW);
39 ASSERT_FALSE(error.isError());
40
41 mTranslatedAttribute.offset = 0;
42 mTranslatedAttribute.stride = sizeof(GLfloat);
43
44 GLubyte indices[] = {0, 0, 3, 4, 2, 1, 1};
45
46 for (size_t i = 0; i < _countof(indices); i++)
47 {
48 mExpectedExpandedData.push_back(testData[indices[i]]);
49 mubyteIndices.push_back(indices[i]);
50 muintIndices.push_back(indices[i]);
51 mushortIndices.push_back(indices[i]);
52 }
53 }
54
55 void TearDown() override
56 {
57 SafeDelete(mSourceBuffer);
58 ANGLETest::TearDown();
59 }
60
61 void createMappableCompareBufferFromEmulatedBuffer(ID3D11Buffer *sourceBuffer, GLuint size, ID3D11Buffer **mappableBuffer)
62 {
63 *mappableBuffer = nullptr;
64
65 D3D11_BUFFER_DESC bufferDesc;
66 bufferDesc.ByteWidth = size;
67 bufferDesc.MiscFlags = 0;
68 bufferDesc.StructureByteStride = 0;
69 bufferDesc.Usage = D3D11_USAGE_STAGING;
70 bufferDesc.BindFlags = 0;
71 bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
72
73 HRESULT hr = mRenderer->getDevice()->CreateBuffer(&bufferDesc, nullptr, mappableBuffer);
74 ASSERT_TRUE(SUCCEEDED(hr));
75
76 D3D11_BOX srcBox;
77 srcBox.left = 0;
78 srcBox.right = size;
79 srcBox.top = 0;
80 srcBox.bottom = 1;
81 srcBox.front = 0;
82 srcBox.back = 1;
83
84 mRenderer->getDeviceContext()->CopySubresourceRegion(*mappableBuffer, 0, 0, 0, 0, sourceBuffer, 0, &srcBox);
85 }
86
87 void compareContents(ID3D11Buffer *actual)
88 {
89 ID3D11Buffer *compareBuffer = nullptr;
90 createMappableCompareBufferFromEmulatedBuffer(actual, sizeof(GLfloat) * mExpectedExpandedData.size(), &compareBuffer);
91
92 D3D11_MAPPED_SUBRESOURCE mappedResource;
93 HRESULT hr = mRenderer->getDeviceContext()->Map(compareBuffer, 0, D3D11_MAP_READ, 0, &mappedResource);
94 ASSERT_TRUE(SUCCEEDED(hr));
95
96 GLfloat* compareData = static_cast<GLfloat*>(mappedResource.pData);
97 for (size_t i = 0; i < mExpectedExpandedData.size(); i++)
98 {
99 EXPECT_EQ(mExpectedExpandedData[i], compareData[i]);
100 }
101
102 mRenderer->getDeviceContext()->Unmap(compareBuffer, 0);
103 SafeRelease(compareBuffer);
104 }
105
106 void emulateAndCompare(rx::SourceIndexData *srcData)
107 {
108 ID3D11Buffer* emulatedBuffer = mSourceBuffer->getEmulatedIndexedBuffer(srcData, &mTranslatedAttribute);
109 ASSERT_TRUE(emulatedBuffer != nullptr);
110
111 compareContents(emulatedBuffer);
112 }
113
114 protected:
115 rx::Buffer11 *mSourceBuffer;
116 rx::Renderer11 *mRenderer;
117 rx::TranslatedAttribute mTranslatedAttribute;
118 std::vector<GLfloat> mExpectedExpandedData;
119 std::vector<GLubyte> mubyteIndices;
120 std::vector<GLuint> muintIndices;
121 std::vector<GLushort> mushortIndices;
122};
123
124// This tests that a GL_UNSIGNED_BYTE indices list can be successfully expanded
125// into a valid emulated indexed buffer.
126TEST_P(D3D11EmulatedIndexedBufferTest, TestNativeToExpandedUsingGLubyteIndices)
127{
128 rx::SourceIndexData srcData = {mubyteIndices.data(), mubyteIndices.size(), GL_UNSIGNED_BYTE, false};
129 emulateAndCompare(&srcData);
130}
131
132// This tests that a GL_UNSIGNED_SHORT indices list can be successfully expanded
133// into a valid emulated indexed buffer.
134TEST_P(D3D11EmulatedIndexedBufferTest, TestNativeToExpandedUsingGLushortIndices)
135{
136 rx::SourceIndexData srcData = {mushortIndices.data(), mushortIndices.size(), GL_UNSIGNED_SHORT, false};
137 emulateAndCompare(&srcData);
138}
139
140// This tests that a GL_UNSIGNED_INT indices list can be successfully expanded
141// into a valid emulated indexed buffer.
142TEST_P(D3D11EmulatedIndexedBufferTest, TestNativeToExpandedUsingGLuintIndices)
143{
144 rx::SourceIndexData srcData = {muintIndices.data(), muintIndices.size(), GL_UNSIGNED_INT, false};
145 emulateAndCompare(&srcData);
146}
147
148// This tests verifies that a Buffer11 contents remain unchanged after calling getEmulatedIndexedBuffer
149TEST_P(D3D11EmulatedIndexedBufferTest, TestSourceBufferRemainsUntouchedAfterExpandOperation)
150{
151 // Copy the original source buffer before any expand calls have been made
152 rx::Buffer11 *cleanSourceBuffer = new rx::Buffer11(mRenderer);
153 cleanSourceBuffer->copySubData(mSourceBuffer, 0, 0, mSourceBuffer->getSize());
154
155 // Do a basic exanded and compare test.
156 rx::SourceIndexData srcData = {muintIndices.data(), muintIndices.size(), GL_UNSIGNED_INT, false};
157 emulateAndCompare(&srcData);
158
159 const uint8_t *sourceBufferMem = nullptr;
160 const uint8_t *cleanBufferMem = nullptr;
161
162 gl::Error error = mSourceBuffer->getData(&sourceBufferMem);
163 ASSERT_FALSE(error.isError());
164
165 error = cleanSourceBuffer->getData(&cleanBufferMem);
166 ASSERT_FALSE(error.isError());
167
168 int result = memcmp(sourceBufferMem, cleanBufferMem, cleanSourceBuffer->getSize());
169 ASSERT_EQ(result, 0);
170
171 SafeDelete(cleanSourceBuffer);
172}
173
174ANGLE_INSTANTIATE_TEST(D3D11EmulatedIndexedBufferTest,
175 ES2_D3D11());
176
177} // anonymous namespace