1 // SwiftShader Software Renderer
3 // Copyright(c) 2005-2012 TransGaming Inc.
5 // All rights reserved. No part of this software may be copied, distributed, transmitted,
6 // transcribed, stored in a retrieval system, translated into any human or computer
7 // language by any means, or disclosed to third parties without the explicit written
8 // agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
9 // or implied, including but not limited to any patent rights, are granted to you.
12 #include "VertexRoutine.hpp"
14 #include "VertexShader.hpp"
17 #include "Renderer.hpp"
18 #include "Constants.hpp"
23 extern bool halfIntegerCoordinates; // Pixel centers are not at integer coordinates
24 extern bool symmetricNormalizedDepth; // [-1, 1] instead of [0, 1]
26 VertexRoutine::VertexRoutine(const VertexProcessor::State &state, const VertexShader *shader) : state(state), shader(shader)
31 VertexRoutine::~VertexRoutine()
35 void VertexRoutine::generate()
37 Function<Void, Pointer<Byte>, Pointer<Byte>, Pointer<Byte>, Pointer<Byte> > function;
39 Pointer<Byte> vertex(function.arg(0));
40 Pointer<Byte> batch(function.arg(1));
41 Pointer<Byte> task(function.arg(2));
42 Pointer<Byte> data(function.arg(3));
44 const bool texldl = state.shaderContainsTexldl;
46 Pointer<Byte> cache = task + OFFSET(VertexTask,vertexCache);
47 Pointer<Byte> vertexCache = cache + OFFSET(VertexCache,vertex);
48 Pointer<Byte> tagCache = cache + OFFSET(VertexCache,tag);
50 UInt vertexCount = *Pointer<UInt>(task + OFFSET(VertexTask,vertexCount));
54 r.constants = *Pointer<Pointer<Byte> >(data + OFFSET(DrawData,constants));
55 if(shader && shader->instanceIdDeclared)
57 r.instanceID = *Pointer<Int>(data + OFFSET(DrawData, instanceID));
62 UInt index = *Pointer<UInt>(batch);
63 UInt tagIndex = index & 0x0000003C;
64 UInt indexQ = !texldl ? UInt(index & 0xFFFFFFFC) : index; // FIXME: TEXLDL hack to have independent LODs, hurts performance.
66 If(*Pointer<UInt>(tagCache + tagIndex) != indexQ)
68 *Pointer<UInt>(tagCache + tagIndex) = indexQ;
75 Pointer<Byte> cacheLine0 = vertexCache + tagIndex * UInt((int)sizeof(Vertex));
76 writeCache(cacheLine0, r);
79 UInt cacheIndex = index & 0x0000003F;
80 Pointer<Byte> cacheLine = vertexCache + cacheIndex * UInt((int)sizeof(Vertex));
81 writeVertex(vertex, cacheLine);
83 vertex += sizeof(Vertex);
84 batch += sizeof(unsigned int);
87 Until(vertexCount == 0)
92 routine = function(L"VertexRoutine_%0.8X", state.shaderID);
95 Routine *VertexRoutine::getRoutine()
100 void VertexRoutine::readInput(Registers &r, UInt &index)
102 for(int i = 0; i < VERTEX_ATTRIBUTES; i++)
104 Pointer<Byte> input = *Pointer<Pointer<Byte> >(r.data + OFFSET(DrawData,input) + sizeof(void*) * i);
105 UInt stride = *Pointer<UInt>(r.data + OFFSET(DrawData,stride) + sizeof(unsigned int) * i);
107 r.v[i] = readStream(r, input, stride, state.input[i], index);
111 void VertexRoutine::computeClipFlags(Registers &r)
113 int pos = state.positionRegister;
116 if(state.preTransformed)
118 Float4 rhw = Float4(1.0f) / r.o[pos].w;
120 Float4 W = *Pointer<Float4>(r.data + OFFSET(DrawData,Wx16)) * Float4(1.0f / 16.0f);
121 Float4 H = *Pointer<Float4>(r.data + OFFSET(DrawData,Hx16)) * Float4(1.0f / 16.0f);
122 Float4 L = *Pointer<Float4>(r.data + OFFSET(DrawData,X0x16)) * Float4(1.0f / 16.0f);
123 Float4 T = *Pointer<Float4>(r.data + OFFSET(DrawData,Y0x16)) * Float4(1.0f / 16.0f);
125 r.o[pos].x = (r.o[pos].x - L) / W * rhw;
126 r.o[pos].y = (r.o[pos].y - T) / H * rhw;
127 r.o[pos].z = r.o[pos].z * rhw;
131 if(state.superSampling)
133 r.o[pos].x = r.o[pos].x + *Pointer<Float4>(r.data + OFFSET(DrawData,XXXX)) * r.o[pos].w;
134 r.o[pos].y = r.o[pos].y + *Pointer<Float4>(r.data + OFFSET(DrawData,YYYY)) * r.o[pos].w;
137 Int4 maxX = CmpLT(r.o[pos].w, r.o[pos].x);
138 Int4 maxY = CmpLT(r.o[pos].w, r.o[pos].y);
139 Int4 maxZ = CmpLT(r.o[pos].w, r.o[pos].z);
141 Int4 minX = CmpNLE(-r.o[pos].w, r.o[pos].x);
142 Int4 minY = CmpNLE(-r.o[pos].w, r.o[pos].y);
143 Int4 minZ = CmpNLE(Float4(0.0f), r.o[pos].z);
147 flags = SignMask(maxX);
148 r.clipFlags = *Pointer<Int>(r.constants + OFFSET(Constants,maxX) + flags * 4); // FIXME: Array indexing
149 flags = SignMask(maxY);
150 r.clipFlags |= *Pointer<Int>(r.constants + OFFSET(Constants,maxY) + flags * 4);
151 flags = SignMask(maxZ);
152 r.clipFlags |= *Pointer<Int>(r.constants + OFFSET(Constants,maxZ) + flags * 4);
153 flags = SignMask(minX);
154 r.clipFlags |= *Pointer<Int>(r.constants + OFFSET(Constants,minX) + flags * 4);
155 flags = SignMask(minY);
156 r.clipFlags |= *Pointer<Int>(r.constants + OFFSET(Constants,minY) + flags * 4);
157 flags = SignMask(minZ);
158 r.clipFlags |= *Pointer<Int>(r.constants + OFFSET(Constants,minZ) + flags * 4);
160 Int4 finiteX = CmpLE(Abs(r.o[pos].x), *Pointer<Float4>(r.constants + OFFSET(Constants,maxPos)));
161 Int4 finiteY = CmpLE(Abs(r.o[pos].y), *Pointer<Float4>(r.constants + OFFSET(Constants,maxPos)));
162 Int4 finiteZ = CmpLE(Abs(r.o[pos].z), *Pointer<Float4>(r.constants + OFFSET(Constants,maxPos)));
164 flags = SignMask(finiteX & finiteY & finiteZ);
165 r.clipFlags |= *Pointer<Int>(r.constants + OFFSET(Constants,fini) + flags * 4);
167 if(state.preTransformed)
169 r.clipFlags &= 0xFBFBFBFB; // Don't clip against far clip plane
173 Vector4f VertexRoutine::readStream(Registers &r, Pointer<Byte> &buffer, UInt &stride, const Stream &stream, const UInt &index)
175 const bool texldl = state.shaderContainsTexldl;
179 Pointer<Byte> source0 = buffer + index * stride;
180 Pointer<Byte> source1 = source0 + (!texldl ? stride : 0);
181 Pointer<Byte> source2 = source1 + (!texldl ? stride : 0);
182 Pointer<Byte> source3 = source2 + (!texldl ? stride : 0);
186 case STREAMTYPE_FLOAT:
188 if(stream.count == 0)
190 // Null stream, all default components
192 else if(stream.count == 1)
194 v.x.x = *Pointer<Float>(source0);
195 v.x.y = *Pointer<Float>(source1);
196 v.x.z = *Pointer<Float>(source2);
197 v.x.w = *Pointer<Float>(source3);
201 v.x = *Pointer<Float4>(source0);
202 v.y = *Pointer<Float4>(source1);
203 v.z = *Pointer<Float4>(source2);
204 v.w = *Pointer<Float4>(source3);
206 transpose4xN(v.x, v.y, v.z, v.w, stream.count);
210 case STREAMTYPE_BYTE:
212 v.x = Float4(*Pointer<Byte4>(source0));
213 v.y = Float4(*Pointer<Byte4>(source1));
214 v.z = Float4(*Pointer<Byte4>(source2));
215 v.w = Float4(*Pointer<Byte4>(source3));
217 transpose4xN(v.x, v.y, v.z, v.w, stream.count);
219 if(stream.normalized)
221 if(stream.count >= 1) v.x *= *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleByte));
222 if(stream.count >= 2) v.y *= *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleByte));
223 if(stream.count >= 3) v.z *= *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleByte));
224 if(stream.count >= 4) v.w *= *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleByte));
228 case STREAMTYPE_SBYTE:
230 v.x = Float4(*Pointer<SByte4>(source0));
231 v.y = Float4(*Pointer<SByte4>(source1));
232 v.z = Float4(*Pointer<SByte4>(source2));
233 v.w = Float4(*Pointer<SByte4>(source3));
235 transpose4xN(v.x, v.y, v.z, v.w, stream.count);
237 if(stream.normalized)
239 if(stream.count >= 1) v.x *= *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleSByte));
240 if(stream.count >= 2) v.y *= *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleSByte));
241 if(stream.count >= 3) v.z *= *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleSByte));
242 if(stream.count >= 4) v.w *= *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleSByte));
246 case STREAMTYPE_COLOR:
248 v.x = Float4(*Pointer<Byte4>(source0)) * *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleByte));
249 v.y = Float4(*Pointer<Byte4>(source1)) * *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleByte));
250 v.z = Float4(*Pointer<Byte4>(source2)) * *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleByte));
251 v.w = Float4(*Pointer<Byte4>(source3)) * *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleByte));
253 transpose4x4(v.x, v.y, v.z, v.w);
261 case STREAMTYPE_SHORT:
263 v.x = Float4(*Pointer<Short4>(source0));
264 v.y = Float4(*Pointer<Short4>(source1));
265 v.z = Float4(*Pointer<Short4>(source2));
266 v.w = Float4(*Pointer<Short4>(source3));
268 transpose4xN(v.x, v.y, v.z, v.w, stream.count);
270 if(stream.normalized)
272 if(stream.count >= 1) v.x *= *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleShort));
273 if(stream.count >= 2) v.y *= *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleShort));
274 if(stream.count >= 3) v.z *= *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleShort));
275 if(stream.count >= 4) v.w *= *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleShort));
279 case STREAMTYPE_USHORT:
281 v.x = Float4(*Pointer<UShort4>(source0));
282 v.y = Float4(*Pointer<UShort4>(source1));
283 v.z = Float4(*Pointer<UShort4>(source2));
284 v.w = Float4(*Pointer<UShort4>(source3));
286 transpose4xN(v.x, v.y, v.z, v.w, stream.count);
288 if(stream.normalized)
290 if(stream.count >= 1) v.x *= *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleUShort));
291 if(stream.count >= 2) v.y *= *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleUShort));
292 if(stream.count >= 3) v.z *= *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleUShort));
293 if(stream.count >= 4) v.w *= *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleUShort));
297 case STREAMTYPE_UDEC3:
303 x = y = z = *Pointer<Int>(source0);
305 v.x.x = Float(x & 0x000003FF);
306 v.x.y = Float(y & 0x000FFC00);
307 v.x.z = Float(z & 0x3FF00000);
313 x = y = z = *Pointer<Int>(source1);
315 v.y.x = Float(x & 0x000003FF);
316 v.y.y = Float(y & 0x000FFC00);
317 v.y.z = Float(z & 0x3FF00000);
323 x = y = z = *Pointer<Int>(source2);
325 v.z.x = Float(x & 0x000003FF);
326 v.z.y = Float(y & 0x000FFC00);
327 v.z.z = Float(z & 0x3FF00000);
333 x = y = z = *Pointer<Int>(source3);
335 v.w.x = Float(x & 0x000003FF);
336 v.w.y = Float(y & 0x000FFC00);
337 v.w.z = Float(z & 0x3FF00000);
340 transpose4x3(v.x, v.y, v.z, v.w);
342 v.y *= Float4(1.0f / 0x00000400);
343 v.z *= Float4(1.0f / 0x00100000);
346 case STREAMTYPE_DEC3N:
352 x = y = z = *Pointer<Int>(source0);
354 v.x.x = Float((x << 22) & 0xFFC00000);
355 v.x.y = Float((y << 12) & 0xFFC00000);
356 v.x.z = Float((z << 2) & 0xFFC00000);
362 x = y = z = *Pointer<Int>(source1);
364 v.y.x = Float((x << 22) & 0xFFC00000);
365 v.y.y = Float((y << 12) & 0xFFC00000);
366 v.y.z = Float((z << 2) & 0xFFC00000);
372 x = y = z = *Pointer<Int>(source2);
374 v.z.x = Float((x << 22) & 0xFFC00000);
375 v.z.y = Float((y << 12) & 0xFFC00000);
376 v.z.z = Float((z << 2) & 0xFFC00000);
382 x = y = z = *Pointer<Int>(source3);
384 v.w.x = Float((x << 22) & 0xFFC00000);
385 v.w.y = Float((y << 12) & 0xFFC00000);
386 v.w.z = Float((z << 2) & 0xFFC00000);
389 transpose4x3(v.x, v.y, v.z, v.w);
391 v.x *= Float4(1.0f / 0x00400000 / 511.0f);
392 v.y *= Float4(1.0f / 0x00400000 / 511.0f);
393 v.z *= Float4(1.0f / 0x00400000 / 511.0f);
396 case STREAMTYPE_FIXED:
398 v.x = Float4(*Pointer<Int4>(source0)) * *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleFixed));
399 v.y = Float4(*Pointer<Int4>(source1)) * *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleFixed));
400 v.z = Float4(*Pointer<Int4>(source2)) * *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleFixed));
401 v.w = Float4(*Pointer<Int4>(source3)) * *Pointer<Float4>(r.constants + OFFSET(Constants,unscaleFixed));
403 transpose4xN(v.x, v.y, v.z, v.w, stream.count);
406 case STREAMTYPE_HALF:
408 if(stream.count >= 1)
410 UShort x0 = *Pointer<UShort>(source0 + 0);
411 UShort x1 = *Pointer<UShort>(source1 + 0);
412 UShort x2 = *Pointer<UShort>(source2 + 0);
413 UShort x3 = *Pointer<UShort>(source3 + 0);
415 v.x.x = *Pointer<Float>(r.constants + OFFSET(Constants,half2float) + Int(x0) * 4);
416 v.x.y = *Pointer<Float>(r.constants + OFFSET(Constants,half2float) + Int(x1) * 4);
417 v.x.z = *Pointer<Float>(r.constants + OFFSET(Constants,half2float) + Int(x2) * 4);
418 v.x.w = *Pointer<Float>(r.constants + OFFSET(Constants,half2float) + Int(x3) * 4);
421 if(stream.count >= 2)
423 UShort y0 = *Pointer<UShort>(source0 + 2);
424 UShort y1 = *Pointer<UShort>(source1 + 2);
425 UShort y2 = *Pointer<UShort>(source2 + 2);
426 UShort y3 = *Pointer<UShort>(source3 + 2);
428 v.y.x = *Pointer<Float>(r.constants + OFFSET(Constants,half2float) + Int(y0) * 4);
429 v.y.y = *Pointer<Float>(r.constants + OFFSET(Constants,half2float) + Int(y1) * 4);
430 v.y.z = *Pointer<Float>(r.constants + OFFSET(Constants,half2float) + Int(y2) * 4);
431 v.y.w = *Pointer<Float>(r.constants + OFFSET(Constants,half2float) + Int(y3) * 4);
434 if(stream.count >= 3)
436 UShort z0 = *Pointer<UShort>(source0 + 4);
437 UShort z1 = *Pointer<UShort>(source1 + 4);
438 UShort z2 = *Pointer<UShort>(source2 + 4);
439 UShort z3 = *Pointer<UShort>(source3 + 4);
441 v.z.x = *Pointer<Float>(r.constants + OFFSET(Constants,half2float) + Int(z0) * 4);
442 v.z.y = *Pointer<Float>(r.constants + OFFSET(Constants,half2float) + Int(z1) * 4);
443 v.z.z = *Pointer<Float>(r.constants + OFFSET(Constants,half2float) + Int(z2) * 4);
444 v.z.w = *Pointer<Float>(r.constants + OFFSET(Constants,half2float) + Int(z3) * 4);
447 if(stream.count >= 4)
449 UShort w0 = *Pointer<UShort>(source0 + 6);
450 UShort w1 = *Pointer<UShort>(source1 + 6);
451 UShort w2 = *Pointer<UShort>(source2 + 6);
452 UShort w3 = *Pointer<UShort>(source3 + 6);
454 v.w.x = *Pointer<Float>(r.constants + OFFSET(Constants,half2float) + Int(w0) * 4);
455 v.w.y = *Pointer<Float>(r.constants + OFFSET(Constants,half2float) + Int(w1) * 4);
456 v.w.z = *Pointer<Float>(r.constants + OFFSET(Constants,half2float) + Int(w2) * 4);
457 v.w.w = *Pointer<Float>(r.constants + OFFSET(Constants,half2float) + Int(w3) * 4);
461 case STREAMTYPE_INDICES:
463 v.x.x = *Pointer<Float>(source0);
464 v.x.y = *Pointer<Float>(source1);
465 v.x.z = *Pointer<Float>(source2);
466 v.x.w = *Pointer<Float>(source3);
473 if(stream.count < 1) v.x = Float4(0.0f);
474 if(stream.count < 2) v.y = Float4(0.0f);
475 if(stream.count < 3) v.z = Float4(0.0f);
476 if(stream.count < 4) v.w = Float4(1.0f);
481 void VertexRoutine::postTransform(Registers &r)
483 int pos = state.positionRegister;
485 if(!halfIntegerCoordinates)
487 r.o[pos].x = r.o[pos].x + *Pointer<Float4>(r.data + OFFSET(DrawData,halfPixelX)) * r.o[pos].w;
488 r.o[pos].y = r.o[pos].y + *Pointer<Float4>(r.data + OFFSET(DrawData,halfPixelY)) * r.o[pos].w;
491 if(symmetricNormalizedDepth && !state.fixedFunction)
493 r.o[pos].z = (r.o[pos].z + r.o[pos].w) * Float4(0.5f);
497 void VertexRoutine::writeCache(Pointer<Byte> &cacheLine, Registers &r)
501 for(int i = 0; i < 12; i++)
503 if(state.output[i].write)
510 if(state.output[i].xClamp)
512 v.x = Max(v.x, Float4(0.0f));
513 v.x = Min(v.x, Float4(1.0f));
516 if(state.output[i].yClamp)
518 v.y = Max(v.y, Float4(0.0f));
519 v.y = Min(v.y, Float4(1.0f));
522 if(state.output[i].zClamp)
524 v.z = Max(v.z, Float4(0.0f));
525 v.z = Min(v.z, Float4(1.0f));
528 if(state.output[i].wClamp)
530 v.w = Max(v.w, Float4(0.0f));
531 v.w = Min(v.w, Float4(1.0f));
534 if(state.output[i].write == 0x01)
536 *Pointer<Float>(cacheLine + OFFSET(Vertex,v[i]) + sizeof(Vertex) * 0) = v.x.x;
537 *Pointer<Float>(cacheLine + OFFSET(Vertex,v[i]) + sizeof(Vertex) * 1) = v.x.y;
538 *Pointer<Float>(cacheLine + OFFSET(Vertex,v[i]) + sizeof(Vertex) * 2) = v.x.z;
539 *Pointer<Float>(cacheLine + OFFSET(Vertex,v[i]) + sizeof(Vertex) * 3) = v.x.w;
543 if(state.output[i].write == 0x02)
545 transpose2x4(v.x, v.y, v.z, v.w);
549 transpose4x4(v.x, v.y, v.z, v.w);
552 *Pointer<Float4>(cacheLine + OFFSET(Vertex,v[i]) + sizeof(Vertex) * 0, 16) = v.x;
553 *Pointer<Float4>(cacheLine + OFFSET(Vertex,v[i]) + sizeof(Vertex) * 1, 16) = v.y;
554 *Pointer<Float4>(cacheLine + OFFSET(Vertex,v[i]) + sizeof(Vertex) * 2, 16) = v.z;
555 *Pointer<Float4>(cacheLine + OFFSET(Vertex,v[i]) + sizeof(Vertex) * 3, 16) = v.w;
560 *Pointer<Int>(cacheLine + OFFSET(Vertex,clipFlags) + sizeof(Vertex) * 0) = (r.clipFlags >> 0) & 0x0000000FF; // FIXME: unsigned char Vertex::clipFlags
561 *Pointer<Int>(cacheLine + OFFSET(Vertex,clipFlags) + sizeof(Vertex) * 1) = (r.clipFlags >> 8) & 0x0000000FF;
562 *Pointer<Int>(cacheLine + OFFSET(Vertex,clipFlags) + sizeof(Vertex) * 2) = (r.clipFlags >> 16) & 0x0000000FF;
563 *Pointer<Int>(cacheLine + OFFSET(Vertex,clipFlags) + sizeof(Vertex) * 3) = (r.clipFlags >> 24) & 0x0000000FF;
565 int pos = state.positionRegister;
572 Float4 w = As<Float4>(As<Int4>(v.w) | (As<Int4>(CmpEQ(v.w, Float4(0.0f))) & As<Int4>(Float4(1.0f))));
573 Float4 rhw = Float4(1.0f) / w;
575 v.x = As<Float4>(RoundInt(*Pointer<Float4>(r.data + OFFSET(DrawData,X0x16)) + v.x * rhw * *Pointer<Float4>(r.data + OFFSET(DrawData,Wx16))));
576 v.y = As<Float4>(RoundInt(*Pointer<Float4>(r.data + OFFSET(DrawData,Y0x16)) + v.y * rhw * *Pointer<Float4>(r.data + OFFSET(DrawData,Hx16))));
580 transpose4x4(v.x, v.y, v.z, v.w);
582 *Pointer<Float4>(cacheLine + OFFSET(Vertex,X) + sizeof(Vertex) * 0, 16) = v.x;
583 *Pointer<Float4>(cacheLine + OFFSET(Vertex,X) + sizeof(Vertex) * 1, 16) = v.y;
584 *Pointer<Float4>(cacheLine + OFFSET(Vertex,X) + sizeof(Vertex) * 2, 16) = v.z;
585 *Pointer<Float4>(cacheLine + OFFSET(Vertex,X) + sizeof(Vertex) * 3, 16) = v.w;
588 void VertexRoutine::writeVertex(Pointer<Byte> &vertex, Pointer<Byte> &cache)
590 for(int i = 0; i < 12; i++)
592 if(state.output[i].write)
594 *Pointer<Float4>(vertex + OFFSET(Vertex,v[i])) = *Pointer<Float4>(cache + OFFSET(Vertex,v[i]));
598 *Pointer<Int>(vertex + OFFSET(Vertex,clipFlags)) = *Pointer<Int>(cache + OFFSET(Vertex,clipFlags));
599 *Pointer<Float4>(vertex + OFFSET(Vertex,X)) = *Pointer<Float4>(cache + OFFSET(Vertex,X));