blob: fe8a722cf5f3f0faa1498ddb48c5845b27e95889 [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 Madill53a36002016-04-08 19:03:18 +000041 mTranslatedAttribute.offset = 0;
Cooper Partin558f2b52015-06-02 09:34:11 -070042 mTranslatedAttribute.stride = sizeof(GLfloat);
43
44 GLubyte indices[] = {0, 0, 3, 4, 2, 1, 1};
45
Corentin Wallez0984d112015-10-29 14:06:04 -040046 for (size_t i = 0; i < ArraySize(indices); i++)
Cooper Partin558f2b52015-06-02 09:34:11 -070047 {
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);
Corentin Wallez37c39792015-08-20 14:19:46 -040058 ANGLETest::TearDown();
Cooper Partin558f2b52015-06-02 09:34:11 -070059 }
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;
Cooper Partin4d61f7e2015-08-12 10:56:50 -070090 createMappableCompareBufferFromEmulatedBuffer(
91 actual, sizeof(GLfloat) * static_cast<GLuint>(mExpectedExpandedData.size()),
92 &compareBuffer);
Cooper Partin558f2b52015-06-02 09:34:11 -070093
94 D3D11_MAPPED_SUBRESOURCE mappedResource;
95 HRESULT hr = mRenderer->getDeviceContext()->Map(compareBuffer, 0, D3D11_MAP_READ, 0, &mappedResource);
96 ASSERT_TRUE(SUCCEEDED(hr));
97
98 GLfloat* compareData = static_cast<GLfloat*>(mappedResource.pData);
99 for (size_t i = 0; i < mExpectedExpandedData.size(); i++)
100 {
101 EXPECT_EQ(mExpectedExpandedData[i], compareData[i]);
102 }
103
104 mRenderer->getDeviceContext()->Unmap(compareBuffer, 0);
105 SafeRelease(compareBuffer);
106 }
107
108 void emulateAndCompare(rx::SourceIndexData *srcData)
109 {
Jamie Madill53a36002016-04-08 19:03:18 +0000110 auto bufferOrError = mSourceBuffer->getEmulatedIndexedBuffer(srcData, mTranslatedAttribute);
Jamie Madill7d712e72016-03-29 21:54:33 -0400111 ASSERT_FALSE(bufferOrError.isError());
112 ID3D11Buffer *emulatedBuffer = bufferOrError.getResult();
Cooper Partin558f2b52015-06-02 09:34:11 -0700113 ASSERT_TRUE(emulatedBuffer != nullptr);
Cooper Partin558f2b52015-06-02 09:34:11 -0700114 compareContents(emulatedBuffer);
115 }
116
117 protected:
118 rx::Buffer11 *mSourceBuffer;
119 rx::Renderer11 *mRenderer;
120 rx::TranslatedAttribute mTranslatedAttribute;
121 std::vector<GLfloat> mExpectedExpandedData;
122 std::vector<GLubyte> mubyteIndices;
123 std::vector<GLuint> muintIndices;
124 std::vector<GLushort> mushortIndices;
125};
126
127// This tests that a GL_UNSIGNED_BYTE indices list can be successfully expanded
128// into a valid emulated indexed buffer.
129TEST_P(D3D11EmulatedIndexedBufferTest, TestNativeToExpandedUsingGLubyteIndices)
130{
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700131 rx::SourceIndexData srcData = {nullptr, mubyteIndices.data(),
132 static_cast<unsigned int>(mubyteIndices.size()),
133 GL_UNSIGNED_BYTE, false};
Cooper Partin558f2b52015-06-02 09:34:11 -0700134 emulateAndCompare(&srcData);
135}
136
137// This tests that a GL_UNSIGNED_SHORT indices list can be successfully expanded
138// into a valid emulated indexed buffer.
139TEST_P(D3D11EmulatedIndexedBufferTest, TestNativeToExpandedUsingGLushortIndices)
140{
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700141 rx::SourceIndexData srcData = {nullptr, mushortIndices.data(),
142 static_cast<unsigned int>(mushortIndices.size()),
143 GL_UNSIGNED_SHORT, false};
Cooper Partin558f2b52015-06-02 09:34:11 -0700144 emulateAndCompare(&srcData);
145}
146
147// This tests that a GL_UNSIGNED_INT indices list can be successfully expanded
148// into a valid emulated indexed buffer.
149TEST_P(D3D11EmulatedIndexedBufferTest, TestNativeToExpandedUsingGLuintIndices)
150{
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700151 rx::SourceIndexData srcData = {nullptr, muintIndices.data(),
152 static_cast<unsigned int>(muintIndices.size()), GL_UNSIGNED_INT,
153 false};
Cooper Partin558f2b52015-06-02 09:34:11 -0700154 emulateAndCompare(&srcData);
155}
156
157// This tests verifies that a Buffer11 contents remain unchanged after calling getEmulatedIndexedBuffer
158TEST_P(D3D11EmulatedIndexedBufferTest, TestSourceBufferRemainsUntouchedAfterExpandOperation)
159{
160 // Copy the original source buffer before any expand calls have been made
161 rx::Buffer11 *cleanSourceBuffer = new rx::Buffer11(mRenderer);
162 cleanSourceBuffer->copySubData(mSourceBuffer, 0, 0, mSourceBuffer->getSize());
163
164 // Do a basic exanded and compare test.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700165 rx::SourceIndexData srcData = {nullptr, muintIndices.data(),
166 static_cast<unsigned int>(muintIndices.size()), GL_UNSIGNED_INT,
167 false};
Cooper Partin558f2b52015-06-02 09:34:11 -0700168 emulateAndCompare(&srcData);
169
170 const uint8_t *sourceBufferMem = nullptr;
171 const uint8_t *cleanBufferMem = nullptr;
172
173 gl::Error error = mSourceBuffer->getData(&sourceBufferMem);
174 ASSERT_FALSE(error.isError());
175
176 error = cleanSourceBuffer->getData(&cleanBufferMem);
177 ASSERT_FALSE(error.isError());
178
179 int result = memcmp(sourceBufferMem, cleanBufferMem, cleanSourceBuffer->getSize());
180 ASSERT_EQ(result, 0);
181
182 SafeDelete(cleanSourceBuffer);
183}
184
185ANGLE_INSTANTIATE_TEST(D3D11EmulatedIndexedBufferTest,
186 ES2_D3D11());
187
188} // anonymous namespace