michael@0: // michael@0: // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: // generatemip.h: Defines the GenerateMip function, templated on the format michael@0: // type of the image for which mip levels are being generated. michael@0: michael@0: #ifndef LIBGLESV2_RENDERER_GENERATEMIP_H_ michael@0: #define LIBGLESV2_RENDERER_GENERATEMIP_H_ michael@0: michael@0: #include "libGLESv2/mathutil.h" michael@0: michael@0: namespace rx michael@0: { michael@0: struct L8 michael@0: { michael@0: unsigned char L; michael@0: michael@0: static void average(L8 *dst, const L8 *src1, const L8 *src2) michael@0: { michael@0: dst->L = ((src1->L ^ src2->L) >> 1) + (src1->L & src2->L); michael@0: } michael@0: }; michael@0: michael@0: typedef L8 R8; // R8 type is functionally equivalent for mip purposes michael@0: typedef L8 A8; // A8 type is functionally equivalent for mip purposes michael@0: michael@0: struct A8L8 michael@0: { michael@0: unsigned char L; michael@0: unsigned char A; michael@0: michael@0: static void average(A8L8 *dst, const A8L8 *src1, const A8L8 *src2) michael@0: { michael@0: *(unsigned short*)dst = (((*(unsigned short*)src1 ^ *(unsigned short*)src2) & 0xFEFE) >> 1) + (*(unsigned short*)src1 & *(unsigned short*)src2); michael@0: } michael@0: }; michael@0: michael@0: typedef A8L8 R8G8; // R8G8 type is functionally equivalent for mip purposes michael@0: michael@0: struct A8R8G8B8 michael@0: { michael@0: unsigned char B; michael@0: unsigned char G; michael@0: unsigned char R; michael@0: unsigned char A; michael@0: michael@0: static void average(A8R8G8B8 *dst, const A8R8G8B8 *src1, const A8R8G8B8 *src2) michael@0: { michael@0: *(unsigned int*)dst = (((*(unsigned int*)src1 ^ *(unsigned int*)src2) & 0xFEFEFEFE) >> 1) + (*(unsigned int*)src1 & *(unsigned int*)src2); michael@0: } michael@0: }; michael@0: michael@0: typedef A8R8G8B8 R8G8B8A8; // R8G8B8A8 type is functionally equivalent for mip purposes michael@0: michael@0: struct A16B16G16R16F michael@0: { michael@0: unsigned short R; michael@0: unsigned short G; michael@0: unsigned short B; michael@0: unsigned short A; michael@0: michael@0: static void average(A16B16G16R16F *dst, const A16B16G16R16F *src1, const A16B16G16R16F *src2) michael@0: { michael@0: dst->R = gl::float32ToFloat16((gl::float16ToFloat32(src1->R) + gl::float16ToFloat32(src2->R)) * 0.5f); michael@0: dst->G = gl::float32ToFloat16((gl::float16ToFloat32(src1->G) + gl::float16ToFloat32(src2->G)) * 0.5f); michael@0: dst->B = gl::float32ToFloat16((gl::float16ToFloat32(src1->B) + gl::float16ToFloat32(src2->B)) * 0.5f); michael@0: dst->A = gl::float32ToFloat16((gl::float16ToFloat32(src1->A) + gl::float16ToFloat32(src2->A)) * 0.5f); michael@0: } michael@0: }; michael@0: michael@0: struct R16F michael@0: { michael@0: unsigned short R; michael@0: michael@0: static void average(R16F *dst, const R16F *src1, const R16F *src2) michael@0: { michael@0: dst->R = gl::float32ToFloat16((gl::float16ToFloat32(src1->R) + gl::float16ToFloat32(src2->R)) * 0.5f); michael@0: } michael@0: }; michael@0: michael@0: struct R16G16F michael@0: { michael@0: unsigned short R; michael@0: unsigned short G; michael@0: michael@0: static void average(R16G16F *dst, const R16G16F *src1, const R16G16F *src2) michael@0: { michael@0: dst->R = gl::float32ToFloat16((gl::float16ToFloat32(src1->R) + gl::float16ToFloat32(src2->R)) * 0.5f); michael@0: dst->G = gl::float32ToFloat16((gl::float16ToFloat32(src1->G) + gl::float16ToFloat32(src2->G)) * 0.5f); michael@0: } michael@0: }; michael@0: michael@0: struct A32B32G32R32F michael@0: { michael@0: float R; michael@0: float G; michael@0: float B; michael@0: float A; michael@0: michael@0: static void average(A32B32G32R32F *dst, const A32B32G32R32F *src1, const A32B32G32R32F *src2) michael@0: { michael@0: dst->R = (src1->R + src2->R) * 0.5f; michael@0: dst->G = (src1->G + src2->G) * 0.5f; michael@0: dst->B = (src1->B + src2->B) * 0.5f; michael@0: dst->A = (src1->A + src2->A) * 0.5f; michael@0: } michael@0: }; michael@0: michael@0: struct R32F michael@0: { michael@0: float R; michael@0: michael@0: static void average(R32F *dst, const R32F *src1, const R32F *src2) michael@0: { michael@0: dst->R = (src1->R + src2->R) * 0.5f; michael@0: } michael@0: }; michael@0: michael@0: struct R32G32F michael@0: { michael@0: float R; michael@0: float G; michael@0: michael@0: static void average(R32G32F *dst, const R32G32F *src1, const R32G32F *src2) michael@0: { michael@0: dst->R = (src1->R + src2->R) * 0.5f; michael@0: dst->G = (src1->G + src2->G) * 0.5f; michael@0: } michael@0: }; michael@0: michael@0: struct R32G32B32F michael@0: { michael@0: float R; michael@0: float G; michael@0: float B; michael@0: michael@0: static void average(R32G32B32F *dst, const R32G32B32F *src1, const R32G32B32F *src2) michael@0: { michael@0: dst->R = (src1->R + src2->R) * 0.5f; michael@0: dst->G = (src1->G + src2->G) * 0.5f; michael@0: dst->B = (src1->B + src2->B) * 0.5f; michael@0: } michael@0: }; michael@0: michael@0: template michael@0: static void GenerateMip(unsigned int sourceWidth, unsigned int sourceHeight, michael@0: const unsigned char *sourceData, int sourcePitch, michael@0: unsigned char *destData, int destPitch) michael@0: { michael@0: unsigned int mipWidth = std::max(1U, sourceWidth >> 1); michael@0: unsigned int mipHeight = std::max(1U, sourceHeight >> 1); michael@0: michael@0: if (sourceHeight == 1) michael@0: { michael@0: ASSERT(sourceWidth != 1); michael@0: michael@0: const T *src = (const T*)sourceData; michael@0: T *dst = (T*)destData; michael@0: michael@0: for (unsigned int x = 0; x < mipWidth; x++) michael@0: { michael@0: T::average(&dst[x], &src[x * 2], &src[x * 2 + 1]); michael@0: } michael@0: } michael@0: else if (sourceWidth == 1) michael@0: { michael@0: ASSERT(sourceHeight != 1); michael@0: michael@0: for (unsigned int y = 0; y < mipHeight; y++) michael@0: { michael@0: const T *src0 = (const T*)(sourceData + y * 2 * sourcePitch); michael@0: const T *src1 = (const T*)(sourceData + y * 2 * sourcePitch + sourcePitch); michael@0: T *dst = (T*)(destData + y * destPitch); michael@0: michael@0: T::average(dst, src0, src1); michael@0: } michael@0: } michael@0: else michael@0: { michael@0: for (unsigned int y = 0; y < mipHeight; y++) michael@0: { michael@0: const T *src0 = (const T*)(sourceData + y * 2 * sourcePitch); michael@0: const T *src1 = (const T*)(sourceData + y * 2 * sourcePitch + sourcePitch); michael@0: T *dst = (T*)(destData + y * destPitch); michael@0: michael@0: for (unsigned int x = 0; x < mipWidth; x++) michael@0: { michael@0: T tmp0; michael@0: T tmp1; michael@0: michael@0: T::average(&tmp0, &src0[x * 2], &src0[x * 2 + 1]); michael@0: T::average(&tmp1, &src1[x * 2], &src1[x * 2 + 1]); michael@0: T::average(&dst[x], &tmp0, &tmp1); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: #endif // LIBGLESV2_RENDERER_GENERATEMIP_H_