liyujie
2025-08-28 786ff4f4ca2374bdd9177f2e24b503d43e7a3b93
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
/*-------------------------------------------------------------------------
 * drawElements Quality Program OpenGL (ES) Module
 * -----------------------------------------------
 *
 * Copyright 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *//*!
 * \file
 * \brief Fragment operation test utilities.
 *//*--------------------------------------------------------------------*/
 
#include "glsFragmentOpUtil.hpp"
#include "gluRenderContext.hpp"
#include "gluShaderProgram.hpp"
#include "gluDrawUtil.hpp"
#include "glwFunctions.hpp"
#include "glwEnums.hpp"
 
namespace deqp
{
namespace gls
{
namespace FragmentOpUtil
{
 
template<typename T>
inline T triQuadInterpolate (const T values[4], float xFactor, float yFactor)
{
   if (xFactor + yFactor < 1.0f)
       return values[0] + (values[2]-values[0])*xFactor        + (values[1]-values[0])*yFactor;
   else
       return values[3] + (values[1]-values[3])*(1.0f-xFactor)    + (values[2]-values[3])*(1.0f-yFactor);
}
 
// GLSL ES 1.0 shaders
static const char* s_glsl1VertSrc =
   "attribute highp vec4 a_position;\n"
   "attribute mediump vec4 a_color;\n"
   "varying mediump vec4 v_color;\n"
   "void main()\n"
   "{\n"
   "    gl_Position = a_position;\n"
   "    v_color = a_color;\n"
   "}\n";
static const char* s_glsl1FragSrc =
   "varying mediump vec4 v_color;\n"
   "void main()\n"
   "{\n"
   "    gl_FragColor = v_color;\n"
   "}\n";
 
// GLSL ES 3.0 shaders
static const char* s_glsl3VertSrc =
   "#version 300 es\n"
   "in highp vec4 a_position;\n"
   "in mediump vec4 a_color;\n"
   "out mediump vec4 v_color;\n"
   "void main()\n"
   "{\n"
   "    gl_Position = a_position;\n"
   "    v_color = a_color;\n"
   "}\n";
static const char* s_glsl3FragSrc =
   "#version 300 es\n"
   "in mediump vec4 v_color;\n"
   "layout(location = 0) out mediump vec4 o_color;\n"
   "void main()\n"
   "{\n"
   "    o_color = v_color;\n"
   "}\n";
 
// GLSL 3.3 shaders
static const char* s_glsl33VertSrc =
   "#version 330 core\n"
   "in vec4 a_position;\n"
   "in vec4 a_color;\n"
   "in vec4 a_color1;\n"
   "out vec4 v_color;\n"
   "out vec4 v_color1;\n"
   "void main()\n"
   "{\n"
   "    gl_Position    = a_position;\n"
   "    v_color        = a_color;\n"
   "    v_color1    = a_color1;\n"
   "}\n";
static const char* s_glsl33FragSrc =
   "#version 330 core\n"
   "in vec4 v_color;\n"
   "in vec4 v_color1;\n"
   "layout(location = 0, index = 0) out vec4 o_color;\n"
   "layout(location = 0, index = 1) out vec4 o_color1;\n"
   "void main()\n"
   "{\n"
   "    o_color  = v_color;\n"
   "    o_color1 = v_color1;\n"
   "}\n";
 
static const char* getVertSrc (glu::GLSLVersion glslVersion)
{
   if (glslVersion == glu::GLSL_VERSION_100_ES)
       return s_glsl1VertSrc;
   else if (glslVersion == glu::GLSL_VERSION_300_ES)
       return s_glsl3VertSrc;
   else if (glslVersion == glu::GLSL_VERSION_330)
       return s_glsl33VertSrc;
 
   DE_ASSERT(DE_FALSE);
   return 0;
}
 
static const char* getFragSrc (glu::GLSLVersion glslVersion)
{
   if (glslVersion == glu::GLSL_VERSION_100_ES)
       return s_glsl1FragSrc;
   else if (glslVersion == glu::GLSL_VERSION_300_ES)
       return s_glsl3FragSrc;
   else if (glslVersion == glu::GLSL_VERSION_330)
       return s_glsl33FragSrc;
 
   DE_ASSERT(DE_FALSE);
   return 0;
}
 
QuadRenderer::QuadRenderer (const glu::RenderContext& context, glu::GLSLVersion glslVersion)
   : m_context            (context)
   , m_program            (DE_NULL)
   , m_positionLoc        (0)
   , m_colorLoc        (-1)
   , m_color1Loc        (-1)
   , m_blendFuncExt    (!glu::glslVersionIsES(glslVersion) && (glslVersion >= glu::GLSL_VERSION_330))
{
   DE_ASSERT(glslVersion == glu::GLSL_VERSION_100_ES ||
             glslVersion == glu::GLSL_VERSION_300_ES ||
             glslVersion == glu::GLSL_VERSION_330);
 
   const glw::Functions&    gl        = context.getFunctions();
   const char*                vertSrc    = getVertSrc(glslVersion);
   const char*                fragSrc    = getFragSrc(glslVersion);
 
   m_program = new glu::ShaderProgram(m_context, glu::makeVtxFragSources(vertSrc, fragSrc));
   if (!m_program->isOk())
   {
       delete m_program;
       throw tcu::TestError("Failed to compile program", DE_NULL, __FILE__, __LINE__);
   }
 
   m_positionLoc    = gl.getAttribLocation(m_program->getProgram(), "a_position");
   m_colorLoc        = gl.getAttribLocation(m_program->getProgram(), "a_color");
 
   if (m_blendFuncExt)
       m_color1Loc = gl.getAttribLocation(m_program->getProgram(), "a_color1");
 
   if (m_positionLoc < 0 || m_colorLoc < 0 || (m_blendFuncExt && m_color1Loc < 0))
   {
       delete m_program;
       throw tcu::TestError("Invalid attribute locations", DE_NULL, __FILE__, __LINE__);
   }
}
 
QuadRenderer::~QuadRenderer (void)
{
   delete m_program;
}
 
void QuadRenderer::render (const Quad& quad) const
{
   const float position[] =
   {
       quad.posA.x(), quad.posA.y(), quad.depth[0], 1.0f,
       quad.posA.x(), quad.posB.y(), quad.depth[1], 1.0f,
       quad.posB.x(), quad.posA.y(), quad.depth[2], 1.0f,
       quad.posB.x(), quad.posB.y(), quad.depth[3], 1.0f
   };
   const deUint8 indices[] = { 0, 2, 1, 1, 2, 3 };
 
   DE_STATIC_ASSERT(sizeof(tcu::Vec4) == sizeof(float)*4);
   DE_STATIC_ASSERT(sizeof(quad.color) == sizeof(float)*4*4);
   DE_STATIC_ASSERT(sizeof(quad.color1) == sizeof(float)*4*4);
 
   std::vector<glu::VertexArrayBinding> vertexArrays;
 
   vertexArrays.push_back(glu::va::Float(m_positionLoc,    4, 4, 0, &position[0]));
   vertexArrays.push_back(glu::va::Float(m_colorLoc,        4, 4, 0, (const float*)&quad.color[0]));
 
   if (m_blendFuncExt)
       vertexArrays.push_back(glu::va::Float(m_color1Loc,    4, 4, 0, (const float*)&quad.color1[0]));
 
   m_context.getFunctions().useProgram(m_program->getProgram());
   glu::draw(m_context, m_program->getProgram(),
             (int)vertexArrays.size(), &vertexArrays[0],
             glu::pr::Triangles(DE_LENGTH_OF_ARRAY(indices), &indices[0]));
}
 
ReferenceQuadRenderer::ReferenceQuadRenderer (void)
   : m_fragmentBufferSize(0)
{
   for (int i = 0; i < DE_LENGTH_OF_ARRAY(m_fragmentDepths); i++)
       m_fragmentDepths[i] = 0.0f;
}
 
void ReferenceQuadRenderer::flushFragmentBuffer (const rr::MultisamplePixelBufferAccess&    colorBuffer,
                                                const rr::MultisamplePixelBufferAccess&    depthBuffer,
                                                const rr::MultisamplePixelBufferAccess&    stencilBuffer,
                                                rr::FaceType                                faceType,
                                                const rr::FragmentOperationState&            state)
{
   m_fragmentProcessor.render(colorBuffer, depthBuffer, stencilBuffer, &m_fragmentBuffer[0], m_fragmentBufferSize, faceType, state);
   m_fragmentBufferSize = 0;
}
 
void ReferenceQuadRenderer::render (const tcu::PixelBufferAccess&            colorBuffer,
                                   const tcu::PixelBufferAccess&            depthBuffer,
                                   const tcu::PixelBufferAccess&            stencilBuffer,
                                   const IntegerQuad&                        quad,
                                   const rr::FragmentOperationState&        state)
{
   bool            flipX            = quad.posA.x() > quad.posB.x();
   bool            flipY            = quad.posA.y() > quad.posB.y();
   rr::FaceType    faceType        = flipX == flipY ? rr::FACETYPE_FRONT : rr::FACETYPE_BACK;
   int                xFirst            = flipX ? quad.posB.x() : quad.posA.x();
   int                xLast            = flipX ? quad.posA.x() : quad.posB.x();
   int                yFirst            = flipY ? quad.posB.y() : quad.posA.y();
   int                yLast            = flipY ? quad.posA.y() : quad.posB.y();
   float            width            = (float)(xLast - xFirst + 1);
   float            height            = (float)(yLast - yFirst + 1);
 
   for (int y = yFirst; y <= yLast; y++)
   {
       // Interpolation factor for y.
       float yRatio = (0.5f + (float)(y - yFirst)) / height;
       if (flipY)
           yRatio = 1.0f - yRatio;
 
       for (int x = xFirst; x <= xLast; x++)
       {
           // Interpolation factor for x.
           float xRatio = (0.5f + (float)(x - xFirst)) / width;
           if (flipX)
               xRatio = 1.0f - xRatio;
 
           tcu::Vec4    color    = triQuadInterpolate(quad.color, xRatio, yRatio);
           tcu::Vec4    color1    = triQuadInterpolate(quad.color1, xRatio, yRatio);
           float        depth    = triQuadInterpolate(quad.depth, xRatio, yRatio);
 
           // Interpolated color and depth.
 
           DE_STATIC_ASSERT(MAX_FRAGMENT_BUFFER_SIZE == DE_LENGTH_OF_ARRAY(m_fragmentBuffer));
 
           if (m_fragmentBufferSize >= MAX_FRAGMENT_BUFFER_SIZE)
               flushFragmentBuffer(rr::MultisamplePixelBufferAccess::fromMultisampleAccess(colorBuffer),
                                   rr::MultisamplePixelBufferAccess::fromMultisampleAccess(depthBuffer),
                                   rr::MultisamplePixelBufferAccess::fromMultisampleAccess(stencilBuffer), faceType, state);
 
           m_fragmentDepths[m_fragmentBufferSize] = depth;
           m_fragmentBuffer[m_fragmentBufferSize] = rr::Fragment(tcu::IVec2(x, y), rr::GenericVec4(color), rr::GenericVec4(color1), 1u /* coverage mask */, &m_fragmentDepths[m_fragmentBufferSize]);
           m_fragmentBufferSize++;
       }
   }
 
   flushFragmentBuffer(rr::MultisamplePixelBufferAccess::fromMultisampleAccess(colorBuffer),
                       rr::MultisamplePixelBufferAccess::fromMultisampleAccess(depthBuffer),
                       rr::MultisamplePixelBufferAccess::fromMultisampleAccess(stencilBuffer), faceType, state);
}
 
tcu::PixelBufferAccess getMultisampleAccess(const tcu::PixelBufferAccess& original)
{
   return tcu::PixelBufferAccess(original.getFormat(),
                                 1,
                                 original.getWidth(),
                                 original.getHeight(),
                                 original.getFormat().getPixelSize(),
                                 original.getRowPitch(),
                                 original.getDataPtr());
}
 
tcu::ConstPixelBufferAccess getMultisampleAccess(const tcu::ConstPixelBufferAccess& original)
{
   return tcu::ConstPixelBufferAccess(original.getFormat(),
                                      1,
                                      original.getWidth(),
                                      original.getHeight(),
                                      original.getFormat().getPixelSize(),
                                      original.getRowPitch(),
                                      original.getDataPtr());
}
 
} // FragmentOpUtil
} // gls
} // deqp