Shader.h 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2017 to 2019 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #ifndef DFPSR_RENDER_RENDERING
  24. #define DFPSR_RENDER_RENDERING
  25. #include <stdint.h>
  26. #include "../../image/PackOrder.h"
  27. #include "../../image/ImageRgbaU8.h"
  28. #include "../../image/ImageF32.h"
  29. #include "../ITriangle2D.h"
  30. #include "shaderMethods.h"
  31. #include "shaderTypes.h"
  32. namespace dsr {
  33. inline float getMinimum(const FVector3D &coordinates) {
  34. float result = coordinates.x;
  35. if (coordinates.y < result) { result = coordinates.y; }
  36. if (coordinates.z < result) { result = coordinates.z; }
  37. return result;
  38. }
  39. struct TriangleTexCoords {
  40. FVector3D u1, v1, u2, v2;
  41. TriangleTexCoords() {}
  42. TriangleTexCoords(const FVector3D &u1, const FVector3D &v1, const FVector3D &u2, const FVector3D &v2) :
  43. u1(u1), v1(v1), u2(u2), v2(v2) {}
  44. TriangleTexCoords(const FVector4D &a, const FVector4D &b, const FVector4D &c) :
  45. u1(FVector3D(a.x, b.x, c.x)), v1(FVector3D(a.y, b.y, c.y)), u2(FVector3D(a.z, b.z, c.z)), v2(FVector3D(a.w, b.w, c.w)) {}
  46. TriangleTexCoords getPositive() const {
  47. return TriangleTexCoords(
  48. this->u1 + FVector3D(1 - (int)getMinimum(this->u1)),
  49. this->v1 + FVector3D(1 - (int)getMinimum(this->v1)),
  50. this->u2 + FVector3D(1 - (int)getMinimum(this->u2)),
  51. this->v2 + FVector3D(1 - (int)getMinimum(this->v2))
  52. );
  53. }
  54. };
  55. struct TriangleColors {
  56. FVector3D red, green, blue, alpha;
  57. TriangleColors() {}
  58. explicit TriangleColors(float monochrome) : red(monochrome), green(monochrome), blue(monochrome), alpha(monochrome) {}
  59. TriangleColors(const FVector3D &red, const FVector3D &green, const FVector3D &blue, const FVector3D &alpha) : red(red), green(green), blue(blue), alpha(alpha) {}
  60. TriangleColors(const FVector4D &a, const FVector4D &b, const FVector4D &c) :
  61. red(FVector3D(a.x, b.x, c.x)), green(FVector3D(a.y, b.y, c.y)), blue(FVector3D(a.z, b.z, c.z)), alpha(FVector3D(a.w, b.w, c.w)) {}
  62. TriangleColors getScaled(float scalar) const {
  63. return TriangleColors(this->red * scalar, this->green * scalar, this->blue * scalar, this->alpha * scalar);
  64. }
  65. };
  66. struct TriangleInput {
  67. const ImageRgbaU8Impl *diffuseImage;
  68. const ImageRgbaU8Impl *lightImage;
  69. const TriangleTexCoords texCoords;
  70. const TriangleColors colors;
  71. TriangleInput(const ImageRgbaU8Impl *diffuseImage, const ImageRgbaU8Impl *lightImage, const TriangleTexCoords &texCoords, const TriangleColors &colors)
  72. : diffuseImage(diffuseImage), lightImage(lightImage), texCoords(texCoords), colors(colors) {}
  73. };
  74. // The template for function pointers doing the work
  75. inline void drawCallbackTemplate(const TriangleInput &triangleInput, ImageRgbaU8Impl *colorBuffer, ImageF32Impl *depthBuffer, const ITriangle2D &triangle, const Projection &projection, const RowShape &shape, Filter filter) {}
  76. #define DRAW_CALLBACK_TYPE decltype(&drawCallbackTemplate)
  77. // Inherit this class for pixel shaders
  78. class Shader {
  79. public:
  80. void fillShape(ImageRgbaU8Impl *colorBuffer, ImageF32Impl *depthBuffer, const ITriangle2D &triangle, const Projection &projection, const RowShape &shape, Filter filter);
  81. // The main call that defines the pixel shader
  82. virtual Rgba_F32 getPixels_2x2(const F32x4x3 &vertexWeights) const = 0;
  83. };
  84. }
  85. #endif