lightAPI.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. 
  2. #include "lightAPI.h"
  3. #include "../../DFPSR/base/simd3D.h"
  4. #include "../../DFPSR/base/threading.h" // TODO: Make an official "dangerous" API for multi-threading
  5. namespace dsr {
  6. // Precondition: The packed color must be in the standard RGBA order, meaning no native packing
  7. inline F32xXx3 unpackRgb_U32xX_to_F32xXx3(const U32xX& color) {
  8. return F32xXx3(floatFromU32(packOrder_getRed(color)), floatFromU32(packOrder_getGreen(color)), floatFromU32(packOrder_getBlue(color)));
  9. }
  10. static inline void setLight(SafePointer<uint8_t> lightPixel, U8xX newlight) {
  11. newlight.writeAligned(lightPixel, "setLight: writing light");
  12. }
  13. static inline void addLight(SafePointer<uint8_t> lightPixel, U8xX addedlight) {
  14. U8xX oldLight = U8xX::readAligned(lightPixel, "addLight: reading light");
  15. U8xX newlight = saturatedAddition(oldLight, addedlight);
  16. newlight.writeAligned(lightPixel, "addLight: writing light");
  17. }
  18. template <bool ADD_LIGHT>
  19. void directedLight(const FMatrix3x3& normalToWorldSpace, OrderedImageRgbaU8& lightBuffer, const OrderedImageRgbaU8& normalBuffer, const FVector3D& lightDirection, float lightIntensity, const ColorRgbI32& lightColor) {
  20. // Normals in range 0..255 - 128 have lengths of 127 and 128, so if we double the reverse light direction we'll end up near 0..255 again for colors
  21. F32xXx3 reverseLightDirection = F32xXx3(-normalize(normalToWorldSpace.transformTransposed(lightDirection)) * lightIntensity * 2.0f);
  22. IRect rectangleBound = image_getBound(lightBuffer);
  23. float colorR = std::max(0.0f, (float)lightColor.red / 255.0f);
  24. float colorG = std::max(0.0f, (float)lightColor.green / 255.0f);
  25. float colorB = std::max(0.0f, (float)lightColor.blue / 255.0f);
  26. threadedSplit(rectangleBound, [
  27. lightBuffer, normalBuffer, reverseLightDirection, colorR, colorG, colorB](const IRect& bound) mutable {
  28. SafePointer<uint8_t> lightRow = image_getSafePointer_channels(lightBuffer, bound.top());
  29. SafePointer<uint32_t> normalRow = image_getSafePointer(normalBuffer, bound.top());
  30. int lightStride = image_getStride(lightBuffer);
  31. int normalStride = image_getStride(normalBuffer);
  32. for (int y = bound.top(); y < bound.bottom(); y++) {
  33. SafePointer<uint8_t> lightPixel = lightRow;
  34. SafePointer<uint32_t> normalPixel = normalRow;
  35. for (int x = bound.left(); x < bound.right(); x += laneCountX_32Bit) {
  36. // Read surface normals
  37. U32xX normalColor = U32xX::readAligned(normalPixel, "directedLight: reading normal");
  38. // TODO: Port SIMD3D to handle arbitrary vector lengths.
  39. F32xXx3 negativeSurfaceNormal = unpackRgb_U32xX_to_F32xXx3(normalColor) - 128.0f;
  40. // Calculate light intensity
  41. // Normalization and negation is already pre-multiplied into reverseLightDirection
  42. F32xX intensity = dotProduct(negativeSurfaceNormal, reverseLightDirection).clampLower(0.0f);
  43. F32xX red = intensity * colorR;
  44. F32xX green = intensity * colorG;
  45. F32xX blue = intensity * colorB;
  46. red = red.clampUpper(255.1f);
  47. green = green.clampUpper(255.1f);
  48. blue = blue.clampUpper(255.1f);
  49. // TODO: Let color packing handle arbitrary vector lengths.
  50. U8xX light = reinterpret_U8FromU32(packOrder_packBytes(truncateToU32(red), truncateToU32(green), truncateToU32(blue)));
  51. if (ADD_LIGHT) {
  52. addLight(lightPixel, light);
  53. } else {
  54. setLight(lightPixel, light);
  55. }
  56. lightPixel += laneCountX_8Bit;
  57. normalPixel += laneCountX_32Bit;
  58. }
  59. lightRow.increaseBytes(lightStride);
  60. normalRow.increaseBytes(normalStride);
  61. }
  62. });
  63. }
  64. void setDirectedLight(const OrthoView& camera, OrderedImageRgbaU8& lightBuffer, const OrderedImageRgbaU8& normalBuffer, const FVector3D& lightDirection, float lightIntensity, const ColorRgbI32& lightColor) {
  65. directedLight<false>(camera.normalToWorldSpace, lightBuffer, normalBuffer, lightDirection, lightIntensity, lightColor);
  66. }
  67. void addDirectedLight(const OrthoView& camera, OrderedImageRgbaU8& lightBuffer, const OrderedImageRgbaU8& normalBuffer, const FVector3D& lightDirection, float lightIntensity, const ColorRgbI32& lightColor) {
  68. directedLight<true>(camera.normalToWorldSpace, lightBuffer, normalBuffer, lightDirection, lightIntensity, lightColor);
  69. }
  70. static IRect calculateBound(const OrthoView& camera, const IVector2D& worldCenter, OrderedImageRgbaU8& lightBuffer, const FVector3D& lightSpacePosition, float lightRadius, int alignmentPixels) {
  71. // Get the light's 2D position in pixels
  72. FVector3D rotatedPosition = camera.lightSpaceToScreenDepth.transform(lightSpacePosition);
  73. IVector2D pixelCenter = IVector2D(rotatedPosition.x, rotatedPosition.y) + worldCenter;
  74. // Use the light-space X axis to convert the sphere's radius into pixels
  75. int pixelRadius = lightRadius * camera.lightSpaceToScreenDepth.xAxis.x;
  76. // Check if the location can be seen
  77. IRect imageBound = image_getBound(lightBuffer);
  78. if (pixelCenter.x < -pixelRadius
  79. || pixelCenter.x > imageBound.right() + pixelRadius
  80. || pixelCenter.y < -pixelRadius
  81. || pixelCenter.y > imageBound.bottom() + pixelRadius) {
  82. // The light source cannot be seen at all
  83. return IRect();
  84. }
  85. // Calculate the bound
  86. IRect result = IRect::cut(imageBound, IRect(pixelCenter.x - pixelRadius, pixelCenter.y - pixelRadius, pixelRadius * 2.0f, pixelRadius * 2.0f));
  87. // Round out to multiples of SIMD vectors
  88. if (result.hasArea() && alignmentPixels > 1) {
  89. int left = roundDown(result.left(), alignmentPixels);
  90. int right = roundUp(result.right(), alignmentPixels);
  91. result = IRect(left, result.top(), right - left, result.height());
  92. }
  93. return result;
  94. }
  95. // Returns:
  96. // 0.0 for blocked
  97. // 1.0 for passing
  98. // Values between 0.0 and 1.0 for fuzzy thresholding
  99. // Precondition: pixelData Does not contain any padding by using widths in multiples of 4 pixels
  100. static float getShadowTransparency(SafePointer<float> pixelData, int32_t width, float halfWidth, const FVector3D& lightOffset) {
  101. // Get lengths
  102. float absX = lightOffset.x; if (absX < 0.0f) { absX = -absX; }
  103. float absY = lightOffset.y; if (absY < 0.0f) { absY = -absY; }
  104. float absZ = lightOffset.z; if (absZ < 0.0f) { absZ = -absZ; }
  105. // Compare dimensions
  106. bool xIsLongest = absX > absY && absX > absZ;
  107. bool yIsLongerThanZ = absY > absZ;
  108. // Transform
  109. float depth = xIsLongest ? lightOffset.x : (yIsLongerThanZ ? lightOffset.y : lightOffset.z);
  110. float slopeUp = (yIsLongerThanZ && !xIsLongest) ? lightOffset.z : lightOffset.y;
  111. float slopeSide = xIsLongest ? -lightOffset.z : (yIsLongerThanZ ? -lightOffset.x : lightOffset.x);
  112. int32_t viewOffset = width * (xIsLongest ? 0 : (yIsLongerThanZ ? 2 : 4));
  113. bool negativeSide = depth < 0.0f;
  114. if (negativeSide) { depth = -depth; }
  115. if (negativeSide) { slopeSide = -slopeSide; }
  116. if (negativeSide) { viewOffset = viewOffset + width; }
  117. // Project and round to pixels
  118. float reciDepth = 1.0f / depth;
  119. float scale = halfWidth * reciDepth;
  120. int32_t sampleX = (int)(halfWidth + (slopeSide * scale));
  121. int32_t sampleY = (int)(halfWidth - (slopeUp * scale));
  122. // Clamp to local view coordinates
  123. int32_t maxPixel = width - 1;
  124. if (sampleX < 0) { sampleX = 0; }
  125. if (sampleX > maxPixel) { sampleX = maxPixel; }
  126. if (sampleY < 0) { sampleY = 0; }
  127. if (sampleY > maxPixel) { sampleY = maxPixel; }
  128. // Read the depth pixel
  129. float shadowReciDepth = pixelData[((sampleY + viewOffset) * width) + sampleX];
  130. // Apply biased thresholding
  131. return reciDepth * 1.02f > shadowReciDepth ? 1.0f : 0.0f;
  132. }
  133. static inline F32xX getShadowTransparency(SafePointer<float> pixelData, int32_t width, float halfWidth, const F32xXx3& lightOffset) {
  134. // TODO: Create a way to quickly iterate over elements in a SIMD vector for interfacing with scalar operations.
  135. ALIGN_BYTES(DSR_DEFAULT_ALIGNMENT) float offsetX[DSR_DEFAULT_VECTOR_SIZE];
  136. ALIGN_BYTES(DSR_DEFAULT_ALIGNMENT) float offsetY[DSR_DEFAULT_VECTOR_SIZE];
  137. ALIGN_BYTES(DSR_DEFAULT_ALIGNMENT) float offsetZ[DSR_DEFAULT_VECTOR_SIZE];
  138. lightOffset.v1.writeAlignedUnsafe(offsetX);
  139. lightOffset.v2.writeAlignedUnsafe(offsetY);
  140. lightOffset.v3.writeAlignedUnsafe(offsetZ);
  141. #if DSR_DEFAULT_VECTOR_SIZE == 16
  142. return F32x4(
  143. getShadowTransparency(pixelData, width, halfWidth, FVector3D(offsetX[0], offsetY[0], offsetZ[0])),
  144. getShadowTransparency(pixelData, width, halfWidth, FVector3D(offsetX[1], offsetY[1], offsetZ[1])),
  145. getShadowTransparency(pixelData, width, halfWidth, FVector3D(offsetX[2], offsetY[2], offsetZ[2])),
  146. getShadowTransparency(pixelData, width, halfWidth, FVector3D(offsetX[3], offsetY[3], offsetZ[3]))
  147. );
  148. #elif DSR_DEFAULT_VECTOR_SIZE == 32
  149. return F32x8(
  150. getShadowTransparency(pixelData, width, halfWidth, FVector3D(offsetX[0], offsetY[0], offsetZ[0])),
  151. getShadowTransparency(pixelData, width, halfWidth, FVector3D(offsetX[1], offsetY[1], offsetZ[1])),
  152. getShadowTransparency(pixelData, width, halfWidth, FVector3D(offsetX[2], offsetY[2], offsetZ[2])),
  153. getShadowTransparency(pixelData, width, halfWidth, FVector3D(offsetX[3], offsetY[3], offsetZ[3])),
  154. getShadowTransparency(pixelData, width, halfWidth, FVector3D(offsetX[4], offsetY[4], offsetZ[4])),
  155. getShadowTransparency(pixelData, width, halfWidth, FVector3D(offsetX[5], offsetY[5], offsetZ[5])),
  156. getShadowTransparency(pixelData, width, halfWidth, FVector3D(offsetX[6], offsetY[6], offsetZ[6])),
  157. getShadowTransparency(pixelData, width, halfWidth, FVector3D(offsetX[7], offsetY[7], offsetZ[7]))
  158. );
  159. #endif
  160. }
  161. template <bool SHADOW_CASTING>
  162. static void addPointLightSuper(const OrthoView& camera, const IVector2D& worldCenter, OrderedImageRgbaU8& lightBuffer, const OrderedImageRgbaU8& normalBuffer, const AlignedImageF32& heightBuffer, const FVector3D& lightPosition, float lightRadius, float lightIntensity, const ColorRgbI32& lightColor, const AlignedImageF32& shadowCubeMap) {
  163. // Rotate the light position from relative space to light space
  164. // Normal-space defines the rotation for light-space
  165. FVector3D lightSpaceSourcePosition = camera.normalToWorldSpace.transformTransposed(lightPosition);
  166. // Align the rectangle with 8 pixels, because that's the widest read to align in the 16-bit height buffer
  167. IRect rectangleBound = calculateBound(camera, worldCenter, lightBuffer, lightSpaceSourcePosition, lightRadius, laneCountX_32Bit);
  168. if (rectangleBound.hasArea()) {
  169. // Uniform values
  170. // How much closer to your face in light-space does the pixel go per depth unit
  171. F32xXx3 inYourFaceAxis = F32xXx3(camera.screenDepthToLightSpace.zAxis);
  172. // Light color
  173. float colorR = std::max(0.0f, (float)lightColor.red * lightIntensity);
  174. float colorG = std::max(0.0f, (float)lightColor.green * lightIntensity);
  175. float colorB = std::max(0.0f, (float)lightColor.blue * lightIntensity);
  176. float reciprocalRadius = 1.0f / lightRadius;
  177. threadedSplit(rectangleBound, [
  178. lightBuffer, normalBuffer, heightBuffer, camera, worldCenter, inYourFaceAxis, lightSpaceSourcePosition,
  179. reciprocalRadius, colorR, colorG, colorB, shadowCubeMap](const IRect& bound) mutable {
  180. // Initiate the local light-space sweep along base height
  181. // The local light space is rotated like normal-space but has the origin at the light source
  182. FVector3D lightBaseRow = camera.screenDepthToLightSpace.transform(FVector3D(0.5f - (float)worldCenter.x + bound.left(), 0.5f - (float)worldCenter.y + bound.top(), 0.0f)) - lightSpaceSourcePosition;
  183. FVector3D dx = camera.screenDepthToLightSpace.xAxis;
  184. FVector3D dy = camera.screenDepthToLightSpace.yAxis;
  185. // Pack the offset for each of the 4 first pixels into a transposing constructor
  186. F32xXx3 lightBaseRowX = F32xXx3::createGradient(lightBaseRow, dx);
  187. // Derivatives for moving four pixels to the right in parallel
  188. // (n+0, y0), (n+1, y0), (n+2, y0), (n+3, y0) -> (n+4, y0), (n+5, y0), (n+6, y0), (n+7, y0)
  189. F32xXx3 dxX = F32xXx3(dx * (float)laneCountX_32Bit);
  190. // Derivatives for moving one pixel down in parallel
  191. // (x0, n+0), (x1, n+0), (x2, n+0), (x3, n+0)
  192. // -> (x0, n+1), (x1, n+1), (x2, n+1), (x3, n+1)
  193. F32xXx3 dy1 = F32xXx3(dy);
  194. // Get strides
  195. int lightStride = image_getStride(lightBuffer);
  196. int normalStride = image_getStride(normalBuffer);
  197. int heightStride = image_getStride(heightBuffer);
  198. // Get pointers
  199. SafePointer<uint8_t> lightRow = image_getSafePointer_channels(lightBuffer, bound.top()) + bound.left() * 4;
  200. SafePointer<uint32_t> normalRow = image_getSafePointer(normalBuffer, bound.top()) + bound.left();
  201. SafePointer<float> heightRow = image_getSafePointer(heightBuffer, bound.top()) + bound.left();
  202. // Get cube map for casting shadows
  203. int32_t shadowCubeWidth;
  204. SafePointer<float> shadowCubeData;
  205. float shadowCubeCenter;
  206. if (SHADOW_CASTING) {
  207. shadowCubeWidth = image_getWidth(shadowCubeMap); assert(shadowCubeWidth % laneCountX_32Bit == 0);
  208. shadowCubeData = image_getSafePointer(shadowCubeMap);
  209. shadowCubeCenter = (float)shadowCubeWidth * 0.5f;
  210. }
  211. // Loop over the pixels to add light
  212. for (int y = bound.top(); y < bound.bottom(); y++) {
  213. // Initiate the leftmost pixels before iterating to the right
  214. F32xXx3 lightBasePixelxX = lightBaseRowX;
  215. SafePointer<uint8_t> lightPixel = lightRow;
  216. SafePointer<uint32_t> normalPixel = normalRow;
  217. SafePointer<float> heightPixel = heightRow;
  218. // Iterate over 16-bit pixels 8 at a time
  219. for (int x = bound.left(); x < bound.right(); x += laneCountX_32Bit) {
  220. // Read pixel height
  221. F32xX depthOffset = F32xX::readAligned(heightPixel, "addPointLight: reading height");
  222. // Extrude the pixel using positive values towards the camera to represent another height
  223. // This will solve X and Z positions based on the height Y
  224. F32xXx3 lightOffset = lightBasePixelxX + (inYourFaceAxis * depthOffset);
  225. // Get the linear distance, divide by sphere radius and limit to length 1 at intensity 0
  226. F32xX lightRatio = min(F32xX(1.0f), length(lightOffset) * reciprocalRadius);
  227. // Read surface normal
  228. U32xX normalColor = U32xX::readAligned(normalPixel, "addPointLight: reading normal");
  229. // normalScale is used to negate the normals in advance so that opposing directions get positive values
  230. F32xXx3 negativeSurfaceNormal = (unpackRgb_U32xX_to_F32xXx3(normalColor) - 128.0f) * (-1.0f / 128.0f);
  231. // Fade from 0 to 1 using 1 - 2x + x²
  232. F32xX distanceIntensity = 1.0f - 2.0f * lightRatio + lightRatio * lightRatio;
  233. F32xX angleIntensity = max(F32xX(0.0f), dotProduct(normalize(lightOffset), negativeSurfaceNormal));
  234. F32xX intensity = angleIntensity * distanceIntensity;
  235. if (SHADOW_CASTING) {
  236. intensity = intensity * getShadowTransparency(shadowCubeData, shadowCubeWidth, shadowCubeCenter, lightOffset);
  237. }
  238. // TODO: Make an optimized version for white light replacing red, green and blue with a single LUMA
  239. F32xX red = intensity * colorR;
  240. F32xX green = intensity * colorG;
  241. F32xX blue = intensity * colorB;
  242. red = red.clampUpper(255.1f);
  243. green = green.clampUpper(255.1f);
  244. blue = blue.clampUpper(255.1f);
  245. // Add light to the image
  246. U8xX morelight = reinterpret_U8FromU32(packOrder_packBytes(truncateToU32(red), truncateToU32(green), truncateToU32(blue)));
  247. addLight(lightPixel, morelight);
  248. // Go to the next four pixels in light-space
  249. lightBasePixelxX += dxX;
  250. // Go to the next 4 pixels of image data
  251. lightPixel += laneCountX_8Bit;
  252. normalPixel += laneCountX_32Bit;
  253. heightPixel += laneCountX_32Bit;
  254. }
  255. // Go to the next row in light-space
  256. lightBaseRowX += dy1;
  257. // Go to the next row of image data
  258. lightRow.increaseBytes(lightStride);
  259. normalRow.increaseBytes(normalStride);
  260. heightRow.increaseBytes(heightStride);
  261. }
  262. });
  263. }
  264. }
  265. void addPointLight(const OrthoView& camera, const IVector2D& worldCenter, OrderedImageRgbaU8& lightBuffer, const OrderedImageRgbaU8& normalBuffer, const AlignedImageF32& heightBuffer, const FVector3D& lightPosition, float lightRadius, float lightIntensity, const ColorRgbI32& lightColor, const AlignedImageF32& shadowCubeMap) {
  266. if (image_exists(shadowCubeMap)) {
  267. addPointLightSuper<true>(camera, worldCenter, lightBuffer, normalBuffer, heightBuffer, lightPosition, lightRadius, lightIntensity, lightColor, shadowCubeMap);
  268. } else {
  269. addPointLightSuper<false>(camera, worldCenter, lightBuffer, normalBuffer, heightBuffer, lightPosition, lightRadius, lightIntensity, lightColor, AlignedImageF32());
  270. }
  271. }
  272. void addPointLight(const OrthoView& camera, const IVector2D& worldCenter, OrderedImageRgbaU8& lightBuffer, const OrderedImageRgbaU8& normalBuffer, const AlignedImageF32& heightBuffer, const FVector3D& lightPosition, float lightRadius, float lightIntensity, const ColorRgbI32& lightColor) {
  273. addPointLightSuper<false>(camera, worldCenter, lightBuffer, normalBuffer, heightBuffer, lightPosition, lightRadius, lightIntensity, lightColor, AlignedImageF32());
  274. }
  275. void blendLight(AlignedImageRgbaU8& colorBuffer, const OrderedImageRgbaU8& diffuseBuffer, const OrderedImageRgbaU8& lightBuffer) {
  276. PackOrder targetOrder = PackOrder::getPackOrder(image_getPackOrderIndex(colorBuffer));
  277. int width = image_getWidth(colorBuffer);
  278. int height = image_getHeight(colorBuffer);
  279. threadedSplit(0, height, [colorBuffer, diffuseBuffer, lightBuffer, targetOrder, width](int startIndex, int stopIndex) mutable {
  280. SafePointer<uint32_t> targetRow = image_getSafePointer(colorBuffer, startIndex);
  281. SafePointer<uint32_t> diffuseRow = image_getSafePointer(diffuseBuffer, startIndex);
  282. SafePointer<uint32_t> lightRow = image_getSafePointer(lightBuffer, startIndex);
  283. int targetStride = image_getStride(colorBuffer);
  284. int diffuseStride = image_getStride(diffuseBuffer);
  285. int lightStride = image_getStride(lightBuffer);
  286. F32xX scale = F32xX(1.0 / 128.0f);
  287. for (int y = startIndex; y < stopIndex; y++) {
  288. SafePointer<uint32_t> targetPixel = targetRow;
  289. SafePointer<uint32_t> diffusePixel = diffuseRow;
  290. SafePointer<uint32_t> lightPixel = lightRow;
  291. for (int x = 0; x < width; x += laneCountX_32Bit) {
  292. U32xX diffuse = U32xX::readAligned(diffusePixel, "blendLight: reading diffuse");
  293. U32xX light = U32xX::readAligned(lightPixel, "blendLight: reading light");
  294. F32xX red = (floatFromU32(packOrder_getRed(diffuse)) * floatFromU32(packOrder_getRed(light))) * scale;
  295. F32xX green = (floatFromU32(packOrder_getGreen(diffuse)) * floatFromU32(packOrder_getGreen(light))) * scale;
  296. F32xX blue = (floatFromU32(packOrder_getBlue(diffuse)) * floatFromU32(packOrder_getBlue(light))) * scale;
  297. red = red.clampUpper(255.1f);
  298. green = green.clampUpper(255.1f);
  299. blue = blue.clampUpper(255.1f);
  300. U32xX color = packOrder_packBytes(truncateToU32(red), truncateToU32(green), truncateToU32(blue), targetOrder);
  301. color.writeAligned(targetPixel, "blendLight: writing color");
  302. targetPixel += laneCountX_32Bit;
  303. diffusePixel += laneCountX_32Bit;
  304. lightPixel += laneCountX_32Bit;
  305. }
  306. targetRow.increaseBytes(targetStride);
  307. diffuseRow.increaseBytes(diffuseStride);
  308. lightRow.increaseBytes(lightStride);
  309. }
  310. });
  311. }
  312. }