Hash :
4d61f7ed
Author :
Date :
2015-08-12T10:56:50
Reland Fixed compiler warning C4267 'conversion from 'size_t' to 'type', possible loss of data' Additional warnings found with more testing and added C4267 warning disable only for angle_libpng BUG=angleproject:1120 Change-Id: Ic403dcff5a8018056fa51a8c408e64207f3362eb Reviewed-on: https://chromium-review.googlesource.com/293028 Reviewed-by: Jamie Madill <jmadill@chromium.org> Reviewed-by: Geoff Lang <geofflang@chromium.org> Tested-by: Geoff Lang <geofflang@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
//
// Copyright 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include "test_utils/ANGLETest.h"
using namespace angle;
class InstancingTest : public ANGLETest
{
protected:
InstancingTest()
{
setWindowWidth(256);
setWindowHeight(256);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
}
virtual void SetUp()
{
ANGLETest::SetUp();
mVertexAttribDivisorANGLE = NULL;
mDrawArraysInstancedANGLE = NULL;
mDrawElementsInstancedANGLE = NULL;
char *extensionString = (char*)glGetString(GL_EXTENSIONS);
if (strstr(extensionString, "GL_ANGLE_instanced_arrays"))
{
mVertexAttribDivisorANGLE = (PFNGLVERTEXATTRIBDIVISORANGLEPROC)eglGetProcAddress("glVertexAttribDivisorANGLE");
mDrawArraysInstancedANGLE = (PFNGLDRAWARRAYSINSTANCEDANGLEPROC)eglGetProcAddress("glDrawArraysInstancedANGLE");
mDrawElementsInstancedANGLE = (PFNGLDRAWELEMENTSINSTANCEDANGLEPROC)eglGetProcAddress("glDrawElementsInstancedANGLE");
}
ASSERT_TRUE(mVertexAttribDivisorANGLE != NULL);
ASSERT_TRUE(mDrawArraysInstancedANGLE != NULL);
ASSERT_TRUE(mDrawElementsInstancedANGLE != NULL);
// Initialize the vertex and index vectors
GLfloat qvertex1[3] = {-quadRadius, quadRadius, 0.0f};
GLfloat qvertex2[3] = {-quadRadius, -quadRadius, 0.0f};
GLfloat qvertex3[3] = { quadRadius, -quadRadius, 0.0f};
GLfloat qvertex4[3] = { quadRadius, quadRadius, 0.0f};
mQuadVertices.insert(mQuadVertices.end(), qvertex1, qvertex1 + 3);
mQuadVertices.insert(mQuadVertices.end(), qvertex2, qvertex2 + 3);
mQuadVertices.insert(mQuadVertices.end(), qvertex3, qvertex3 + 3);
mQuadVertices.insert(mQuadVertices.end(), qvertex4, qvertex4 + 3);
GLfloat coord1[2] = {0.0f, 0.0f};
GLfloat coord2[2] = {0.0f, 1.0f};
GLfloat coord3[2] = {1.0f, 1.0f};
GLfloat coord4[2] = {1.0f, 0.0f};
mTexcoords.insert(mTexcoords.end(), coord1, coord1 + 2);
mTexcoords.insert(mTexcoords.end(), coord2, coord2 + 2);
mTexcoords.insert(mTexcoords.end(), coord3, coord3 + 2);
mTexcoords.insert(mTexcoords.end(), coord4, coord4 + 2);
mIndices.push_back(0);
mIndices.push_back(1);
mIndices.push_back(2);
mIndices.push_back(0);
mIndices.push_back(2);
mIndices.push_back(3);
for (size_t vertexIndex = 0; vertexIndex < 6; ++vertexIndex)
{
mNonIndexedVertices.insert(mNonIndexedVertices.end(),
mQuadVertices.begin() + mIndices[vertexIndex] * 3,
mQuadVertices.begin() + mIndices[vertexIndex] * 3 + 3);
}
for (size_t vertexIndex = 0; vertexIndex < 6; ++vertexIndex)
{
mNonIndexedVertices.insert(mNonIndexedVertices.end(),
mQuadVertices.begin() + mIndices[vertexIndex] * 3,
mQuadVertices.begin() + mIndices[vertexIndex] * 3 + 3);
}
// Tile a 2x2 grid of the tiles
for (float y = -1.0f + quadRadius; y < 1.0f - quadRadius; y += quadRadius * 3)
{
for (float x = -1.0f + quadRadius; x < 1.0f - quadRadius; x += quadRadius * 3)
{
GLfloat instance[3] = {x + quadRadius, y + quadRadius, 0.0f};
mInstances.insert(mInstances.end(), instance, instance + 3);
}
}
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
ASSERT_GL_NO_ERROR();
}
GLuint setupDrawArraysTest(const std::string &vs)
{
const std::string fs = SHADER_SOURCE
(
precision mediump float;
void main()
{
gl_FragColor = vec4(1.0, 0, 0, 1.0);
}
);
GLuint program = CompileProgram(vs, fs);
if (program == 0)
{
return 0;
}
// Set the viewport
glViewport(0, 0, getWindowWidth(), getWindowHeight());
// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT);
// Use the program object
glUseProgram(program);
return program;
}
void runDrawArraysTest(GLuint program, GLint first, GLsizei count, GLsizei instanceCount, float *offset)
{
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, mInstances.size() * sizeof(mInstances[0]), &mInstances[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Get the attribute locations
GLint positionLoc = glGetAttribLocation(program, "a_position");
GLint instancePosLoc = glGetAttribLocation(program, "a_instancePos");
// Load the vertex position
glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 0, mNonIndexedVertices.data());
glEnableVertexAttribArray(positionLoc);
// Load the instance position
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(instancePosLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(instancePosLoc);
// Enable instancing
mVertexAttribDivisorANGLE(instancePosLoc, 1);
// Offset
GLint uniformLoc = glGetUniformLocation(program, "u_offset");
ASSERT_NE(uniformLoc, -1);
glUniform3fv(uniformLoc, 1, offset);
// Do the instanced draw
mDrawArraysInstancedANGLE(GL_TRIANGLES, first, count, instanceCount);
ASSERT_GL_NO_ERROR();
}
virtual void runDrawElementsTest(std::string vs, bool shouldAttribZeroBeInstanced)
{
const std::string fs = SHADER_SOURCE
(
precision mediump float;
void main()
{
gl_FragColor = vec4(1.0, 0, 0, 1.0);
}
);
GLuint program = CompileProgram(vs, fs);
ASSERT_NE(program, 0u);
// Get the attribute locations
GLint positionLoc = glGetAttribLocation(program, "a_position");
GLint instancePosLoc = glGetAttribLocation(program, "a_instancePos");
// If this ASSERT fails then the vertex shader code should be refactored
ASSERT_EQ(shouldAttribZeroBeInstanced, (instancePosLoc == 0));
// Set the viewport
glViewport(0, 0, getWindowWidth(), getWindowHeight());
// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT);
// Use the program object
glUseProgram(program);
// Load the vertex position
glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 0, mQuadVertices.data());
glEnableVertexAttribArray(positionLoc);
// Load the instance position
glVertexAttribPointer(instancePosLoc, 3, GL_FLOAT, GL_FALSE, 0, mInstances.data());
glEnableVertexAttribArray(instancePosLoc);
// Enable instancing
mVertexAttribDivisorANGLE(instancePosLoc, 1);
// Do the instanced draw
mDrawElementsInstancedANGLE(GL_TRIANGLES, static_cast<GLsizei>(mIndices.size()),
GL_UNSIGNED_SHORT, mIndices.data(),
static_cast<GLsizei>(mInstances.size()) / 3);
ASSERT_GL_NO_ERROR();
checkQuads();
}
void checkQuads()
{
// Check that various pixels are the expected color.
for (unsigned int quadIndex = 0; quadIndex < 4; ++quadIndex)
{
unsigned int baseOffset = quadIndex * 3;
int quadx = static_cast<int>(((mInstances[baseOffset + 0]) * 0.5f + 0.5f) * getWindowWidth());
int quady = static_cast<int>(((mInstances[baseOffset + 1]) * 0.5f + 0.5f) * getWindowHeight());
EXPECT_PIXEL_EQ(quadx, quady, 255, 0, 0, 255);
}
}
// Loaded entry points
PFNGLVERTEXATTRIBDIVISORANGLEPROC mVertexAttribDivisorANGLE;
PFNGLDRAWARRAYSINSTANCEDANGLEPROC mDrawArraysInstancedANGLE;
PFNGLDRAWELEMENTSINSTANCEDANGLEPROC mDrawElementsInstancedANGLE;
// Vertex data
std::vector<GLfloat> mQuadVertices;
std::vector<GLfloat> mNonIndexedVertices;
std::vector<GLfloat> mTexcoords;
std::vector<GLfloat> mInstances;
std::vector<GLushort> mIndices;
const GLfloat quadRadius = 0.30f;
};
class InstancingTestAllConfigs : public InstancingTest
{
protected:
InstancingTestAllConfigs() {}
};
class InstancingTestNo9_3 : public InstancingTest
{
protected:
InstancingTestNo9_3() {}
};
// This test uses a vertex shader with the first attribute (attribute zero) instanced.
// On D3D9 and D3D11 FL9_3, this triggers a special codepath that rearranges the input layout sent to D3D,
// to ensure that slot/stream zero of the input layout doesn't contain per-instance data.
TEST_P(InstancingTestAllConfigs, AttributeZeroInstanced)
{
const std::string vs = SHADER_SOURCE
(
attribute vec3 a_instancePos;
attribute vec3 a_position;
void main()
{
gl_Position = vec4(a_position.xyz + a_instancePos.xyz, 1.0);
}
);
runDrawElementsTest(vs, true);
}
// Same as AttributeZeroInstanced, but attribute zero is not instanced.
// This ensures the general instancing codepath (i.e. without rearranging the input layout) works as expected.
TEST_P(InstancingTestAllConfigs, AttributeZeroNotInstanced)
{
const std::string vs = SHADER_SOURCE
(
attribute vec3 a_position;
attribute vec3 a_instancePos;
void main()
{
gl_Position = vec4(a_position.xyz + a_instancePos.xyz, 1.0);
}
);
runDrawElementsTest(vs, false);
}
// Tests that the "first" parameter to glDrawArraysInstancedANGLE is only an offset into
// the non-instanced vertex attributes.
TEST_P(InstancingTestNo9_3, DrawArraysWithOffset)
{
const std::string vs = SHADER_SOURCE
(
attribute vec3 a_position;
attribute vec3 a_instancePos;
uniform vec3 u_offset;
void main()
{
gl_Position = vec4(a_position.xyz + a_instancePos.xyz + u_offset, 1.0);
}
);
GLuint program = setupDrawArraysTest(vs);
ASSERT_NE(program, 0u);
float offset1[3] = { 0, 0, 0 };
runDrawArraysTest(program, 0, 6, 2, offset1);
float offset2[3] = { 0.0f, 1.0f, 0 };
runDrawArraysTest(program, 6, 6, 2, offset2);
checkQuads();
glDeleteProgram(program);
}
// Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against.
// We test on D3D9 and D3D11 9_3 because they use special codepaths when attribute zero is instanced, unlike D3D11.
ANGLE_INSTANTIATE_TEST(InstancingTestAllConfigs, ES2_D3D9(), ES2_D3D11(), ES2_D3D11_FL9_3());
// TODO(jmadill): Figure out the situation with DrawInstanced on FL 9_3
ANGLE_INSTANTIATE_TEST(InstancingTestNo9_3, ES2_D3D9(), ES2_D3D11());