using namespace std;
+namespace {
+
+int swizzle_orders[] =
+{
+ GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA,
+ GL_RED, GL_RED, GL_RED, GL_ONE,
+ GL_RED, GL_RED, GL_RED, GL_GREEN,
+ GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA
+};
+
+}
+
namespace Msp {
namespace GL {
+ComponentSwizzle get_required_swizzle(PixelComponents comp)
+{
+ switch(comp)
+ {
+ case BGR: return RGB_TO_BGR;
+ case BGRA: return RGB_TO_BGR;
+ case LUMINANCE: return R_TO_LUMINANCE;
+ case LUMINANCE_ALPHA: return RG_TO_LUMINANCE_ALPHA;
+ default: return NO_SWIZZLE;
+ }
+}
+
void require_pixelformat(PixelFormat pf)
{
/* TODO These checks are only accurate for textures. On OpenGL ES some
}
}
+const int *get_gl_swizzle(ComponentSwizzle swiz)
+{
+ return swizzle_orders+4*swiz;
+}
+
} // namespace GL
} // namespace Msp
unsigned get_gl_components(PixelComponents);
unsigned get_gl_pixelformat(PixelFormat);
+const int *get_gl_swizzle(ComponentSwizzle);
} // namespace GL
} // namespace Msp
namespace Msp {
namespace GL {
-int OpenGLTexture::swizzle_orders[] =
-{
- GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA,
- GL_RED, GL_RED, GL_RED, GL_ONE,
- GL_RED, GL_RED, GL_RED, GL_GREEN,
- GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA
-};
-
OpenGLTexture *OpenGLTexture::scratch_binding = 0;
OpenGLTexture::OpenGLTexture(unsigned t):
void OpenGLTexture::apply_swizzle()
{
- Texture::FormatSwizzle swizzle = static_cast<const Texture *>(this)->swizzle;
- if(swizzle==Texture::NO_SWIZZLE)
+ ComponentSwizzle swizzle = static_cast<const Texture *>(this)->swizzle;
+ if(swizzle==NO_SWIZZLE)
return;
+ const int *swizzle_order = get_gl_swizzle(swizzle);
if(get_backend_api()==OPENGL_ES)
{
- set_parameter_i(GL_TEXTURE_SWIZZLE_R, swizzle_orders[swizzle*4]);
- set_parameter_i(GL_TEXTURE_SWIZZLE_G, swizzle_orders[swizzle*4+1]);
- set_parameter_i(GL_TEXTURE_SWIZZLE_B, swizzle_orders[swizzle*4+2]);
- set_parameter_i(GL_TEXTURE_SWIZZLE_A, swizzle_orders[swizzle*4+3]);
+ set_parameter_i(GL_TEXTURE_SWIZZLE_R, swizzle_order[0]);
+ set_parameter_i(GL_TEXTURE_SWIZZLE_G, swizzle_order[1]);
+ set_parameter_i(GL_TEXTURE_SWIZZLE_B, swizzle_order[2]);
+ set_parameter_i(GL_TEXTURE_SWIZZLE_A, swizzle_order[3]);
}
else
{
if(ARB_direct_state_access)
- glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
+ glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_order);
else
- glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
+ glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_order);
}
}
unsigned target;
std::string debug_name;
- static int swizzle_orders[];
static OpenGLTexture *scratch_binding;
OpenGLTexture(unsigned);
}
}
+PixelComponents swizzle_components(PixelComponents comp, ComponentSwizzle swiz)
+{
+ if(swiz==NO_SWIZZLE)
+ return comp;
+ else if(comp==RED && swiz==R_TO_LUMINANCE)
+ return LUMINANCE;
+ else if(comp==RG && swiz==RG_TO_LUMINANCE_ALPHA)
+ return LUMINANCE_ALPHA;
+ else if(comp==RGB && swiz==RGB_TO_BGR)
+ return BGR;
+ else if(comp==RGBA && swiz==RGB_TO_BGR)
+ return BGRA;
+ else
+ throw invalid_argument("swizzle_components");
+}
+
+PixelComponents unswizzle_components(PixelComponents comp, ComponentSwizzle swiz)
+{
+ if(swiz==NO_SWIZZLE)
+ return comp;
+ else if(comp==LUMINANCE && swiz==R_TO_LUMINANCE)
+ return RED;
+ else if(comp==LUMINANCE_ALPHA && swiz==RG_TO_LUMINANCE_ALPHA)
+ return RG;
+ else if(comp==BGR && swiz==RGB_TO_BGR)
+ return RGB;
+ else if(comp==BGRA && swiz==RGB_TO_BGR)
+ return RGBA;
+ else
+ throw invalid_argument("swizzle_components");
+}
+
PixelFormat pixelformat_from_image(const Graphics::Image &image, bool srgb)
{
PixelComponents comp = components_from_graphics(image.get_format());
LUMINANCE_ALPHA = 0x42
};
+/**
+Describes a mapping from one set of components to another.
+*/
+enum ComponentSwizzle
+{
+ NO_SWIZZLE,
+ R_TO_LUMINANCE,
+ RG_TO_LUMINANCE_ALPHA,
+ RGB_TO_BGR
+};
+
/**
Identifies a pixel format, with components and type.
PixelComponents components_from_graphics(Graphics::PixelFormat);
PixelFormat pixelformat_from_image(const Graphics::Image &, bool = false);
+ComponentSwizzle get_required_swizzle(PixelComponents);
+PixelComponents swizzle_components(PixelComponents, ComponentSwizzle);
+PixelComponents unswizzle_components(PixelComponents, ComponentSwizzle);
+
PixelFormat make_pixelformat(PixelComponents, DataType, bool = false);
inline PixelComponents get_components(PixelFormat f) { return static_cast<PixelComponents>(f&0xFF); }
inline unsigned get_component_count(PixelComponents c) { return c&7; }
void Texture::set_format(PixelFormat fmt)
{
PixelComponents comp = get_components(fmt);
- PixelComponents st_comp = comp;
- FormatSwizzle swiz = NO_SWIZZLE;
- switch(comp)
- {
- case LUMINANCE:
- st_comp = RED;
- swiz = R_TO_LUMINANCE;
- break;
- case LUMINANCE_ALPHA:
- st_comp = RG;
- swiz = RG_TO_LUMINANCE_ALPHA;
- break;
- case BGR:
- st_comp = RGB;
- swiz = RGB_TO_BGR;
- break;
- case BGRA:
- st_comp = RGBA;
- swiz = RGB_TO_BGR;
- break;
- default:;
- }
+ ComponentSwizzle swiz = get_required_swizzle(comp);
+ PixelComponents st_comp = unswizzle_components(comp, swiz);
PixelFormat st_fmt = make_pixelformat(st_comp, get_component_type(fmt), is_srgb(fmt));
require_pixelformat(st_fmt);
};
protected:
- enum FormatSwizzle
- {
- NO_SWIZZLE,
- R_TO_LUMINANCE,
- RG_TO_LUMINANCE_ALPHA,
- RGB_TO_BGR
- };
-
PixelFormat format;
PixelFormat storage_fmt;
- FormatSwizzle swizzle;
+ ComponentSwizzle swizzle;
bool use_srgb_format;
bool auto_gen_mipmap;