util: fix a typo in util_format_swizzle_4f
[mesa.git] / src / gallium / auxiliary / util / u_format.c
index c50c807eb8997d589f55d2f5602e74555c318ccf..34922ab18ab545177df5acd2fd10f2385b26e649 100644 (file)
 #include "u_memory.h"
 #include "u_rect.h"
 #include "u_format.h"
+#include "u_format_s3tc.h"
+
+#include "pipe/p_defines.h"
+
+
+boolean
+util_format_is_float(enum pipe_format format)
+{
+   const struct util_format_description *desc = util_format_description(format);
+   unsigned i;
+
+   assert(desc);
+   if (!desc) {
+      return FALSE;
+   }
+
+   /* Find the first non-void channel. */
+   for (i = 0; i < 4; i++) {
+      if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
+         break;
+      }
+   }
+
+   if (i == 4) {
+      return FALSE;
+   }
+
+   return desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT ? TRUE : FALSE;
+}
+
+
+boolean
+util_format_is_supported(enum pipe_format format, unsigned bind)
+{
+   if (util_format_is_s3tc(format) && !util_format_s3tc_enabled) {
+      return FALSE;
+   }
+
+#ifndef TEXTURE_FLOAT_ENABLED
+   if ((bind & PIPE_BIND_RENDER_TARGET) &&
+       format != PIPE_FORMAT_R9G9B9E5_FLOAT &&
+       format != PIPE_FORMAT_R11G11B10_FLOAT &&
+       util_format_is_float(format)) {
+      return FALSE;
+   }
+#endif
+
+   return TRUE;
+}
 
 
 void
@@ -120,11 +169,67 @@ util_format_write_4ub(enum pipe_format format, const uint8_t *src, unsigned src_
 }
 
 
-static INLINE boolean
+boolean
+util_is_format_compatible(const struct util_format_description *src_desc,
+                          const struct util_format_description *dst_desc)
+{
+   unsigned chan;
+
+   if (src_desc->format == dst_desc->format) {
+      return TRUE;
+   }
+
+   if (src_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN ||
+       dst_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN) {
+      return FALSE;
+   }
+
+   if (src_desc->block.bits != dst_desc->block.bits ||
+       src_desc->nr_channels != dst_desc->nr_channels ||
+       src_desc->colorspace != dst_desc->colorspace) {
+      return FALSE;
+   }
+
+   for (chan = 0; chan < 4; ++chan) {
+      if (src_desc->channel[chan].size !=
+          dst_desc->channel[chan].size) {
+         return FALSE;
+      }
+   }
+
+   for (chan = 0; chan < 4; ++chan) {
+      enum util_format_swizzle swizzle = dst_desc->swizzle[chan];
+
+      if (swizzle < 4) {
+         if (src_desc->swizzle[chan] != swizzle) {
+            return FALSE;
+         }
+         if ((src_desc->channel[swizzle].type !=
+              dst_desc->channel[swizzle].type) ||
+             (src_desc->channel[swizzle].normalized !=
+              dst_desc->channel[swizzle].normalized)) {
+            return FALSE;
+         }
+      }
+   }
+
+   return TRUE;
+}
+
+
+boolean
 util_format_fits_8unorm(const struct util_format_description *format_desc)
 {
    unsigned chan;
 
+   /*
+    * After linearized sRGB values require more than 8bits.
+    */
+
+   if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
+      return FALSE;
+   }
+
    switch (format_desc->layout) {
 
    case UTIL_FORMAT_LAYOUT_S3TC:
@@ -189,11 +294,14 @@ util_format_translate(enum pipe_format dst_format,
    const struct util_format_description *src_format_desc;
    uint8_t *dst_row;
    const uint8_t *src_row;
-   unsigned y_step;
+   unsigned x_step, y_step;
    unsigned dst_step;
    unsigned src_step;
 
-   if (dst_format == src_format) {
+   dst_format_desc = util_format_description(dst_format);
+   src_format_desc = util_format_description(src_format);
+
+   if (util_is_format_compatible(src_format_desc, dst_format_desc)) {
       /*
        * Trivial case.
        */
@@ -204,9 +312,6 @@ util_format_translate(enum pipe_format dst_format,
       return;
    }
 
-   dst_format_desc = util_format_description(dst_format);
-   src_format_desc = util_format_description(src_format);
-
    assert(dst_x % dst_format_desc->block.width == 0);
    assert(dst_y % dst_format_desc->block.height == 0);
    assert(src_x % src_format_desc->block.width == 0);
@@ -221,6 +326,7 @@ util_format_translate(enum pipe_format dst_format,
     */
 
    y_step = MAX2(dst_format_desc->block.height, src_format_desc->block.height);
+   x_step = MAX2(dst_format_desc->block.width, src_format_desc->block.width);
    assert(y_step % dst_format_desc->block.height == 0);
    assert(y_step % src_format_desc->block.height == 0);
 
@@ -237,7 +343,7 @@ util_format_translate(enum pipe_format dst_format,
       unsigned tmp_stride;
       uint8_t *tmp_row;
 
-      tmp_stride = width * 4 * sizeof *tmp_row;
+      tmp_stride = MAX2(width, x_step) * 4 * sizeof *tmp_row;
       tmp_row = MALLOC(y_step * tmp_stride);
       if (!tmp_row)
          return;
@@ -262,7 +368,7 @@ util_format_translate(enum pipe_format dst_format,
       unsigned tmp_stride;
       float *tmp_row;
 
-      tmp_stride = width * 4 * sizeof *tmp_row;
+      tmp_stride = MAX2(width, x_step) * 4 * sizeof *tmp_row;
       tmp_row = MALLOC(y_step * tmp_stride);
       if (!tmp_row)
          return;
@@ -284,3 +390,53 @@ util_format_translate(enum pipe_format dst_format,
       FREE(tmp_row);
    }
 }
+
+void util_format_compose_swizzles(const unsigned char swz1[4],
+                                  const unsigned char swz2[4],
+                                  unsigned char dst[4])
+{
+   unsigned i;
+
+   for (i = 0; i < 4; i++) {
+      dst[i] = swz2[i] <= UTIL_FORMAT_SWIZZLE_W ?
+               swz1[swz2[i]] : swz2[i];
+   }
+}
+
+void util_format_swizzle_4f(float *dst, const float *src,
+                            const unsigned char swz[4])
+{
+   unsigned i;
+
+   for (i = 0; i < 4; i++) {
+      if (swz[i] <= UTIL_FORMAT_SWIZZLE_W)
+         dst[i] = src[swz[i]];
+      else if (swz[i] == UTIL_FORMAT_SWIZZLE_0)
+         dst[i] = 0;
+      else if (swz[i] == UTIL_FORMAT_SWIZZLE_1)
+         dst[i] = 1;
+   }
+}
+
+void util_format_unswizzle_4f(float *dst, const float *src,
+                              const unsigned char swz[4])
+{
+   unsigned i;
+
+   for (i = 0; i < 4; i++) {
+      switch (swz[i]) {
+      case UTIL_FORMAT_SWIZZLE_X:
+         dst[0] = src[i];
+         break;
+      case UTIL_FORMAT_SWIZZLE_Y:
+         dst[1] = src[i];
+         break;
+      case UTIL_FORMAT_SWIZZLE_Z:
+         dst[2] = src[i];
+         break;
+      case UTIL_FORMAT_SWIZZLE_W:
+         dst[3] = src[i];
+         break;
+      }
+   }
+}