RgbaMultiply.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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_SHADER_RGBA_MULTIPLY
  24. #define DFPSR_RENDER_SHADER_RGBA_MULTIPLY
  25. #include <stdio.h>
  26. #include <stdint.h>
  27. #include <cassert>
  28. #include <algorithm>
  29. #include "Shader.h"
  30. #include "../../image/ImageRgbaU8.h"
  31. namespace dsr {
  32. template <bool HAS_DIFFUSE_MAP, bool HAS_LIGHT_MAP, bool HAS_VERTEX_FADING, bool COLORLESS, bool DISABLE_MIPMAP>
  33. class Shader_RgbaMultiply : public Shader {
  34. private:
  35. const TextureRgba *diffuseMap; // The full diffuseMap mipmap pyramid to use without DISABLE_MIPMAP
  36. const TextureRgbaLayer *diffuseLayer; // Layer 0 of diffuseMap to use with DISABLE_MIPMAP
  37. const TextureRgbaLayer *lightLayer;
  38. // Planar format with each vector representing the three triangle corners
  39. const TriangleTexCoords texCoords;
  40. const TriangleColors colors;
  41. // Normalize the color product by pre-multiplying the vertex colors
  42. float getVertexScale() {
  43. float result = 255.0f; // Scale from normalized to byte for the output
  44. if (HAS_DIFFUSE_MAP) {
  45. result /= 255.0f; // Normalize the diffuse map from 0..255 to 0..1 by dividing the vertex color
  46. }
  47. if (HAS_LIGHT_MAP) {
  48. result /= 255.0f; // Normalize the light map from 0..255 to 0..1 by dividing the vertex color
  49. }
  50. return result;
  51. }
  52. explicit Shader_RgbaMultiply(const TriangleInput &triangleInput) :
  53. diffuseMap(triangleInput.diffuseImage ? &(triangleInput.diffuseImage->texture) : nullptr),
  54. diffuseLayer(triangleInput.diffuseImage ? &(triangleInput.diffuseImage->texture.mips[0]) : nullptr),
  55. lightLayer(triangleInput.lightImage ? &(triangleInput.lightImage->texture.mips[0]) : nullptr),
  56. texCoords(triangleInput.texCoords.getPositive()), colors(triangleInput.colors.getScaled(getVertexScale())) {
  57. // Texture coordinates must be on the positive side to allow using truncation as a floor function
  58. if (HAS_DIFFUSE_MAP) {
  59. // Incorrect tests?
  60. if (DISABLE_MIPMAP) {
  61. assert(this->diffuseLayer != nullptr); // Cannot sample null
  62. assert(this->diffuseLayer->exists()); // Cannot sample regular images
  63. } else {
  64. assert(this->diffuseMap != nullptr); // Cannot sample null
  65. assert(this->diffuseMap->exists()); // Cannot sample regular images
  66. }
  67. }
  68. if (HAS_LIGHT_MAP) {
  69. assert(this->lightLayer != nullptr); // Cannot sample null
  70. assert(this->lightLayer->exists()); // Cannot sample regular images
  71. }
  72. }
  73. public:
  74. // The process method to take a function pointer to.
  75. // Must have the same signature as drawCallbackTemplate in Shader.h.
  76. static void processTriangle(const TriangleInput &triangleInput, ImageRgbaU8Impl *colorBuffer, ImageF32Impl *depthBuffer, const ITriangle2D &triangle, const Projection &projection, const RowShape &shape, Filter filter) {
  77. Shader_RgbaMultiply tempShader(triangleInput);
  78. tempShader.fillShape(colorBuffer, depthBuffer, triangle, projection, shape, filter);
  79. }
  80. Rgba_F32 getPixels_2x2(const F32x4x3 &vertexWeights) const override {
  81. if (HAS_DIFFUSE_MAP && !HAS_LIGHT_MAP && COLORLESS) {
  82. // Optimized for diffuse only
  83. ALIGN16 F32x4 u1(shaderMethods::interpolate(this->texCoords.u1, vertexWeights));
  84. ALIGN16 F32x4 v1(shaderMethods::interpolate(this->texCoords.v1, vertexWeights));
  85. if (DISABLE_MIPMAP) {
  86. return shaderMethods::sample_F32<Interpolation::BL, false>(this->diffuseLayer, u1, v1);
  87. } else {
  88. return shaderMethods::sample_F32<Interpolation::BL, false>(this->diffuseMap, u1, v1);
  89. }
  90. } else if (HAS_LIGHT_MAP && !HAS_DIFFUSE_MAP && COLORLESS) {
  91. // Optimized for light only
  92. ALIGN16 F32x4 u2(shaderMethods::interpolate(this->texCoords.u2, vertexWeights));
  93. ALIGN16 F32x4 v2(shaderMethods::interpolate(this->texCoords.v2, vertexWeights));
  94. return shaderMethods::sample_F32<Interpolation::BL, false>(this->lightLayer, u2, v2);
  95. } else {
  96. // Interpolate the vertex color
  97. ALIGN16 Rgba_F32 color = HAS_VERTEX_FADING ?
  98. shaderMethods::interpolateVertexColor(this->colors.red, this->colors.green, this->colors.blue, this->colors.alpha, vertexWeights) :
  99. Rgba_F32(F32x4(this->colors.red.x), F32x4(this->colors.green.x), F32x4(this->colors.blue.x), F32x4(this->colors.alpha.x));
  100. // Sample diffuse
  101. if (HAS_DIFFUSE_MAP) {
  102. ALIGN16 F32x4 u1(shaderMethods::interpolate(this->texCoords.u1, vertexWeights));
  103. ALIGN16 F32x4 v1(shaderMethods::interpolate(this->texCoords.v1, vertexWeights));
  104. if (DISABLE_MIPMAP) {
  105. color = color * shaderMethods::sample_F32<Interpolation::BL, false>(this->diffuseLayer, u1, v1);
  106. } else {
  107. color = color * shaderMethods::sample_F32<Interpolation::BL, false>(this->diffuseMap, u1, v1);
  108. }
  109. }
  110. // Sample lightmap
  111. if (HAS_LIGHT_MAP) {
  112. ALIGN16 F32x4 u2(shaderMethods::interpolate(this->texCoords.u2, vertexWeights));
  113. ALIGN16 F32x4 v2(shaderMethods::interpolate(this->texCoords.v2, vertexWeights));
  114. color = color * shaderMethods::sample_F32<Interpolation::BL, false>(this->lightLayer, u2, v2);
  115. }
  116. return color;
  117. }
  118. }
  119. };
  120. }
  121. #endif