summaryrefslogtreecommitdiff
path: root/src/pixelflinger2/raster.cpp
blob: 74fdc19cf901246302e3b599ab6c7596b6aeb66f (plain)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/**
 **
 ** Copyright 2010, 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.
 */

#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <string.h>
#include <stdio.h>

#include "pixelflinger2.h"
#include "src/mesa/main/mtypes.h"

#ifdef SHADER_SOA
static struct tgsi_exec_machine machine;
#endif

static inline void LerpVector4(const Vector4 * a, const Vector4 * b,
                               const VectorComp_t x, Vector4 * d) __attribute__((always_inline));
static inline void LerpVector4(const Vector4 * a, const Vector4 * b,
                               const VectorComp_t x, Vector4 * d)
{
   assert(a != d && b != d);
   //d = (b - a) * x + a;
   (*d) = (*b);
   (*d) -= (*a);
   (*d) *= x;
   (*d) += (*a);
}

static inline void InterpolateVertex(const VertexOutput * a, const VertexOutput * b, const VectorComp_t x,
                                     VertexOutput * v, const unsigned varyingCount)
{
   LerpVector4(&a->position, &b->position, x, &v->position);
   for (unsigned i = 0; i < varyingCount; i++)
      LerpVector4(a->varyings + i, b->varyings + i, x, v->varyings + i);
   LerpVector4(&a->frontFacingPointCoord, &b->frontFacingPointCoord,
               x, &v->frontFacingPointCoord); // gl_PointCoord
   v->frontFacingPointCoord.y = a->frontFacingPointCoord.y; // gl_FrontFacing not interpolated

}

void GGLProcessVertex(const gl_shader_program * program, const VertexInput * input,
                         VertexOutput * output, const float (*constants)[4])
{
   ShaderFunction_t function = (ShaderFunction_t)program->_LinkedShaders[MESA_SHADER_VERTEX]->function;
   function(input, output, constants);
}

static void ProcessVertex(const GGLInterface * iface, const VertexInput * input,
                          VertexOutput * output)
{
   GGL_GET_CONST_CONTEXT(ctx, iface);

//#if !USE_LLVM_TEXTURE_SAMPLER
//    extern const GGLContext * textureGGLContext;
//    textureGGLContext = ctx;
//#endif
//

//   memcpy(ctx->glCtx->CurrentProgram->ValuesVertexInput, input, sizeof(*input));
//   ctx->glCtx->CurrentProgram->_LinkedShaders[MESA_SHADER_VERTEX]->function();
//   memcpy(output, ctx->glCtx->CurrentProgram->ValuesVertexOutput, sizeof(*output));
   
   GGLProcessVertex(ctx->CurrentProgram, input, output, ctx->CurrentProgram->ValuesUniform);
//   const Vector4 * constants = (Vector4 *)
//    ctx->glCtx->Shader.CurrentProgram->VertexProgram->Parameters->ParameterValues;
//	ctx->glCtx->Shader.CurrentProgram->GLVMVP->function(input, output, constants);
//
//#if !USE_LLVM_TEXTURE_SAMPLER
//    textureGGLContext = NULL;
//#endif
}

static void RasterTrapezoid(const GGLInterface * iface, const VertexOutput * tl,
                            const VertexOutput * tr, const VertexOutput * bl,
                            const VertexOutput * br)
{
   GGL_GET_CONST_CONTEXT(ctx, iface);

   assert(tl->position.x <= tr->position.x && bl->position.x <= br->position.x);
   assert(tl->position.y <= bl->position.y && tr->position.y <= br->position.y);
   assert(fabs(tl->position.y - tr->position.y) < 1 && fabs(bl->position.y - br->position.y) < 1);

   const unsigned width = ctx->frameSurface.width, height = ctx->frameSurface.height;
   const unsigned varyingCount = ctx->CurrentProgram->VaryingSlots;


   // tlv-trv and blv-brv are parallel and horizontal
   VertexOutput tlv(*tl), trv(*tr), blv(*bl), brv(*br);
   VertexOutput tmp;

   // vertically clip
   if ((int)tlv.position.y < 0) {
      InterpolateVertex(&tlv, &blv, (0 - tlv.position.y) / (blv.position.y - tlv.position.y),
                        &tmp, varyingCount);
      tlv = tmp;
   }
   if ((int)trv.position.y < 0) {
      InterpolateVertex(&trv, &brv, (0 - trv.position.y) / (brv.position.y - trv.position.y),
                        &tmp, varyingCount);
      trv = tmp;
   }
   if ((int)blv.position.y >= (int)height) {
      InterpolateVertex(&tlv, &blv, (height - 1 - tlv.position.y) / (blv.position.y - tlv.position.y),
                        &tmp, varyingCount);
      blv = tmp;
   }
   if ((int)brv.position.y >= (int)height) {
      InterpolateVertex(&trv, &brv, (height - 1 - trv.position.y) / (brv.position.y - trv.position.y),
                        &tmp, varyingCount);
      brv = tmp;
   }

   // horizontally clip
   if ((int)tlv.position.x < 0) {
      InterpolateVertex(&tlv, &trv, (0 - tlv.position.x) / (trv.position.x - tlv.position.x),
                        &tmp, varyingCount);
      tlv = tmp;
   }
   if ((int)blv.position.x < 0) {
      InterpolateVertex(&blv, &brv, (0 - blv.position.x) / (brv.position.x - blv.position.x),
                        &tmp, varyingCount);
      blv = tmp;
   }
   if ((int)trv.position.x >= (int)width) {
      InterpolateVertex(&tlv, &trv, (width - 1 - tlv.position.x) / (trv.position.x - tlv.position.x),
                        &tmp, varyingCount);
      trv = tmp;
   }
   if ((int)brv.position.x >= (int)width) {
      InterpolateVertex(&blv, &brv, (width - 1 - blv.position.x) / (brv.position.x - blv.position.x),
                        &tmp, varyingCount);
      brv = tmp;
   }

   const unsigned int startY = tlv.position.y;
   const unsigned int endY = blv.position.y;

   if (endY < startY)
      return;

   const VectorComp_t yDistInv = VectorComp_t_CTR(1.0f / (endY - startY));

   // bV and cV are left and right vertices on a horizontal line in quad
   // bDx and cDx are iterators from tlv to blv, trv to brv for bV and cV

   VertexOutput bV(tlv), cV(trv);
   VertexOutput bDx(blv), cDx(brv);

   for (unsigned i = 0; i < varyingCount; i++) {
      bDx.varyings[i] -= tlv.varyings[i];
      bDx.varyings[i] *= yDistInv;

      cDx.varyings[i] -= trv.varyings[i];
      cDx.varyings[i] *= yDistInv;
   }

   bDx.position -= tlv.position;
   bDx.position *= yDistInv;

   cDx.position -= trv.position;
   cDx.position *= yDistInv;

   bDx.frontFacingPointCoord -= tlv.frontFacingPointCoord; // gl_PointCoord
   bDx.frontFacingPointCoord *= yDistInv;
   bDx.frontFacingPointCoord.y = VectorComp_t_Zero; // gl_FrontFacing not interpolated
   cDx.frontFacingPointCoord -= trv.frontFacingPointCoord; // gl_PointCoord
   cDx.frontFacingPointCoord *= yDistInv;
   cDx.frontFacingPointCoord.y = VectorComp_t_Zero; // gl_FrontFacing not interpolated

   for (unsigned y = startY; y <= endY; y++) {
      iface->ScanLine(iface, &bV, &cV);
      for (unsigned i = 0; i < varyingCount; i++) {
         bV.varyings[i] += bDx.varyings[i];
         cV.varyings[i] += cDx.varyings[i];
      }
      bV.position += bDx.position;
      cV.position += cDx.position;
      bV.frontFacingPointCoord += bDx.frontFacingPointCoord;
      cV.frontFacingPointCoord += cDx.frontFacingPointCoord;
   }
}

static void RasterTriangle(const GGLInterface * iface, const VertexOutput * v1,
                           const VertexOutput * v2, const VertexOutput * v3)
{
   GGL_GET_CONST_CONTEXT(ctx, iface);
   const unsigned varyingCount = ctx->CurrentProgram->VaryingSlots;
   const unsigned height = ctx->frameSurface.height;
   const VertexOutput * a = v1, * b = v2, * d = v3;
   //abc is a triangle, bcd is another triangle, they share bc as horizontal edge
   //c is between a and d, xy is screen coord

   //first sort 3 vertices by MIN y first
   if (v2->position.y < v1->position.y) {
      a = v2;
      b = v1;
   }
   if (v3->position.y < a->position.y) {
      d = b;
      b = a;
      a = v3;
   } else if (v3->position.y < b->position.y) {
      d = b;
      b = v3;
   }

   assert(a->position.y <= b->position.y && b->position.y <= d->position.y);

   VertexOutput cVertex;
   const VertexOutput* c = &cVertex;

   const VectorComp_t cLerp = (b->position.y - a->position.y) /
                              MAX2(VectorComp_t_One, (d->position.y - a->position.y));
   // create 4th vertex, same y as b to form two triangles/trapezoids sharing horizontal edge
   InterpolateVertex(a, d, cLerp, &cVertex, varyingCount);

   if (c->position.x < b->position.x) {
      const VertexOutput * tmp = c;
      c = b;
      b = tmp;
   }

   if ((int)a->position.y < (int)height && (int)b->position.y >= 0)
      RasterTrapezoid(iface, a, a, b, c);
   //b->position.y += VectorComp_t_One;
   //c->position.y += VectorComp_t_One;
   if ((int)b->position.y < (int)height && (int)d->position.y >= 0)
      RasterTrapezoid(iface, b, c, d, d);
}

static void DrawTriangle(const GGLInterface * iface, const VertexInput * vin1,
                         const VertexInput * vin2, const VertexInput * vin3)
{
   GGL_GET_CONST_CONTEXT(ctx, iface);

   VertexOutput vouts[3];
   VertexOutput * v1 = vouts + 0, * v2 = vouts + 1, * v3 = vouts + 2;

#ifdef SHADER_SOA
   assert(0); // not implemented
#endif

   iface->ProcessVertex(iface, vin1, v1);
   iface->ProcessVertex(iface, vin2, v2);
   iface->ProcessVertex(iface, vin3, v3);

   v1->position /= v1->position.w;
   v2->position /= v2->position.w;
   v3->position /= v3->position.w;

   iface->ViewportTransform(iface, &v1->position);
   iface->ViewportTransform(iface, &v2->position);
   iface->ViewportTransform(iface, &v3->position);

   VectorComp_t area;
   area = v1->position.x * v2->position.y - v2->position.x * v1->position.y;
   area += v2->position.x * v3->position.y - v3->position.x * v2->position.y;
   area += v3->position.x * v1->position.y - v1->position.x * v3->position.y;
   area *= 0.5f;

   if (GL_CCW == ctx->cullState.frontFace + GL_CW)
      (unsigned &)area ^= 0x80000000;

   if (ctx->cullState.enable) {
      switch (ctx->cullState.cullFace + GL_FRONT) {
      case GL_FRONT:
         if (!((unsigned &)area & 0x80000000)) // +ve, front facing
            return;
         break;
      case GL_BACK:
         if ((unsigned &)area & 0x80000000) // -ve, back facing
            return;
         break;
      case GL_FRONT_AND_BACK:
         return;
      default:
         assert(0);
      }
   }

   v1->frontFacingPointCoord.y = v2->frontFacingPointCoord.y =
                                    v3->frontFacingPointCoord.y = !((unsigned &)area & 0x80000000) ?
                                                                  VectorComp_t_One : VectorComp_t_Zero;

   iface->StencilSelect(iface, ((unsigned &)area & 0x80000000) ? GL_BACK : GL_FRONT);

//    if (0)
//    {
//        GGLContext * ctx =(GGLContext *)iface;
//        for (unsigned sampler = 0; sampler < GGL_MAXCOMBINEDTEXTUREIMAGEUNITS; sampler++)
//        {
//            if (!((1 << sampler) & ctx->glCtx->Shader.CurrentProgram->FragmentProgram->SamplersUsed))
//                continue;
//            const GGLTexture * texture = ctx->textureState.textures + sampler;
//            int level = texture->width * texture->height / (area * 2) - 4;
//            assert(texture->levels);
//            ctx->textureState.textureData[sampler] = texture->levels[0];
//            ctx->textureState.textureDimensions[sampler * 2] = texture->width;
//            ctx->textureState.textureDimensions[sampler * 2 + 1] = texture->height;
//            for (unsigned i = 1; i < texture->levelCount && i <= level; i++)
//            {
//                ctx->textureState.textureData[sampler] = texture->levels[i];
//                ctx->textureState.textureDimensions[sampler * 2] += 1;
//                ctx->textureState.textureDimensions[sampler * 2] /= 2;
//                ctx->textureState.textureDimensions[sampler * 2 + 1] += 1;
//                ctx->textureState.textureDimensions[sampler * 2 + 1] /= 2;
//            }
//        }
//    }

   // TODO DXL view frustum clipping
   iface->RasterTriangle(iface, v1, v2, v3);

}

static void PickRaster(GGLInterface * iface)
{
   iface->ProcessVertex = ProcessVertex;
   iface->DrawTriangle = DrawTriangle;
   iface->RasterTriangle = RasterTriangle;
   iface->RasterTrapezoid = RasterTrapezoid;
}

static void ViewportTransform(const GGLInterface * iface, Vector4 * v)
{
   GGL_GET_CONST_CONTEXT(ctx, iface);
   v->x = v->x * ctx->viewport.w + ctx->viewport.x;
   v->y = v->y * ctx->viewport.h + ctx->viewport.y;
   v->z = v->z * ctx->viewport.f + ctx->viewport.n;
}


void InitializeRasterFunctions(GGLInterface * iface)
{
   GGL_GET_CONTEXT(ctx, iface);
   ctx->PickRaster = PickRaster;
   iface->ViewportTransform = ViewportTransform;
}