/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#ifndef HEADLESS
#include <jlong.h>
#include "OGLBufImgOps.h"
#include "OGLContext.h"
#include "OGLRenderQueue.h"
#include "OGLSurfaceData.h"
#include "GraphicsPrimitiveMgr.h"
/** Evaluates to true if the given bit is set on the local flags variable. */
/**************************** ConvolveOp support ****************************/
/**
* The ConvolveOp shader is fairly straightforward. For each texel in
* the source texture, the shader samples the MxN texels in the surrounding
* area, multiplies each by its corresponding kernel value, and then sums
* them all together to produce a single color result. Finally, the
* resulting value is multiplied by the current OpenGL color, which contains
* the extra alpha value.
*
* Note that this shader source code includes some "holes" marked by "%s".
* This allows us to build different shader programs (e.g. one for
* 3x3, one for 5x5, and so on) simply by filling in these "holes" with
* a call to sprintf(). See the OGLBufImgOps_CreateConvolveProgram() method
* for more details.
*
* REMIND: Currently this shader (and the supporting code in the
* EnableConvolveOp() method) only supports 3x3 and 5x5 filters.
* Early shader-level hardware did not support non-constant sized
* arrays but modern hardware should support them (although I
* don't know of any simple way to find out, other than to compile
* the shader at runtime and see if the drivers complain).
*/
static const char *convolveShaderSource =
// maximum size supported by this shader
"const int MAX_KERNEL_SIZE = %s;"
// image to be convolved
"uniform sampler%s baseImage;"
// image edge limits:
// imgEdge.xy = imgMin.xy (anything < will be treated as edge case)
// imgEdge.zw = imgMax.xy (anything > will be treated as edge case)
"uniform vec4 imgEdge;"
// value for each location in the convolution kernel:
// kernelVals[i].x = offsetX[i]
// kernelVals[i].y = offsetY[i]
// kernelVals[i].z = kernel[i]
"uniform vec3 kernelVals[MAX_KERNEL_SIZE];"
""
"void main(void)"
"{"
" int i;"
" vec4 sum;"
""
" if (any(lessThan(gl_TexCoord[0].st, imgEdge.xy)) ||"
" any(greaterThan(gl_TexCoord[0].st, imgEdge.zw)))"
" {"
// (placeholder for edge condition code)
" %s"
" } else {"
" sum = vec4(0.0);"
" for (i = 0; i < MAX_KERNEL_SIZE; i++) {"
" sum +="
" kernelVals[i].z *"
" texture%s(baseImage,"
" gl_TexCoord[0].st + kernelVals[i].xy);"
" }"
" }"
""
// modulate with gl_Color in order to apply extra alpha
" gl_FragColor = sum * gl_Color;"
"}";
/**
* Flags that can be bitwise-or'ed together to control how the shader
* source code is generated.
*/
/**
* The handles to the ConvolveOp fragment program objects. The index to
* the array should be a bitwise-or'ing of the CONVOLVE_* flags defined
* above. Note that most applications will likely need to initialize one
* or two of these elements, so the array is usually sparsely populated.
*/
/**
* The maximum kernel size supported by the ConvolveOp shader.
*/
/**
* Compiles and links the ConvolveOp shader program. If successful, this
* function returns a handle to the newly created shader program; otherwise
* returns 0.
*/
static GLhandleARB
{
"OGLBufImgOps_CreateConvolveProgram: flags=%d",
flags);
if (IS_SET(CONVOLVE_EDGE_ZERO_FILL)) {
// EDGE_ZERO_FILL: fill in zero at the edges
} else {
// EDGE_NO_OP: use the source pixel color at the edges
"sum = texture%s(baseImage, gl_TexCoord[0].st);",
target);
}
// compose the final source code string from the various pieces
if (convolveProgram == 0) {
"OGLBufImgOps_CreateConvolveProgram: error creating program");
return 0;
}
// "use" the program object temporarily so that we can set the uniforms
// set the "uniform" texture unit binding
// "unuse" the program object; it will be re-bound later as needed
return convolveProgram;
}
void
unsigned char *kernel)
{
"OGLBufImgOps_EnableConvolveOp: kernelW=%d kernelH=%d",
flags |= CONVOLVE_RECT;
// for GL_TEXTURE_RECTANGLE_ARB, texcoords are specified in the
// range [0,srcw] and [0,srch], so to achieve an x/y offset of
// exactly one pixel we simply use the value 1 here
xoff = 1.0f;
yoff = 1.0f;
} else {
// for GL_TEXTURE_2D, texcoords are specified in the range [0,1],
// so to achieve an x/y offset of approximately one pixel we have
// to normalize to that range here
}
if (edgeZeroFill) {
}
flags |= CONVOLVE_5X5;
}
// locate/initialize the shader program for the given flags
if (convolvePrograms[flags] == 0) {
if (convolvePrograms[flags] == 0) {
// shouldn't happen, but just in case...
return;
}
}
// enable the convolve shader
// texcoords are in the range [0,srcw] and [0,srch]
} else {
// texcoords are in the range [0,1]
}
// update the "uniform" kernel offsets and values
kIndex = 0;
kIndex += 3;
}
}
}
void
{
// disable the ConvolveOp shader
}
/**************************** RescaleOp support *****************************/
/**
* The RescaleOp shader is one of the simplest possible. Each fragment
* from the source image is multiplied by the user's scale factor and added
* to the user's offset value (these are component-wise operations).
* Finally, the resulting value is multiplied by the current OpenGL color,
* which contains the extra alpha value.
*
* The RescaleOp spec says that the operation is performed regardless of
* whether the source data is premultiplied or non-premultiplied. This is
* a problem for the OpenGL pipeline in that a non-premultiplied
* BufferedImage will have already been converted into premultiplied
* when uploaded to an OpenGL texture. Therefore, we have a special mode
* called RESCALE_NON_PREMULT (used only for source images that were
* originally non-premultiplied) that un-premultiplies the source color
* prior to the rescale operation, then re-premultiplies the resulting
* color before returning from the fragment shader.
*
* Note that this shader source code includes some "holes" marked by "%s".
* This allows us to build different shader programs (e.g. one for
* GL_TEXTURE_2D targets, one for GL_TEXTURE_RECTANGLE_ARB targets, and so on)
* simply by filling in these "holes" with a call to sprintf(). See the
* OGLBufImgOps_CreateRescaleProgram() method for more details.
*/
static const char *rescaleShaderSource =
// image to be rescaled
"uniform sampler%s baseImage;"
// vector containing scale factors
"uniform vec4 scaleFactors;"
// vector containing offsets
"uniform vec4 offsets;"
""
"void main(void)"
"{"
" vec4 srcColor = texture%s(baseImage, gl_TexCoord[0].st);"
// (placeholder for un-premult code)
" %s"
// rescale source value
" vec4 result = (srcColor * scaleFactors) + offsets;"
// (placeholder for re-premult code)
" %s"
// modulate with gl_Color in order to apply extra alpha
" gl_FragColor = result * gl_Color;"
"}";
/**
* Flags that can be bitwise-or'ed together to control how the shader
* source code is generated.
*/
/**
* The handles to the RescaleOp fragment program objects. The index to
* the array should be a bitwise-or'ing of the RESCALE_* flags defined
* above. Note that most applications will likely need to initialize one
* or two of these elements, so the array is usually sparsely populated.
*/
/**
* Compiles and links the RescaleOp shader program. If successful, this
* function returns a handle to the newly created shader program; otherwise
* returns 0.
*/
static GLhandleARB
{
"OGLBufImgOps_CreateRescaleProgram: flags=%d",
flags);
if (IS_SET(RESCALE_NON_PREMULT)) {
preRescale = "srcColor.rgb /= srcColor.a;";
postRescale = "result.rgb *= result.a;";
}
// compose the final source code string from the various pieces
if (rescaleProgram == 0) {
"OGLBufImgOps_CreateRescaleProgram: error creating program");
return 0;
}
// "use" the program object temporarily so that we can set the uniforms
// set the "uniform" values
// "unuse" the program object; it will be re-bound later as needed
return rescaleProgram;
}
void
unsigned char *scaleFactors,
unsigned char *offsets)
{
// choose the appropriate shader, depending on the source texture target
flags |= RESCALE_RECT;
}
if (nonPremult) {
}
// locate/initialize the shader program for the given flags
if (rescalePrograms[flags] == 0) {
if (rescalePrograms[flags] == 0) {
// shouldn't happen, but just in case...
return;
}
}
// enable the rescale shader
// update the "uniform" scale factor values (note that the Java-level
// dispatching code always passes down 4 values here, regardless of
// the original source image type)
{
}
// update the "uniform" offset values (note that the Java-level
// dispatching code always passes down 4 values here, and that the
// offsets will have already been normalized to the range [0,1])
{
}
}
void
{
// disable the RescaleOp shader
}
/**************************** LookupOp support ******************************/
/**
* The LookupOp shader takes a fragment color (from the source texture) as
* input, subtracts the optional user offset value, and then uses the
* resulting value to index into the lookup table texture to provide
* a new color result. Finally, the resulting value is multiplied by
* the current OpenGL color, which contains the extra alpha value.
*
* The lookup step requires 3 texture accesses (or 4, when alpha is included),
* which is somewhat unfortunate because it's not ideal from a performance
* standpoint, but that sort of thing is getting faster with newer hardware.
* In the 3-band case, we could consider using a three-dimensional texture
* and performing the lookup with a single texture access step. We already
* use this approach in the LCD text shader, and it works well, but for the
* purposes of this LookupOp shader, it's probably overkill. Also, there's
* a difference in that the LCD text shader only needs to populate the 3D LUT
* once, but here we would need to populate it on every invocation, which
*
* 256 elements. This means that we currently only support user-provided
* tables with no more than 256 elements in each band (this is checked at
* at the Java level). If the user provides a table with less than 256
* elements per band, our shader will still work fine, but if elements are
* accessed with an index >= the size of the LUT, then the shader will simply
* produce undefined values. Typically the user would provide an offset
* value that would prevent this from happening, but it's worth pointing out
* this fact because the software LookupOp implementation would usually
* throw an ArrayIndexOutOfBoundsException in this scenario (although it is
* not something demanded by the spec).
*
* The LookupOp spec says that the operation is performed regardless of
* whether the source data is premultiplied or non-premultiplied. This is
* a problem for the OpenGL pipeline in that a non-premultiplied
* BufferedImage will have already been converted into premultiplied
* when uploaded to an OpenGL texture. Therefore, we have a special mode
* called LOOKUP_NON_PREMULT (used only for source images that were
* originally non-premultiplied) that un-premultiplies the source color
* prior to the lookup operation, then re-premultiplies the resulting
* color before returning from the fragment shader.
*
* Note that this shader source code includes some "holes" marked by "%s".
* This allows us to build different shader programs (e.g. one for
* GL_TEXTURE_2D targets, one for GL_TEXTURE_RECTANGLE_ARB targets, and so on)
* simply by filling in these "holes" with a call to sprintf(). See the
* OGLBufImgOps_CreateLookupProgram() method for more details.
*/
static const char *lookupShaderSource =
// source image (bound to texture unit 0)
"uniform sampler%s baseImage;"
// lookup table (bound to texture unit 1)
"uniform sampler2D lookupTable;"
// offset subtracted from source index prior to lookup step
"uniform vec4 offset;"
""
"void main(void)"
"{"
" vec4 srcColor = texture%s(baseImage, gl_TexCoord[0].st);"
// (placeholder for un-premult code)
" %s"
// subtract offset from original index
" vec4 srcIndex = srcColor - offset;"
// use source value as input to lookup table (note that
// "v" texcoords are hardcoded to hit texel centers of
" vec4 result;"
" result.r = texture2D(lookupTable, vec2(srcIndex.r, 0.125)).r;"
" result.g = texture2D(lookupTable, vec2(srcIndex.g, 0.375)).r;"
" result.b = texture2D(lookupTable, vec2(srcIndex.b, 0.625)).r;"
// (placeholder for alpha store code)
" %s"
// (placeholder for re-premult code)
" %s"
// modulate with gl_Color in order to apply extra alpha
" gl_FragColor = result * gl_Color;"
"}";
/**
* Flags that can be bitwise-or'ed together to control how the shader
* source code is generated.
*/
/**
* The handles to the LookupOp fragment program objects. The index to
* the array should be a bitwise-or'ing of the LOOKUP_* flags defined
* above. Note that most applications will likely need to initialize one
* or two of these elements, so the array is usually sparsely populated.
*/
/**
* The handle to the lookup table texture object used by the shader.
*/
/**
* Compiles and links the LookupOp shader program. If successful, this
* function returns a handle to the newly created shader program; otherwise
* returns 0.
*/
static GLhandleARB
{
char *alpha;
"OGLBufImgOps_CreateLookupProgram: flags=%d",
flags);
if (IS_SET(LOOKUP_USE_SRC_ALPHA)) {
// when numComps is 1 or 3, the alpha is not looked up in the table;
// just keep the alpha from the source fragment
alpha = "result.a = srcColor.a;";
} else {
// when numComps is 4, the alpha is looked up in the table, just
// like the other color components from the source fragment
alpha =
"result.a = texture2D(lookupTable, vec2(srcIndex.a, 0.875)).r;";
}
if (IS_SET(LOOKUP_NON_PREMULT)) {
preLookup = "srcColor.rgb /= srcColor.a;";
postLookup = "result.rgb *= result.a;";
}
// compose the final source code string from the various pieces
if (lookupProgram == 0) {
"OGLBufImgOps_CreateLookupProgram: error creating program");
return 0;
}
// "use" the program object temporarily so that we can set the uniforms
// set the "uniform" values
// "unuse" the program object; it will be re-bound later as needed
return lookupProgram;
}
void
void *tableValues)
{
int i;
"OGLBufImgOps_EnableLookupOp: short=%d num=%d len=%d off=%d",
// choose the appropriate shader, depending on the source texture target
// and the number of bands involved
flags |= LOOKUP_RECT;
}
if (numBands != 4) {
}
if (nonPremult) {
}
// locate/initialize the shader program for the given flags
if (lookupPrograms[flags] == 0) {
if (lookupPrograms[flags] == 0) {
// shouldn't happen, but just in case...
return;
}
}
// enable the lookup shader
// update the "uniform" offset value
// bind the lookup table to texture unit 1 and enable texturing
if (lutTextureID == 0) {
/*
* Create the lookup table texture with 4 rows (one band per row)
* and 256 columns (one LUT band element per column) and with an
* internal format of 16-bit luminance values, which will be
* sufficient for either byte or short LUT data. Note that the
* texture wrap mode will be set to the default of GL_CLAMP_TO_EDGE,
* which means that out-of-range index value will be clamped
* appropriately.
*/
256, 4);
if (lutTextureID == 0) {
// should never happen, but just to be safe...
return;
}
}
// update the lookup table with the user-provided values
if (numBands == 1) {
// replicate the single band for R/G/B; alpha band is unused
for (i = 0; i < 3; i++) {
bands[i] = tableValues;
}
} else if (numBands == 3) {
// user supplied band for each of R/G/B; alpha band is unused
for (i = 0; i < 3; i++) {
}
} else if (numBands == 4) {
// user supplied band for each of R/G/B/A
for (i = 0; i < 4; i++) {
}
}
// upload the bands one row at a time into our lookup table texture
for (i = 0; i < 4; i++) {
continue;
}
0, i, bandLength, 1,
bands[i]);
}
// restore texture unit 0 (the default) as the active one since
// the OGLBlitTextureToSurface() method is responsible for binding the
// source image texture, which will happen later
}
void
{
// disable the LookupOp shader
// disable the lookup table on texture unit 1
}
#endif /* !HEADLESS */