John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1 | // SwiftShader Software Renderer |
| 2 | // |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3 | // Copyright(c) 2005-2012 TransGaming Inc. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4 | // |
| 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. |
| 10 | // |
| 11 | |
| 12 | #include "VertexPipeline.hpp" |
| 13 | |
| 14 | #include "Vertex.hpp" |
| 15 | #include "Renderer.hpp" |
| 16 | #include "Debug.hpp" |
| 17 | |
| 18 | #include <string.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <stdio.h> |
| 21 | |
| 22 | #undef max |
| 23 | #undef min |
| 24 | |
| 25 | namespace sw |
| 26 | { |
Nicolas Capens | 44ffb65 | 2015-08-04 16:11:24 -0400 | [diff] [blame] | 27 | extern bool secondaryColor; |
| 28 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 29 | VertexPipeline::VertexPipeline(const VertexProcessor::State &state) : VertexRoutine(state, 0) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 30 | { |
| 31 | } |
| 32 | |
| 33 | VertexPipeline::~VertexPipeline() |
| 34 | { |
| 35 | } |
| 36 | |
Nicolas Capens | b4fb367 | 2016-01-15 17:02:41 -0500 | [diff] [blame^] | 37 | Vector4f VertexPipeline::transformBlend(const Register &src, const Pointer<Byte> &matrix, bool homogeneous) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 38 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 39 | Vector4f dst; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 40 | |
| 41 | if(state.vertexBlendMatrixCount == 0) |
| 42 | { |
| 43 | dst = transform(src, matrix, homogeneous); |
| 44 | } |
| 45 | else |
| 46 | { |
| 47 | UInt index0[4]; |
| 48 | UInt index1[4]; |
| 49 | UInt index2[4]; |
| 50 | UInt index3[4]; |
| 51 | |
| 52 | if(state.indexedVertexBlendEnable) |
| 53 | { |
| 54 | for(int i = 0; i < 4; i++) |
| 55 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 56 | Float4 B = r.v[BlendIndices].x; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 57 | UInt indices; |
Nicolas Capens | b4fb367 | 2016-01-15 17:02:41 -0500 | [diff] [blame^] | 58 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 59 | switch(i) |
| 60 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 61 | case 0: indices = As<UInt>(Float(B.x)); break; |
| 62 | case 1: indices = As<UInt>(Float(B.y)); break; |
| 63 | case 2: indices = As<UInt>(Float(B.z)); break; |
| 64 | case 3: indices = As<UInt>(Float(B.w)); break; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 65 | } |
| 66 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 67 | index0[i] = (indices & 0x000000FF) << 6; |
| 68 | index1[i] = (indices & 0x0000FF00) >> 2; |
| 69 | index2[i] = (indices & 0x00FF0000) >> 10; |
| 70 | index3[i] = (indices & 0xFF000000) >> 18; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | for(int i = 0; i < 4; i++) |
| 76 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 77 | index0[i] = 0 * 64; |
| 78 | index1[i] = 1 * 64; |
| 79 | index2[i] = 2 * 64; |
| 80 | index3[i] = 3 * 64; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | Float4 weight0; |
| 85 | Float4 weight1; |
| 86 | Float4 weight2; |
| 87 | Float4 weight3; |
| 88 | |
| 89 | switch(state.vertexBlendMatrixCount) |
| 90 | { |
| 91 | case 4: weight2 = r.v[BlendWeight].z; |
| 92 | case 3: weight1 = r.v[BlendWeight].y; |
| 93 | case 2: weight0 = r.v[BlendWeight].x; |
| 94 | case 1: |
| 95 | break; |
| 96 | } |
| 97 | |
| 98 | if(state.vertexBlendMatrixCount == 1) |
| 99 | { |
| 100 | dst = transform(src, matrix, index0, homogeneous); |
| 101 | } |
| 102 | else if(state.vertexBlendMatrixCount == 2) |
| 103 | { |
| 104 | weight1 = Float4(1.0f) - weight0; |
| 105 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 106 | Vector4f pos0; |
| 107 | Vector4f pos1; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 108 | |
| 109 | pos0 = transform(src, matrix, index0, homogeneous); |
| 110 | pos1 = transform(src, matrix, index1, homogeneous); |
| 111 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 112 | dst.x = pos0.x * weight0 + pos1.x * weight1; // FIXME: Vector4f operators |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 113 | dst.y = pos0.y * weight0 + pos1.y * weight1; |
| 114 | dst.z = pos0.z * weight0 + pos1.z * weight1; |
| 115 | dst.w = pos0.w * weight0 + pos1.w * weight1; |
| 116 | } |
| 117 | else if(state.vertexBlendMatrixCount == 3) |
| 118 | { |
| 119 | weight2 = Float4(1.0f) - (weight0 + weight1); |
| 120 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 121 | Vector4f pos0; |
| 122 | Vector4f pos1; |
| 123 | Vector4f pos2; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 124 | |
| 125 | pos0 = transform(src, matrix, index0, homogeneous); |
| 126 | pos1 = transform(src, matrix, index1, homogeneous); |
| 127 | pos2 = transform(src, matrix, index2, homogeneous); |
| 128 | |
| 129 | dst.x = pos0.x * weight0 + pos1.x * weight1 + pos2.x * weight2; |
| 130 | dst.y = pos0.y * weight0 + pos1.y * weight1 + pos2.y * weight2; |
| 131 | dst.z = pos0.z * weight0 + pos1.z * weight1 + pos2.z * weight2; |
| 132 | dst.w = pos0.w * weight0 + pos1.w * weight1 + pos2.w * weight2; |
| 133 | } |
| 134 | else if(state.vertexBlendMatrixCount == 4) |
| 135 | { |
| 136 | weight3 = Float4(1.0f) - (weight0 + weight1 + weight2); |
| 137 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 138 | Vector4f pos0; |
| 139 | Vector4f pos1; |
| 140 | Vector4f pos2; |
| 141 | Vector4f pos3; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 142 | |
| 143 | pos0 = transform(src, matrix, index0, homogeneous); |
| 144 | pos1 = transform(src, matrix, index1, homogeneous); |
| 145 | pos2 = transform(src, matrix, index2, homogeneous); |
| 146 | pos3 = transform(src, matrix, index3, homogeneous); |
| 147 | |
| 148 | dst.x = pos0.x * weight0 + pos1.x * weight1 + pos2.x * weight2 + pos3.x * weight3; |
| 149 | dst.y = pos0.y * weight0 + pos1.y * weight1 + pos2.y * weight2 + pos3.y * weight3; |
| 150 | dst.z = pos0.z * weight0 + pos1.z * weight1 + pos2.z * weight2 + pos3.z * weight3; |
| 151 | dst.w = pos0.w * weight0 + pos1.w * weight1 + pos2.w * weight2 + pos3.w * weight3; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return dst; |
| 156 | } |
| 157 | |
Nicolas Capens | b4fb367 | 2016-01-15 17:02:41 -0500 | [diff] [blame^] | 158 | void VertexPipeline::pipeline() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 159 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 160 | Vector4f position; |
| 161 | Vector4f normal; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 162 | |
| 163 | if(!state.preTransformed) |
| 164 | { |
Nicolas Capens | b4fb367 | 2016-01-15 17:02:41 -0500 | [diff] [blame^] | 165 | position = transformBlend(r.v[Position], Pointer<Byte>(r.data + OFFSET(DrawData,ff.transformT)), true); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 166 | } |
| 167 | else |
| 168 | { |
| 169 | position = r.v[PositionT]; |
| 170 | } |
| 171 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 172 | r.o[Pos].x = position.x; |
| 173 | r.o[Pos].y = position.y; |
| 174 | r.o[Pos].z = position.z; |
| 175 | r.o[Pos].w = position.w; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 176 | |
Nicolas Capens | b4fb367 | 2016-01-15 17:02:41 -0500 | [diff] [blame^] | 177 | Vector4f vertexPosition = transformBlend(r.v[Position], Pointer<Byte>(r.data + OFFSET(DrawData,ff.cameraTransformT)), true); |
Nicolas Capens | a36f3f9 | 2015-08-04 15:34:26 -0400 | [diff] [blame] | 178 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 179 | if(state.vertexNormalActive) |
| 180 | { |
Nicolas Capens | b4fb367 | 2016-01-15 17:02:41 -0500 | [diff] [blame^] | 181 | normal = transformBlend(r.v[Normal], Pointer<Byte>(r.data + OFFSET(DrawData,ff.normalTransformT)), false); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 182 | |
| 183 | if(state.normalizeNormals) |
| 184 | { |
| 185 | normal = normalize(normal); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | if(!state.vertexLightingActive) |
| 190 | { |
| 191 | // FIXME: Don't process if not used at all |
| 192 | if(state.diffuseActive && state.input[Color0]) |
| 193 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 194 | Vector4f diffuse = r.v[Color0]; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 195 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 196 | r.o[D0].x = diffuse.x; |
| 197 | r.o[D0].y = diffuse.y; |
| 198 | r.o[D0].z = diffuse.z; |
| 199 | r.o[D0].w = diffuse.w; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 200 | } |
| 201 | else |
| 202 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 203 | r.o[D0].x = Float4(1.0f); |
| 204 | r.o[D0].y = Float4(1.0f); |
| 205 | r.o[D0].z = Float4(1.0f); |
| 206 | r.o[D0].w = Float4(1.0f); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // FIXME: Don't process if not used at all |
| 210 | if(state.specularActive && state.input[Color1]) |
| 211 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 212 | Vector4f specular = r.v[Color1]; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 213 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 214 | r.o[D1].x = specular.x; |
| 215 | r.o[D1].y = specular.y; |
| 216 | r.o[D1].z = specular.z; |
| 217 | r.o[D1].w = specular.w; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 218 | } |
| 219 | else |
| 220 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 221 | r.o[D1].x = Float4(0.0f); |
| 222 | r.o[D1].y = Float4(0.0f); |
| 223 | r.o[D1].z = Float4(0.0f); |
| 224 | r.o[D1].w = Float4(1.0f); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | else |
| 228 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 229 | r.o[D0].x = Float4(0.0f); |
| 230 | r.o[D0].y = Float4(0.0f); |
| 231 | r.o[D0].z = Float4(0.0f); |
| 232 | r.o[D0].w = Float4(0.0f); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 233 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 234 | r.o[D1].x = Float4(0.0f); |
| 235 | r.o[D1].y = Float4(0.0f); |
| 236 | r.o[D1].z = Float4(0.0f); |
| 237 | r.o[D1].w = Float4(0.0f); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 238 | |
Nicolas Capens | 6aea1b2 | 2015-08-05 00:05:20 -0400 | [diff] [blame] | 239 | Vector4f ambient; |
| 240 | Float4 globalAmbient = *Pointer<Float4>(r.data + OFFSET(DrawData,ff.globalAmbient)); // FIXME: Unpack |
| 241 | |
| 242 | ambient.x = globalAmbient.x; |
| 243 | ambient.y = globalAmbient.y; |
| 244 | ambient.z = globalAmbient.z; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 245 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 246 | for(int i = 0; i < 8; i++) |
| 247 | { |
| 248 | if(!(state.vertexLightActive & (1 << i))) |
| 249 | { |
| 250 | continue; |
| 251 | } |
| 252 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 253 | Vector4f L; // Light vector |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 254 | Float4 att; // Attenuation |
| 255 | |
| 256 | // Attenuation |
| 257 | { |
| 258 | Float4 d; // Distance |
| 259 | |
| 260 | L.x = L.y = L.z = *Pointer<Float4>(r.data + OFFSET(DrawData,ff.lightPosition[i])); // FIXME: Unpack |
| 261 | L.x = L.x.xxxx; |
| 262 | L.y = L.y.yyyy; |
| 263 | L.z = L.z.zzzz; |
| 264 | |
| 265 | L.x -= vertexPosition.x; |
| 266 | L.y -= vertexPosition.y; |
| 267 | L.z -= vertexPosition.z; |
| 268 | d = dot3(L, L); |
| 269 | d = RcpSqrt_pp(d); // FIXME: Sufficient precision? |
| 270 | L.x *= d; |
| 271 | L.y *= d; |
| 272 | L.z *= d; |
| 273 | d = Rcp_pp(d); // FIXME: Sufficient precision? |
| 274 | |
| 275 | Float4 q = *Pointer<Float4>(r.data + OFFSET(DrawData,ff.attenuationQuadratic[i])); |
| 276 | Float4 l = *Pointer<Float4>(r.data + OFFSET(DrawData,ff.attenuationLinear[i])); |
| 277 | Float4 c = *Pointer<Float4>(r.data + OFFSET(DrawData,ff.attenuationConstant[i])); |
| 278 | |
| 279 | att = Rcp_pp((q * d + l) * d + c); |
| 280 | } |
| 281 | |
| 282 | // Ambient per light |
| 283 | { |
| 284 | Float4 lightAmbient = *Pointer<Float4>(r.data + OFFSET(DrawData,ff.lightAmbient[i])); // FIXME: Unpack |
| 285 | |
Nicolas Capens | 6aea1b2 | 2015-08-05 00:05:20 -0400 | [diff] [blame] | 286 | ambient.x = ambient.x + lightAmbient.x * att; |
| 287 | ambient.y = ambient.y + lightAmbient.y * att; |
| 288 | ambient.z = ambient.z + lightAmbient.z * att; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | // Diffuse |
| 292 | if(state.vertexNormalActive) |
| 293 | { |
| 294 | Float4 dot; |
| 295 | |
| 296 | dot = dot3(L, normal); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 297 | dot = Max(dot, Float4(0.0f)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 298 | dot *= att; |
| 299 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 300 | Vector4f diff; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 301 | |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 302 | if(state.vertexDiffuseMaterialSourceActive == MATERIAL_MATERIAL) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 303 | { |
| 304 | diff.x = diff.y = diff.z = *Pointer<Float4>(r.data + OFFSET(DrawData,ff.materialDiffuse)); // FIXME: Unpack |
| 305 | diff.x = diff.x.xxxx; |
| 306 | diff.y = diff.y.yyyy; |
| 307 | diff.z = diff.z.zzzz; |
| 308 | } |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 309 | else if(state.vertexDiffuseMaterialSourceActive == MATERIAL_COLOR1) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 310 | { |
| 311 | diff = r.v[Color0]; |
| 312 | } |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 313 | else if(state.vertexDiffuseMaterialSourceActive == MATERIAL_COLOR2) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 314 | { |
| 315 | diff = r.v[Color1]; |
| 316 | } |
| 317 | else ASSERT(false); |
| 318 | |
| 319 | Float4 lightDiffuse = *Pointer<Float4>(r.data + OFFSET(DrawData,ff.lightDiffuse[i])); |
| 320 | |
Nicolas Capens | 6aea1b2 | 2015-08-05 00:05:20 -0400 | [diff] [blame] | 321 | r.o[D0].x = r.o[D0].x + diff.x * dot * lightDiffuse.x; // FIXME: Clamp first? |
| 322 | r.o[D0].y = r.o[D0].y + diff.y * dot * lightDiffuse.y; // FIXME: Clamp first? |
| 323 | r.o[D0].z = r.o[D0].z + diff.z * dot * lightDiffuse.z; // FIXME: Clamp first? |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | // Specular |
| 327 | if(state.vertexSpecularActive) |
| 328 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 329 | Vector4f S; |
| 330 | Vector4f C; // Camera vector |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 331 | Float4 pow; |
| 332 | |
| 333 | pow = *Pointer<Float>(r.data + OFFSET(DrawData,ff.materialShininess)); |
| 334 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 335 | S.x = Float4(0.0f) - vertexPosition.x; |
| 336 | S.y = Float4(0.0f) - vertexPosition.y; |
| 337 | S.z = Float4(0.0f) - vertexPosition.z; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 338 | C = normalize(S); |
| 339 | |
| 340 | S.x = L.x + C.x; |
| 341 | S.y = L.y + C.y; |
| 342 | S.z = L.z + C.z; |
| 343 | C = normalize(S); |
| 344 | |
| 345 | Float4 dot = Max(dot3(C, normal), Float4(0.0f)); // FIXME: max(dot3(C, normal), 0) |
| 346 | |
| 347 | Float4 P = power(dot, pow); |
| 348 | P *= att; |
| 349 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 350 | Vector4f spec; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 351 | |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 352 | if(state.vertexSpecularMaterialSourceActive == MATERIAL_MATERIAL) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 353 | { |
| 354 | Float4 materialSpecular = *Pointer<Float4>(r.data + OFFSET(DrawData,ff.materialSpecular)); // FIXME: Unpack |
| 355 | |
| 356 | spec.x = materialSpecular.x; |
| 357 | spec.y = materialSpecular.y; |
| 358 | spec.z = materialSpecular.z; |
| 359 | } |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 360 | else if(state.vertexSpecularMaterialSourceActive == MATERIAL_COLOR1) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 361 | { |
| 362 | spec = r.v[Color0]; |
| 363 | } |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 364 | else if(state.vertexSpecularMaterialSourceActive == MATERIAL_COLOR2) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 365 | { |
| 366 | spec = r.v[Color1]; |
| 367 | } |
| 368 | else ASSERT(false); |
| 369 | |
| 370 | Float4 lightSpecular = *Pointer<Float4>(r.data + OFFSET(DrawData,ff.lightSpecular[i])); |
| 371 | |
| 372 | spec.x *= lightSpecular.x; |
| 373 | spec.y *= lightSpecular.y; |
| 374 | spec.z *= lightSpecular.z; |
| 375 | |
| 376 | spec.x *= P; |
| 377 | spec.y *= P; |
| 378 | spec.z *= P; |
| 379 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 380 | spec.x = Max(spec.x, Float4(0.0f)); |
| 381 | spec.y = Max(spec.y, Float4(0.0f)); |
| 382 | spec.z = Max(spec.z, Float4(0.0f)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 383 | |
Nicolas Capens | 44ffb65 | 2015-08-04 16:11:24 -0400 | [diff] [blame] | 384 | if(secondaryColor) |
| 385 | { |
| 386 | r.o[D1].x = r.o[D1].x + spec.x; |
| 387 | r.o[D1].y = r.o[D1].y + spec.y; |
| 388 | r.o[D1].z = r.o[D1].z + spec.z; |
| 389 | } |
| 390 | else |
| 391 | { |
| 392 | r.o[D0].x = r.o[D0].x + spec.x; |
| 393 | r.o[D0].y = r.o[D0].y + spec.y; |
| 394 | r.o[D0].z = r.o[D0].z + spec.z; |
| 395 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 399 | if(state.vertexAmbientMaterialSourceActive == MATERIAL_MATERIAL) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 400 | { |
| 401 | Float4 materialAmbient = *Pointer<Float4>(r.data + OFFSET(DrawData,ff.materialAmbient)); // FIXME: Unpack |
| 402 | |
Nicolas Capens | 6aea1b2 | 2015-08-05 00:05:20 -0400 | [diff] [blame] | 403 | ambient.x = ambient.x * materialAmbient.x; |
| 404 | ambient.y = ambient.y * materialAmbient.y; |
| 405 | ambient.z = ambient.z * materialAmbient.z; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 406 | } |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 407 | else if(state.vertexAmbientMaterialSourceActive == MATERIAL_COLOR1) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 408 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 409 | Vector4f materialDiffuse = r.v[Color0]; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 410 | |
Nicolas Capens | 6aea1b2 | 2015-08-05 00:05:20 -0400 | [diff] [blame] | 411 | ambient.x = ambient.x * materialDiffuse.x; |
| 412 | ambient.y = ambient.y * materialDiffuse.y; |
| 413 | ambient.z = ambient.z * materialDiffuse.z; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 414 | } |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 415 | else if(state.vertexAmbientMaterialSourceActive == MATERIAL_COLOR2) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 416 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 417 | Vector4f materialSpecular = r.v[Color1]; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 418 | |
Nicolas Capens | 6aea1b2 | 2015-08-05 00:05:20 -0400 | [diff] [blame] | 419 | ambient.x = ambient.x * materialSpecular.x; |
| 420 | ambient.y = ambient.y * materialSpecular.y; |
| 421 | ambient.z = ambient.z * materialSpecular.z; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 422 | } |
| 423 | else ASSERT(false); |
| 424 | |
Nicolas Capens | 6aea1b2 | 2015-08-05 00:05:20 -0400 | [diff] [blame] | 425 | r.o[D0].x = r.o[D0].x + ambient.x; |
| 426 | r.o[D0].y = r.o[D0].y + ambient.y; |
| 427 | r.o[D0].z = r.o[D0].z + ambient.z; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 428 | |
| 429 | // Emissive |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 430 | if(state.vertexEmissiveMaterialSourceActive == MATERIAL_MATERIAL) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 431 | { |
| 432 | Float4 materialEmission = *Pointer<Float4>(r.data + OFFSET(DrawData,ff.materialEmission)); // FIXME: Unpack |
| 433 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 434 | r.o[D0].x = r.o[D0].x + materialEmission.x; |
| 435 | r.o[D0].y = r.o[D0].y + materialEmission.y; |
| 436 | r.o[D0].z = r.o[D0].z + materialEmission.z; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 437 | } |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 438 | else if(state.vertexEmissiveMaterialSourceActive == MATERIAL_COLOR1) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 439 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 440 | Vector4f materialSpecular = r.v[Color0]; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 441 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 442 | r.o[D0].x = r.o[D0].x + materialSpecular.x; |
| 443 | r.o[D0].y = r.o[D0].y + materialSpecular.y; |
| 444 | r.o[D0].z = r.o[D0].z + materialSpecular.z; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 445 | } |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 446 | else if(state.vertexEmissiveMaterialSourceActive == MATERIAL_COLOR2) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 447 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 448 | Vector4f materialSpecular = r.v[Color1]; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 449 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 450 | r.o[D0].x = r.o[D0].x + materialSpecular.x; |
| 451 | r.o[D0].y = r.o[D0].y + materialSpecular.y; |
| 452 | r.o[D0].z = r.o[D0].z + materialSpecular.z; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 453 | } |
| 454 | else ASSERT(false); |
| 455 | |
| 456 | // Diffuse alpha component |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 457 | if(state.vertexDiffuseMaterialSourceActive == MATERIAL_MATERIAL) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 458 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 459 | r.o[D0].w = Float4(*Pointer<Float4>(r.data + OFFSET(DrawData,ff.materialDiffuse[0]))).wwww; // FIXME: Unpack |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 460 | } |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 461 | else if(state.vertexDiffuseMaterialSourceActive == MATERIAL_COLOR1) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 462 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 463 | Vector4f alpha = r.v[Color0]; |
| 464 | r.o[D0].w = alpha.w; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 465 | } |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 466 | else if(state.vertexDiffuseMaterialSourceActive == MATERIAL_COLOR2) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 467 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 468 | Vector4f alpha = r.v[Color1]; |
| 469 | r.o[D0].w = alpha.w; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 470 | } |
| 471 | else ASSERT(false); |
| 472 | |
| 473 | if(state.vertexSpecularActive) |
| 474 | { |
| 475 | // Specular alpha component |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 476 | if(state.vertexSpecularMaterialSourceActive == MATERIAL_MATERIAL) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 477 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 478 | r.o[D1].w = Float4(*Pointer<Float4>(r.data + OFFSET(DrawData,ff.materialSpecular[3]))).wwww; // FIXME: Unpack |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 479 | } |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 480 | else if(state.vertexSpecularMaterialSourceActive == MATERIAL_COLOR1) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 481 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 482 | Vector4f alpha = r.v[Color0]; |
| 483 | r.o[D1].w = alpha.w; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 484 | } |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 485 | else if(state.vertexSpecularMaterialSourceActive == MATERIAL_COLOR2) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 486 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 487 | Vector4f alpha = r.v[Color1]; |
| 488 | r.o[D1].w = alpha.w; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 489 | } |
| 490 | else ASSERT(false); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | if(state.fogActive) |
| 495 | { |
Nicolas Capens | a36f3f9 | 2015-08-04 15:34:26 -0400 | [diff] [blame] | 496 | Float4 f; |
| 497 | |
| 498 | if(!state.rangeFogActive) |
| 499 | { |
| 500 | f = Abs(vertexPosition.z); |
| 501 | } |
| 502 | else |
| 503 | { |
| 504 | f = Sqrt(dot3(vertexPosition, vertexPosition)); // FIXME: f = length(vertexPosition); |
| 505 | } |
| 506 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 507 | switch(state.vertexFogMode) |
| 508 | { |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 509 | case FOG_NONE: |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 510 | if(state.specularActive) |
| 511 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 512 | r.o[Fog].x = r.o[D1].w; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 513 | } |
| 514 | else |
| 515 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 516 | r.o[Fog].x = Float4(0.0f); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 517 | } |
| 518 | break; |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 519 | case FOG_LINEAR: |
Nicolas Capens | a36f3f9 | 2015-08-04 15:34:26 -0400 | [diff] [blame] | 520 | r.o[Fog].x = f * *Pointer<Float4>(r.data + OFFSET(DrawData,fog.scale)) + *Pointer<Float4>(r.data + OFFSET(DrawData,fog.offset)); |
| 521 | break; |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 522 | case FOG_EXP: |
Nicolas Capens | a36f3f9 | 2015-08-04 15:34:26 -0400 | [diff] [blame] | 523 | r.o[Fog].x = exponential2(f * *Pointer<Float4>(r.data + OFFSET(DrawData,fog.densityE)), true); |
| 524 | break; |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 525 | case FOG_EXP2: |
Nicolas Capens | a36f3f9 | 2015-08-04 15:34:26 -0400 | [diff] [blame] | 526 | r.o[Fog].x = exponential2((f * f) * *Pointer<Float4>(r.data + OFFSET(DrawData,fog.density2E)), true); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 527 | break; |
| 528 | default: |
| 529 | ASSERT(false); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | for(int stage = 0; stage < 8; stage++) |
| 534 | { |
Nicolas Capens | b4fb367 | 2016-01-15 17:02:41 -0500 | [diff] [blame^] | 535 | processTextureCoordinate(stage, normal, position); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 536 | } |
| 537 | |
Nicolas Capens | b4fb367 | 2016-01-15 17:02:41 -0500 | [diff] [blame^] | 538 | processPointSize(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 539 | } |
| 540 | |
Nicolas Capens | b4fb367 | 2016-01-15 17:02:41 -0500 | [diff] [blame^] | 541 | void VertexPipeline::processTextureCoordinate(int stage, Vector4f &normal, Vector4f &position) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 542 | { |
| 543 | if(state.output[T0 + stage].write) |
| 544 | { |
| 545 | int i = state.textureState[stage].texCoordIndexActive; |
| 546 | |
| 547 | switch(state.textureState[stage].texGenActive) |
| 548 | { |
Nicolas Capens | 70583fa | 2015-05-14 18:17:14 -0400 | [diff] [blame] | 549 | case TEXGEN_NONE: |
| 550 | { |
| 551 | Vector4f v = r.v[TexCoord0 + i]; |
| 552 | |
| 553 | r.o[T0 + stage].x = v.x; |
| 554 | r.o[T0 + stage].y = v.y; |
| 555 | r.o[T0 + stage].z = v.z; |
| 556 | r.o[T0 + stage].w = v.w; |
| 557 | } |
| 558 | break; |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 559 | case TEXGEN_PASSTHRU: |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 560 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 561 | Vector4f v = r.v[TexCoord0 + i]; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 562 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 563 | r.o[T0 + stage].x = v.x; |
| 564 | r.o[T0 + stage].y = v.y; |
| 565 | r.o[T0 + stage].z = v.z; |
| 566 | r.o[T0 + stage].w = v.w; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 567 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 568 | if(state.input[TexCoord0 + i]) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 569 | { |
| 570 | switch(state.input[TexCoord0 + i].count) |
| 571 | { |
| 572 | case 1: |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 573 | r.o[T0 + stage].y = Float4(1.0f); |
| 574 | r.o[T0 + stage].z = Float4(0.0f); |
| 575 | r.o[T0 + stage].w = Float4(0.0f); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 576 | break; |
| 577 | case 2: |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 578 | r.o[T0 + stage].z = Float4(1.0f); |
| 579 | r.o[T0 + stage].w = Float4(0.0f); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 580 | break; |
| 581 | case 3: |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 582 | r.o[T0 + stage].w = Float4(1.0f); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 583 | break; |
| 584 | case 4: |
| 585 | break; |
| 586 | default: |
| 587 | ASSERT(false); |
| 588 | } |
| 589 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 590 | } |
| 591 | break; |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 592 | case TEXGEN_NORMAL: |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 593 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 594 | Vector4f Nc; // Normal vector in camera space |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 595 | |
| 596 | if(state.vertexNormalActive) |
| 597 | { |
| 598 | Nc = normal; |
| 599 | } |
| 600 | else |
| 601 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 602 | Nc.x = Float4(0.0f); |
| 603 | Nc.y = Float4(0.0f); |
| 604 | Nc.z = Float4(0.0f); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 605 | } |
| 606 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 607 | Nc.w = Float4(1.0f); |
Nicolas Capens | b4fb367 | 2016-01-15 17:02:41 -0500 | [diff] [blame^] | 608 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 609 | r.o[T0 + stage].x = Nc.x; |
| 610 | r.o[T0 + stage].y = Nc.y; |
| 611 | r.o[T0 + stage].z = Nc.z; |
| 612 | r.o[T0 + stage].w = Nc.w; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 613 | } |
| 614 | break; |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 615 | case TEXGEN_POSITION: |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 616 | { |
Nicolas Capens | b4fb367 | 2016-01-15 17:02:41 -0500 | [diff] [blame^] | 617 | Vector4f Pn = transformBlend(r.v[Position], Pointer<Byte>(r.data + OFFSET(DrawData,ff.cameraTransformT)), true); // Position in camera space |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 618 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 619 | Pn.w = Float4(1.0f); |
Nicolas Capens | b4fb367 | 2016-01-15 17:02:41 -0500 | [diff] [blame^] | 620 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 621 | r.o[T0 + stage].x = Pn.x; |
| 622 | r.o[T0 + stage].y = Pn.y; |
| 623 | r.o[T0 + stage].z = Pn.z; |
| 624 | r.o[T0 + stage].w = Pn.w; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 625 | } |
| 626 | break; |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 627 | case TEXGEN_REFLECTION: |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 628 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 629 | Vector4f R; // Reflection vector |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 630 | |
| 631 | if(state.vertexNormalActive) |
| 632 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 633 | Vector4f Nc; // Normal vector in camera space |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 634 | |
| 635 | Nc = normal; |
| 636 | |
| 637 | if(state.localViewerActive) |
| 638 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 639 | Vector4f Ec; // Eye vector in camera space |
| 640 | Vector4f N2; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 641 | |
Nicolas Capens | b4fb367 | 2016-01-15 17:02:41 -0500 | [diff] [blame^] | 642 | Ec = transformBlend(r.v[Position], Pointer<Byte>(r.data + OFFSET(DrawData,ff.cameraTransformT)), true); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 643 | Ec = normalize(Ec); |
| 644 | |
| 645 | // R = E - 2 * N * (E . N) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 646 | Float4 dot = Float4(2.0f) * dot3(Ec, Nc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 647 | |
| 648 | R.x = Ec.x - Nc.x * dot; |
| 649 | R.y = Ec.y - Nc.y * dot; |
| 650 | R.z = Ec.z - Nc.z * dot; |
| 651 | } |
| 652 | else |
| 653 | { |
| 654 | // u = -2 * Nz * Nx |
| 655 | // v = -2 * Nz * Ny |
| 656 | // w = 1 - 2 * Nz * Nz |
| 657 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 658 | R.x = -Float4(2.0f) * Nc.z * Nc.x; |
| 659 | R.y = -Float4(2.0f) * Nc.z * Nc.y; |
| 660 | R.z = Float4(1.0f) - Float4(2.0f) * Nc.z * Nc.z; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | else |
| 664 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 665 | R.x = Float4(0.0f); |
| 666 | R.y = Float4(0.0f); |
| 667 | R.z = Float4(0.0f); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 668 | } |
| 669 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 670 | R.w = Float4(1.0f); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 671 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 672 | r.o[T0 + stage].x = R.x; |
| 673 | r.o[T0 + stage].y = R.y; |
| 674 | r.o[T0 + stage].z = R.z; |
| 675 | r.o[T0 + stage].w = R.w; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 676 | } |
| 677 | break; |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 678 | case TEXGEN_SPHEREMAP: |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 679 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 680 | Vector4f R; // Reflection vector |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 681 | |
| 682 | if(state.vertexNormalActive) |
| 683 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 684 | Vector4f Nc; // Normal vector in camera space |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 685 | |
| 686 | Nc = normal; |
| 687 | |
| 688 | if(state.localViewerActive) |
| 689 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 690 | Vector4f Ec; // Eye vector in camera space |
| 691 | Vector4f N2; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 692 | |
Nicolas Capens | b4fb367 | 2016-01-15 17:02:41 -0500 | [diff] [blame^] | 693 | Ec = transformBlend(r.v[Position], Pointer<Byte>(r.data + OFFSET(DrawData,ff.cameraTransformT)), true); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 694 | Ec = normalize(Ec); |
| 695 | |
| 696 | // R = E - 2 * N * (E . N) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 697 | Float4 dot = Float4(2.0f) * dot3(Ec, Nc); |
Nicolas Capens | b4fb367 | 2016-01-15 17:02:41 -0500 | [diff] [blame^] | 698 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 699 | R.x = Ec.x - Nc.x * dot; |
| 700 | R.y = Ec.y - Nc.y * dot; |
| 701 | R.z = Ec.z - Nc.z * dot; |
| 702 | } |
| 703 | else |
| 704 | { |
| 705 | // u = -2 * Nz * Nx |
| 706 | // v = -2 * Nz * Ny |
| 707 | // w = 1 - 2 * Nz * Nz |
| 708 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 709 | R.x = -Float4(2.0f) * Nc.z * Nc.x; |
| 710 | R.y = -Float4(2.0f) * Nc.z * Nc.y; |
| 711 | R.z = Float4(1.0f) - Float4(2.0f) * Nc.z * Nc.z; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | else |
| 715 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 716 | R.x = Float4(0.0f); |
| 717 | R.y = Float4(0.0f); |
| 718 | R.z = Float4(0.0f); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 719 | } |
| 720 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 721 | R.z -= Float4(1.0f); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 722 | R = normalize(R); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 723 | R.x = Float4(0.5f) * R.x + Float4(0.5f); |
| 724 | R.y = Float4(0.5f) * R.y + Float4(0.5f); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 725 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 726 | R.z = Float4(1.0f); |
| 727 | R.w = Float4(0.0f); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 728 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 729 | r.o[T0 + stage].x = R.x; |
| 730 | r.o[T0 + stage].y = R.y; |
| 731 | r.o[T0 + stage].z = R.z; |
| 732 | r.o[T0 + stage].w = R.w; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 733 | } |
| 734 | break; |
| 735 | default: |
| 736 | ASSERT(false); |
| 737 | } |
| 738 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 739 | Vector4f texTrans0; |
| 740 | Vector4f texTrans1; |
| 741 | Vector4f texTrans2; |
| 742 | Vector4f texTrans3; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 743 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 744 | Vector4f T; |
| 745 | Vector4f t; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 746 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 747 | T.x = r.o[T0 + stage].x; |
| 748 | T.y = r.o[T0 + stage].y; |
| 749 | T.z = r.o[T0 + stage].z; |
| 750 | T.w = r.o[T0 + stage].w; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 751 | |
| 752 | switch(state.textureState[stage].textureTransformCountActive) |
| 753 | { |
| 754 | case 4: |
| 755 | texTrans3.x = texTrans3.y = texTrans3.z = texTrans3.w = *Pointer<Float4>(r.data + OFFSET(DrawData,ff.textureTransform[stage][3])); // FIXME: Unpack |
| 756 | texTrans3.x = texTrans3.x.xxxx; |
| 757 | texTrans3.y = texTrans3.y.yyyy; |
| 758 | texTrans3.z = texTrans3.z.zzzz; |
| 759 | texTrans3.w = texTrans3.w.wwww; |
| 760 | t.w = dot4(T, texTrans3); |
| 761 | case 3: |
| 762 | texTrans2.x = texTrans2.y = texTrans2.z = texTrans2.w = *Pointer<Float4>(r.data + OFFSET(DrawData,ff.textureTransform[stage][2])); // FIXME: Unpack |
| 763 | texTrans2.x = texTrans2.x.xxxx; |
| 764 | texTrans2.y = texTrans2.y.yyyy; |
| 765 | texTrans2.z = texTrans2.z.zzzz; |
| 766 | texTrans2.w = texTrans2.w.wwww; |
| 767 | t.z = dot4(T, texTrans2); |
| 768 | case 2: |
| 769 | texTrans1.x = texTrans1.y = texTrans1.z = texTrans1.w = *Pointer<Float4>(r.data + OFFSET(DrawData,ff.textureTransform[stage][1])); // FIXME: Unpack |
| 770 | texTrans1.x = texTrans1.x.xxxx; |
| 771 | texTrans1.y = texTrans1.y.yyyy; |
| 772 | texTrans1.z = texTrans1.z.zzzz; |
| 773 | texTrans1.w = texTrans1.w.wwww; |
| 774 | t.y = dot4(T, texTrans1); |
| 775 | case 1: |
| 776 | texTrans0.x = texTrans0.y = texTrans0.z = texTrans0.w = *Pointer<Float4>(r.data + OFFSET(DrawData,ff.textureTransform[stage][0])); // FIXME: Unpack |
| 777 | texTrans0.x = texTrans0.x.xxxx; |
| 778 | texTrans0.y = texTrans0.y.yyyy; |
| 779 | texTrans0.z = texTrans0.z.zzzz; |
| 780 | texTrans0.w = texTrans0.w.wwww; |
| 781 | t.x = dot4(T, texTrans0); |
| 782 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 783 | r.o[T0 + stage].x = t.x; |
| 784 | r.o[T0 + stage].y = t.y; |
| 785 | r.o[T0 + stage].z = t.z; |
| 786 | r.o[T0 + stage].w = t.w; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 787 | case 0: |
| 788 | break; |
| 789 | default: |
| 790 | ASSERT(false); |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | |
Nicolas Capens | b4fb367 | 2016-01-15 17:02:41 -0500 | [diff] [blame^] | 795 | void VertexPipeline::processPointSize() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 796 | { |
| 797 | if(!state.pointSizeActive) |
| 798 | { |
| 799 | return; // Use global pointsize |
| 800 | } |
| 801 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 802 | if(state.input[PointSize]) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 803 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 804 | r.o[Pts].y = r.v[PointSize].x; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 805 | } |
| 806 | else |
| 807 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 808 | r.o[Pts].y = *Pointer<Float4>(r.data + OFFSET(DrawData,point.pointSize)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | if(state.pointScaleActive && !state.preTransformed) |
| 812 | { |
Nicolas Capens | b4fb367 | 2016-01-15 17:02:41 -0500 | [diff] [blame^] | 813 | Vector4f p = transformBlend(r.v[Position], Pointer<Byte>(r.data + OFFSET(DrawData,ff.cameraTransformT)), true); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 814 | |
| 815 | Float4 d = Sqrt(dot3(p, p)); // FIXME: length(p); |
| 816 | |
| 817 | Float4 A = *Pointer<Float>(r.data + OFFSET(DrawData,point.pointScaleA)); // FIXME: Unpack |
| 818 | Float4 B = *Pointer<Float>(r.data + OFFSET(DrawData,point.pointScaleB)); // FIXME: Unpack |
| 819 | Float4 C = *Pointer<Float>(r.data + OFFSET(DrawData,point.pointScaleC)); // FIXME: Unpack |
| 820 | |
| 821 | A = RcpSqrt_pp(A + d * (B + d * C)); |
| 822 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 823 | r.o[Pts].y = r.o[Pts].y * Float4(*Pointer<Float>(r.data + OFFSET(DrawData,viewportHeight))) * A; // FIXME: Unpack |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 824 | } |
| 825 | } |
| 826 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 827 | Vector4f VertexPipeline::transform(const Register &src, const Pointer<Byte> &matrix, bool homogeneous) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 828 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 829 | Vector4f dst; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 830 | |
| 831 | if(homogeneous) |
| 832 | { |
| 833 | Float4 m[4][4]; |
| 834 | |
| 835 | for(int j = 0; j < 4; j++) |
| 836 | { |
| 837 | for(int i = 0; i < 4; i++) |
| 838 | { |
| 839 | m[j][i].x = *Pointer<Float>(matrix + 16 * i + 4 * j); |
| 840 | m[j][i].y = *Pointer<Float>(matrix + 16 * i + 4 * j); |
| 841 | m[j][i].z = *Pointer<Float>(matrix + 16 * i + 4 * j); |
| 842 | m[j][i].w = *Pointer<Float>(matrix + 16 * i + 4 * j); |
| 843 | } |
| 844 | } |
| 845 | |
Nicolas Capens | 48621bd | 2015-08-03 15:33:44 -0400 | [diff] [blame] | 846 | dst.x = src.x * m[0][0] + src.y * m[0][1] + src.z * m[0][2] + src.w * m[0][3]; |
| 847 | dst.y = src.x * m[1][0] + src.y * m[1][1] + src.z * m[1][2] + src.w * m[1][3]; |
| 848 | dst.z = src.x * m[2][0] + src.y * m[2][1] + src.z * m[2][2] + src.w * m[2][3]; |
| 849 | dst.w = src.x * m[3][0] + src.y * m[3][1] + src.z * m[3][2] + src.w * m[3][3]; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 850 | } |
| 851 | else |
| 852 | { |
| 853 | Float4 m[3][3]; |
| 854 | |
| 855 | for(int j = 0; j < 3; j++) |
| 856 | { |
| 857 | for(int i = 0; i < 3; i++) |
| 858 | { |
| 859 | m[j][i].x = *Pointer<Float>(matrix + 16 * i + 4 * j); |
| 860 | m[j][i].y = *Pointer<Float>(matrix + 16 * i + 4 * j); |
| 861 | m[j][i].z = *Pointer<Float>(matrix + 16 * i + 4 * j); |
| 862 | m[j][i].w = *Pointer<Float>(matrix + 16 * i + 4 * j); |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | dst.x = src.x * m[0][0] + src.y * m[0][1] + src.z * m[0][2]; |
| 867 | dst.y = src.x * m[1][0] + src.y * m[1][1] + src.z * m[1][2]; |
| 868 | dst.z = src.x * m[2][0] + src.y * m[2][1] + src.z * m[2][2]; |
| 869 | } |
| 870 | |
| 871 | return dst; |
| 872 | } |
| 873 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 874 | Vector4f VertexPipeline::transform(const Register &src, const Pointer<Byte> &matrix, UInt index[4], bool homogeneous) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 875 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 876 | Vector4f dst; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 877 | |
| 878 | if(homogeneous) |
| 879 | { |
| 880 | Float4 m[4][4]; |
| 881 | |
| 882 | for(int j = 0; j < 4; j++) |
| 883 | { |
| 884 | for(int i = 0; i < 4; i++) |
| 885 | { |
| 886 | m[j][i].x = *Pointer<Float>(matrix + 16 * i + 4 * j + index[0]); |
| 887 | m[j][i].y = *Pointer<Float>(matrix + 16 * i + 4 * j + index[1]); |
| 888 | m[j][i].z = *Pointer<Float>(matrix + 16 * i + 4 * j + index[2]); |
| 889 | m[j][i].w = *Pointer<Float>(matrix + 16 * i + 4 * j + index[3]); |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | dst.x = src.x * m[0][0] + src.y * m[0][1] + src.z * m[0][2] + m[0][3]; |
| 894 | dst.y = src.x * m[1][0] + src.y * m[1][1] + src.z * m[1][2] + m[1][3]; |
| 895 | dst.z = src.x * m[2][0] + src.y * m[2][1] + src.z * m[2][2] + m[2][3]; |
| 896 | dst.w = src.x * m[3][0] + src.y * m[3][1] + src.z * m[3][2] + m[3][3]; |
| 897 | } |
| 898 | else |
| 899 | { |
| 900 | Float4 m[3][3]; |
| 901 | |
| 902 | for(int j = 0; j < 3; j++) |
| 903 | { |
| 904 | for(int i = 0; i < 3; i++) |
| 905 | { |
| 906 | m[j][i].x = *Pointer<Float>(matrix + 16 * i + 4 * j + index[0]); |
| 907 | m[j][i].y = *Pointer<Float>(matrix + 16 * i + 4 * j + index[1]); |
| 908 | m[j][i].z = *Pointer<Float>(matrix + 16 * i + 4 * j + index[2]); |
| 909 | m[j][i].w = *Pointer<Float>(matrix + 16 * i + 4 * j + index[3]); |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | dst.x = src.x * m[0][0] + src.y * m[0][1] + src.z * m[0][2]; |
| 914 | dst.y = src.x * m[1][0] + src.y * m[1][1] + src.z * m[1][2]; |
| 915 | dst.z = src.x * m[2][0] + src.y * m[2][1] + src.z * m[2][2]; |
| 916 | } |
| 917 | |
| 918 | return dst; |
| 919 | } |
| 920 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 921 | Vector4f VertexPipeline::normalize(Vector4f &src) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 922 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 923 | Vector4f dst; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 924 | |
| 925 | Float4 rcpLength = RcpSqrt_pp(dot3(src, src)); |
Nicolas Capens | b4fb367 | 2016-01-15 17:02:41 -0500 | [diff] [blame^] | 926 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 927 | dst.x = src.x * rcpLength; |
| 928 | dst.y = src.y * rcpLength; |
| 929 | dst.z = src.z * rcpLength; |
| 930 | |
| 931 | return dst; |
| 932 | } |
| 933 | |
| 934 | Float4 VertexPipeline::power(Float4 &src0, Float4 &src1) |
| 935 | { |
| 936 | Float4 dst = src0; |
Nicolas Capens | b4fb367 | 2016-01-15 17:02:41 -0500 | [diff] [blame^] | 937 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 938 | dst = dst * dst; |
| 939 | dst = dst * dst; |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 940 | dst = Float4(As<Int4>(dst) - As<Int4>(Float4(1.0f))); |
Nicolas Capens | b4fb367 | 2016-01-15 17:02:41 -0500 | [diff] [blame^] | 941 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 942 | dst *= src1; |
| 943 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 944 | dst = As<Float4>(Int4(dst) + As<Int4>(Float4(1.0f))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 945 | dst = RcpSqrt_pp(dst); |
| 946 | dst = RcpSqrt_pp(dst); |
| 947 | |
| 948 | return dst; |
| 949 | } |
| 950 | } |