#include "drivers/common/meta.h"
#include "main/enums.h"
#include "main/glformats.h"
-
+#include "../glsl/ralloc.h"
/** Return offset in bytes of the field within a vertex struct */
#define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
struct temp_texture Tex; /**< separate texture from other meta ops */
};
+/**
+ * State for GLSL texture sampler which is used to generate fragment
+ * shader in _mesa_meta_generate_mipmap().
+ */
+struct glsl_sampler {
+ const char *type;
+ const char *func;
+ const char *texcoords;
+ GLuint shader_prog;
+};
/**
* State for _mesa_meta_generate_mipmap()
GLuint Sampler;
GLuint ShaderProg;
GLuint IntegerShaderProg;
+ struct glsl_sampler sampler_1d;
+ struct glsl_sampler sampler_2d;
+ struct glsl_sampler sampler_3d;
+ struct glsl_sampler sampler_cubemap;
+ struct glsl_sampler sampler_1d_array;
+ struct glsl_sampler sampler_2d_array;
};
-
/**
* State for texture decompression
*/
static void
setup_ff_generate_mipmap(struct gl_context *ctx,
- struct gen_mipmap_state *mipmap)
+ struct gen_mipmap_state *mipmap)
{
struct vertex {
GLfloat x, y, tex[3];
}
+static struct glsl_sampler *
+setup_texture_sampler(GLenum target, struct gen_mipmap_state *mipmap)
+{
+ switch(target) {
+ case GL_TEXTURE_1D:
+ mipmap->sampler_1d.type = "sampler1D";
+ mipmap->sampler_1d.func = "texture1D";
+ mipmap->sampler_1d.texcoords = "texCoords.x";
+ return &mipmap->sampler_1d;
+ case GL_TEXTURE_2D:
+ mipmap->sampler_2d.type = "sampler2D";
+ mipmap->sampler_2d.func = "texture2D";
+ mipmap->sampler_2d.texcoords = "texCoords.xy";
+ return &mipmap->sampler_2d;
+ case GL_TEXTURE_3D:
+ /* Code for mipmap generation with 3D textures is not used yet.
+ * It's a sw fallback.
+ */
+ mipmap->sampler_3d.type = "sampler3D";
+ mipmap->sampler_3d.func = "texture3D";
+ mipmap->sampler_3d.texcoords = "texCoords";
+ return &mipmap->sampler_3d;
+ case GL_TEXTURE_CUBE_MAP:
+ mipmap->sampler_cubemap.type = "samplerCube";
+ mipmap->sampler_cubemap.func = "textureCube";
+ mipmap->sampler_cubemap.texcoords = "texCoords";
+ return &mipmap->sampler_cubemap;
+ case GL_TEXTURE_1D_ARRAY:
+ mipmap->sampler_1d_array.type = "sampler1DArray";
+ mipmap->sampler_1d_array.func = "texture1DArray";
+ mipmap->sampler_1d_array.texcoords = "texCoords.xy";
+ return &mipmap->sampler_1d_array;
+ case GL_TEXTURE_2D_ARRAY:
+ mipmap->sampler_2d_array.type = "sampler2DArray";
+ mipmap->sampler_2d_array.func = "texture2DArray";
+ mipmap->sampler_2d_array.texcoords = "texCoords";
+ return &mipmap->sampler_2d_array;
+ default:
+ _mesa_problem(NULL, "Unexpected texture target 0x%x in"
+ " setup_texture_sampler()\n", target);
+ return NULL;
+ }
+}
+
+
static void
setup_glsl_generate_mipmap(struct gl_context *ctx,
- struct gen_mipmap_state *mipmap)
+ struct gen_mipmap_state *mipmap,
+ GLenum target)
{
struct vertex {
GLfloat x, y, tex[3];
};
+ struct glsl_sampler *sampler;
+ const char *vs_source;
- static const char *vs_source =
- "attribute vec2 position;\n"
- "attribute vec3 textureCoords;\n"
- "varying vec3 texCoords;\n"
- "void main()\n"
- "{\n"
- " texCoords = textureCoords;\n"
- " gl_Position = vec4(position, 0.0, 1.0);\n"
- "}\n";
- static const char *fs_source =
- "uniform sampler2D tex2d;\n"
- "varying vec3 texCoords;\n"
- "void main()\n"
- "{\n"
- " gl_FragColor = texture2D(tex2d, texCoords.xy);\n"
- "}\n";
-
static const char *vs_int_source =
"#version 130\n"
"in vec2 position;\n"
"{\n"
" out_color = texture(tex2d, texCoords.xy);\n"
"}\n";
+ char *fs_source;
GLuint vs, fs;
+ void *mem_ctx;
/* Check if already initialized */
- if (mipmap->ArrayObj != 0)
- return;
- /* create vertex array object */
- _mesa_GenVertexArrays(1, &mipmap->ArrayObj);
- _mesa_BindVertexArray(mipmap->ArrayObj);
+ if (mipmap->ArrayObj == 0) {
- /* create vertex array buffer */
- _mesa_GenBuffersARB(1, &mipmap->VBO);
- _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
+ /* create vertex array object */
+ _mesa_GenVertexArrays(1, &mipmap->ArrayObj);
+ _mesa_BindVertexArray(mipmap->ArrayObj);
- /* setup vertex arrays */
- _mesa_VertexAttribPointerARB(0, 2, GL_FLOAT, GL_FALSE,
- sizeof(struct vertex), OFFSET(x));
- _mesa_VertexAttribPointerARB(1, 3, GL_FLOAT, GL_FALSE,
- sizeof(struct vertex), OFFSET(tex));
+ /* create vertex array buffer */
+ _mesa_GenBuffersARB(1, &mipmap->VBO);
+ _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
+
+ /* setup vertex arrays */
+ _mesa_VertexAttribPointerARB(0, 2, GL_FLOAT, GL_FALSE,
+ sizeof(struct vertex), OFFSET(x));
+ _mesa_VertexAttribPointerARB(1, 3, GL_FLOAT, GL_FALSE,
+ sizeof(struct vertex), OFFSET(tex));
+ }
+
+ /* Generate a fragment shader program appropriate for the texture target */
+ sampler = setup_texture_sampler(target, mipmap);
+ assert(sampler != NULL);
+ if (sampler->shader_prog != 0) {
+ mipmap->ShaderProg = sampler->shader_prog;
+ return;
+ }
+
+ mem_ctx = ralloc_context(NULL);
+
+ if (ctx->API == API_OPENGLES2 || ctx->Const.GLSLVersion < 130) {
+ const char *fs_template;
+ const char *extension_mode;
+
+ vs_source =
+ "attribute vec2 position;\n"
+ "attribute vec3 textureCoords;\n"
+ "varying vec3 texCoords;\n"
+ "void main()\n"
+ "{\n"
+ " texCoords = textureCoords;\n"
+ " gl_Position = vec4(position, 0.0, 1.0);\n"
+ "}\n";
+ fs_template =
+ "#extension GL_EXT_texture_array : %s\n"
+ "uniform %s texSampler;\n"
+ "varying vec3 texCoords;\n"
+ "void main()\n"
+ "{\n"
+ " gl_FragColor = %s(texSampler, %s);\n"
+ "}\n";
+
+ extension_mode = ((target == GL_TEXTURE_1D_ARRAY) ||
+ (target == GL_TEXTURE_2D_ARRAY)) ?
+ "require" : "disable";
+
+ fs_source = ralloc_asprintf(mem_ctx, fs_template,
+ extension_mode, sampler->type,
+ sampler->func, sampler->texcoords);
+ }
+ else {
+ const char *fs_template;
+
+ vs_source =
+ "#version 130\n"
+ "in vec2 position;\n"
+ "in vec3 textureCoords;\n"
+ "out vec3 texCoords;\n"
+ "void main()\n"
+ "{\n"
+ " texCoords = textureCoords;\n"
+ " gl_Position = vec4(position, 0.0, 1.0);\n"
+ "}\n";
+ fs_template =
+ "#version 130\n"
+ "uniform %s texSampler;\n"
+ "in vec3 texCoords;\n"
+ "out %s out_color;\n"
+ "\n"
+ "void main()\n"
+ "{\n"
+ " out_color = texture(texSampler, %s);\n"
+ "}\n";
+
+ fs_source = ralloc_asprintf(mem_ctx, fs_template,
+ sampler->type, "vec4",
+ sampler->texcoords);
+ }
vs = compile_shader_with_debug(ctx, GL_VERTEX_SHADER, vs_source);
fs = compile_shader_with_debug(ctx, GL_FRAGMENT_SHADER, fs_source);
_mesa_EnableVertexAttribArrayARB(0);
_mesa_EnableVertexAttribArrayARB(1);
link_program_with_debug(ctx, mipmap->ShaderProg);
+ sampler->shader_prog = mipmap->ShaderProg;
+ ralloc_free(mem_ctx);
if ((_mesa_is_desktop_gl(ctx) && ctx->Const.GLSLVersion >= 130) ||
_mesa_is_gles3(ctx)){
mipmap->ArrayObj = 0;
_mesa_DeleteBuffersARB(1, &mipmap->VBO);
mipmap->VBO = 0;
- _mesa_DeleteObjectARB(mipmap->ShaderProg);
- mipmap->ShaderProg = 0;
+
+ _mesa_DeleteObjectARB(mipmap->sampler_1d.shader_prog);
+ _mesa_DeleteObjectARB(mipmap->sampler_2d.shader_prog);
+ _mesa_DeleteObjectARB(mipmap->sampler_3d.shader_prog);
+ _mesa_DeleteObjectARB(mipmap->sampler_cubemap.shader_prog);
+ _mesa_DeleteObjectARB(mipmap->sampler_1d_array.shader_prog);
+ _mesa_DeleteObjectARB(mipmap->sampler_2d_array.shader_prog);
+
+ mipmap->sampler_1d.shader_prog = 0;
+ mipmap->sampler_2d.shader_prog = 0;
+ mipmap->sampler_3d.shader_prog = 0;
+ mipmap->sampler_cubemap.shader_prog = 0;
+ mipmap->sampler_1d_array.shader_prog = 0;
+ mipmap->sampler_2d_array.shader_prog = 0;
if (mipmap->IntegerShaderProg) {
_mesa_DeleteObjectARB(mipmap->IntegerShaderProg);
* GenerateMipmap function.
*/
if (use_glsl_version) {
- setup_glsl_generate_mipmap(ctx, mipmap);
+ setup_glsl_generate_mipmap(ctx, mipmap, target);
if (texObj->_IsIntegerFormat)
_mesa_UseProgramObjectARB(mipmap->IntegerShaderProg);