blob: 01c983216892b008e1362ec462900c138135d929 [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
Jamie Madill52b09c22016-04-11 14:12:31 -040041 mTranslatedAttribute.baseOffset = 0;
42 mTranslatedAttribute.usesFirstVertexOffset = false;
Cooper Partin558f2b52015-06-02 09:34:11 -070043 mTranslatedAttribute.stride = sizeof(GLfloat);
44
45 GLubyte indices[] = {0, 0, 3, 4, 2, 1, 1};
46
Corentin Wallez0984d112015-10-29 14:06:04 -040047 for (size_t i = 0; i < ArraySize(indices); i++)
Cooper Partin558f2b52015-06-02 09:34:11 -070048 {
49 mExpectedExpandedData.push_back(testData[indices[i]]);
50 mubyteIndices.push_back(indices[i]);
51 muintIndices.push_back(indices[i]);
52 mushortIndices.push_back(indices[i]);
53 }
54 }
55
56 void TearDown() override
57 {
58 SafeDelete(mSourceBuffer);
Corentin Wallez37c39792015-08-20 14:19:46 -040059 ANGLETest::TearDown();
Cooper Partin558f2b52015-06-02 09:34:11 -070060 }
61
62 void createMappableCompareBufferFromEmulatedBuffer(ID3D11Buffer *sourceBuffer, GLuint size, ID3D11Buffer **mappableBuffer)
63 {
64 *mappableBuffer = nullptr;
65
66 D3D11_BUFFER_DESC bufferDesc;
67 bufferDesc.ByteWidth = size;
68 bufferDesc.MiscFlags = 0;
69 bufferDesc.StructureByteStride = 0;
70 bufferDesc.Usage = D3D11_USAGE_STAGING;
71 bufferDesc.BindFlags = 0;
72 bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
73
74 HRESULT hr = mRenderer->getDevice()->CreateBuffer(&bufferDesc, nullptr, mappableBuffer);
75 ASSERT_TRUE(SUCCEEDED(hr));
76
77 D3D11_BOX srcBox;
78 srcBox.left = 0;
79 srcBox.right = size;
80 srcBox.top = 0;
81 srcBox.bottom = 1;
82 srcBox.front = 0;
83 srcBox.back = 1;
84
85 mRenderer->getDeviceContext()->CopySubresourceRegion(*mappableBuffer, 0, 0, 0, 0, sourceBuffer, 0, &srcBox);
86 }
87
88 void compareContents(ID3D11Buffer *actual)
89 {
90 ID3D11Buffer *compareBuffer = nullptr;
Cooper Partin4d61f7e2015-08-12 10:56:50 -070091 createMappableCompareBufferFromEmulatedBuffer(
92 actual, sizeof(GLfloat) * static_cast<GLuint>(mExpectedExpandedData.size()),
93 &compareBuffer);
Cooper Partin558f2b52015-06-02 09:34:11 -070094
95 D3D11_MAPPED_SUBRESOURCE mappedResource;
96 HRESULT hr = mRenderer->getDeviceContext()->Map(compareBuffer, 0, D3D11_MAP_READ, 0, &mappedResource);
97 ASSERT_TRUE(SUCCEEDED(hr));
98
99 GLfloat* compareData = static_cast<GLfloat*>(mappedResource.pData);
100 for (size_t i = 0; i < mExpectedExpandedData.size(); i++)
101 {
102 EXPECT_EQ(mExpectedExpandedData[i], compareData[i]);
103 }
104
105 mRenderer->getDeviceContext()->Unmap(compareBuffer, 0);
106 SafeRelease(compareBuffer);
107 }
108
109 void emulateAndCompare(rx::SourceIndexData *srcData)
110 {
Jamie Madill52b09c22016-04-11 14:12:31 -0400111 auto bufferOrError =
112 mSourceBuffer->getEmulatedIndexedBuffer(srcData, mTranslatedAttribute, 0);
Jamie Madill7d712e72016-03-29 21:54:33 -0400113 ASSERT_FALSE(bufferOrError.isError());
114 ID3D11Buffer *emulatedBuffer = bufferOrError.getResult();
Cooper Partin558f2b52015-06-02 09:34:11 -0700115 ASSERT_TRUE(emulatedBuffer != nullptr);
Cooper Partin558f2b52015-06-02 09:34:11 -0700116 compareContents(emulatedBuffer);
117 }
118
119 protected:
120 rx::Buffer11 *mSourceBuffer;
121 rx::Renderer11 *mRenderer;
122 rx::TranslatedAttribute mTranslatedAttribute;
123 std::vector<GLfloat> mExpectedExpandedData;
124 std::vector<GLubyte> mubyteIndices;
125 std::vector<GLuint> muintIndices;
126 std::vector<GLushort> mushortIndices;
127};
128
129// This tests that a GL_UNSIGNED_BYTE indices list can be successfully expanded
130// into a valid emulated indexed buffer.
131TEST_P(D3D11EmulatedIndexedBufferTest, TestNativeToExpandedUsingGLubyteIndices)
132{
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700133 rx::SourceIndexData srcData = {nullptr, mubyteIndices.data(),
134 static_cast<unsigned int>(mubyteIndices.size()),
135 GL_UNSIGNED_BYTE, false};
Cooper Partin558f2b52015-06-02 09:34:11 -0700136 emulateAndCompare(&srcData);
137}
138
139// This tests that a GL_UNSIGNED_SHORT indices list can be successfully expanded
140// into a valid emulated indexed buffer.
141TEST_P(D3D11EmulatedIndexedBufferTest, TestNativeToExpandedUsingGLushortIndices)
142{
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700143 rx::SourceIndexData srcData = {nullptr, mushortIndices.data(),
144 static_cast<unsigned int>(mushortIndices.size()),
145 GL_UNSIGNED_SHORT, false};
Cooper Partin558f2b52015-06-02 09:34:11 -0700146 emulateAndCompare(&srcData);
147}
148
149// This tests that a GL_UNSIGNED_INT indices list can be successfully expanded
150// into a valid emulated indexed buffer.
151TEST_P(D3D11EmulatedIndexedBufferTest, TestNativeToExpandedUsingGLuintIndices)
152{
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700153 rx::SourceIndexData srcData = {nullptr, muintIndices.data(),
154 static_cast<unsigned int>(muintIndices.size()), GL_UNSIGNED_INT,
155 false};
Cooper Partin558f2b52015-06-02 09:34:11 -0700156 emulateAndCompare(&srcData);
157}
158
159// This tests verifies that a Buffer11 contents remain unchanged after calling getEmulatedIndexedBuffer
160TEST_P(D3D11EmulatedIndexedBufferTest, TestSourceBufferRemainsUntouchedAfterExpandOperation)
161{
162 // Copy the original source buffer before any expand calls have been made
163 rx::Buffer11 *cleanSourceBuffer = new rx::Buffer11(mRenderer);
164 cleanSourceBuffer->copySubData(mSourceBuffer, 0, 0, mSourceBuffer->getSize());
165
166 // Do a basic exanded and compare test.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700167 rx::SourceIndexData srcData = {nullptr, muintIndices.data(),
168 static_cast<unsigned int>(muintIndices.size()), GL_UNSIGNED_INT,
169 false};
Cooper Partin558f2b52015-06-02 09:34:11 -0700170 emulateAndCompare(&srcData);
171
172 const uint8_t *sourceBufferMem = nullptr;
173 const uint8_t *cleanBufferMem = nullptr;
174
175 gl::Error error = mSourceBuffer->getData(&sourceBufferMem);
176 ASSERT_FALSE(error.isError());
177
178 error = cleanSourceBuffer->getData(&cleanBufferMem);
179 ASSERT_FALSE(error.isError());
180
181 int result = memcmp(sourceBufferMem, cleanBufferMem, cleanSourceBuffer->getSize());
182 ASSERT_EQ(result, 0);
183
184 SafeDelete(cleanSourceBuffer);
185}
186
187ANGLE_INSTANTIATE_TEST(D3D11EmulatedIndexedBufferTest,
188 ES2_D3D11());
189
190} // anonymous namespace