[Mesa-dev] [PATCH] meta: Add GLSL variant of _mesa_meta_GenerateMipmap() function
Anuj Phogat
anuj.phogat at gmail.com
Mon Aug 27 23:14:17 PDT 2012
This reduces the overhead of using the fixed function internally
in the driver.
Note: ARB_fragment_shader is not present in intel->gen < 4. I'll send
out a separate patch to avoid enabling _mesa_meta_GenerateMipmap() in
those chips.
No regressions observed in all.tests with this patch. Need more testing
for integer textures.
Signed-off-by: Anuj Phogat <anuj.phogat at gmail.com>
---
src/mesa/drivers/common/meta.c | 344 ++++++++++++++++++++++++++++++++
src/mesa/drivers/common/meta.h | 4 +
src/mesa/drivers/dri/intel/intel_tex.c | 1 +
3 files changed, 349 insertions(+), 0 deletions(-)
diff --git a/src/mesa/drivers/common/meta.c b/src/mesa/drivers/common/meta.c
index 8c9c803..5de5b54 100644
--- a/src/mesa/drivers/common/meta.c
+++ b/src/mesa/drivers/common/meta.c
@@ -282,6 +282,8 @@ struct gen_mipmap_state
GLuint VBO;
GLuint FBO;
GLuint Sampler;
+ GLuint ShaderProg;
+ GLuint IntegerShaderProg;
};
@@ -330,6 +332,8 @@ struct gl_meta_state
static void meta_glsl_blit_cleanup(struct gl_context *ctx, struct blit_state *blit);
static void cleanup_temp_texture(struct gl_context *ctx, struct temp_texture *tex);
static void meta_glsl_clear_cleanup(struct gl_context *ctx, struct clear_state *clear);
+static void meta_glsl_generate_mipmap_cleanup(struct gl_context *ctx,
+ struct gen_mipmap_state *mipmap);
static GLuint
compile_shader_with_debug(struct gl_context *ctx, GLenum target, const GLcharARB *source)
@@ -422,6 +426,7 @@ _mesa_meta_free(struct gl_context *ctx)
_mesa_make_current(ctx, NULL, NULL);
meta_glsl_blit_cleanup(ctx, &ctx->Meta->Blit);
meta_glsl_clear_cleanup(ctx, &ctx->Meta->Clear);
+ meta_glsl_generate_mipmap_cleanup(ctx, &ctx->Meta->Mipmap);
cleanup_temp_texture(ctx, &ctx->Meta->TempTex);
if (old_context)
_mesa_make_current(old_context, old_context->WinSysDrawBuffer, old_context->WinSysReadBuffer);
@@ -3158,6 +3163,345 @@ _mesa_meta_GenerateMipmap(struct gl_context *ctx, GLenum target,
_mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboSave);
}
+static void
+meta_glsl_GenerateMipmap_init(struct gl_context *ctx,
+ struct gen_mipmap_state *mipmap)
+{
+ 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";
+ const char *fs_source =
+ "uniform sampler2D tex2d;\n"
+ "varying vec3 texCoords;\n"
+ "void main()\n"
+ "{\n"
+ " gl_FragColor = texture2D(tex2d, texCoords.xy);\n"
+ "}\n";
+
+ const char *vs_int_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 = gl_Vertex;\n"
+ "}\n";
+ const char *fs_int_source =
+ "#version 130\n"
+ "uniform isampler2D tex2d;\n"
+ "in vec3 texCoords;\n"
+ "out ivec4 out_color;\n"
+ "\n"
+ "void main()\n"
+ "{\n"
+ " out_color = texture(tex2d, texCoords.xy);\n"
+ "}\n";
+ GLuint vs, fs;
+
+ if (mipmap->ArrayObj != 0)
+ return;
+ /* create vertex array object */
+ _mesa_GenVertexArrays(1, &mipmap->ArrayObj);
+ _mesa_BindVertexArray(mipmap->ArrayObj);
+
+ /* create vertex array buffer */
+ _mesa_GenBuffersARB(1, &mipmap->VBO);
+ _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
+
+ vs = _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER);
+ _mesa_ShaderSourceARB(vs, 1, &vs_source, NULL);
+ _mesa_CompileShaderARB(vs);
+
+ fs = _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER);
+ _mesa_ShaderSourceARB(fs, 1, &fs_source, NULL);
+ _mesa_CompileShaderARB(fs);
+
+ mipmap->ShaderProg = _mesa_CreateProgramObjectARB();
+ _mesa_AttachShader(mipmap->ShaderProg, fs);
+ _mesa_DeleteObjectARB(fs);
+ _mesa_AttachShader(mipmap->ShaderProg, vs);
+ _mesa_DeleteObjectARB(vs);
+ _mesa_BindAttribLocationARB(mipmap->ShaderProg, 0, "position");
+ _mesa_BindAttribLocationARB(mipmap->ShaderProg, 1, "texcoords");
+ _mesa_EnableVertexAttribArrayARB(0);
+ _mesa_EnableVertexAttribArrayARB(1);
+ _mesa_LinkProgramARB(mipmap->ShaderProg);
+
+ if (_mesa_is_desktop_gl(ctx) && ctx->Const.GLSLVersion >= 130) {
+ vs = compile_shader_with_debug(ctx, GL_VERTEX_SHADER, vs_int_source);
+ fs = compile_shader_with_debug(ctx, GL_FRAGMENT_SHADER, fs_int_source);
+
+ mipmap->IntegerShaderProg = _mesa_CreateProgramObjectARB();
+ _mesa_AttachShader(mipmap->IntegerShaderProg, fs);
+ _mesa_DeleteObjectARB(fs);
+ _mesa_AttachShader(mipmap->IntegerShaderProg, vs);
+ _mesa_DeleteObjectARB(vs);
+ _mesa_BindAttribLocationARB(mipmap->IntegerShaderProg, 0, "position");
+ _mesa_BindAttribLocationARB(mipmap->IntegerShaderProg, 1, "texcoords");
+
+ /* Note that user-defined out attributes get automatically assigned
+ * locations starting from 0, so we don't need to explicitly
+ * BindFragDataLocation to 0.
+ */
+
+ link_program_with_debug(ctx, mipmap->IntegerShaderProg);
+ }
+}
+
+static void
+meta_glsl_generate_mipmap_cleanup(struct gl_context *ctx,
+ struct gen_mipmap_state *mipmap)
+{
+ if (mipmap->ArrayObj == 0)
+ return;
+ _mesa_DeleteVertexArraysAPPLE(1, &mipmap->ArrayObj);
+ mipmap->ArrayObj = 0;
+ _mesa_DeleteBuffersARB(1, &mipmap->VBO);
+ mipmap->VBO = 0;
+ _mesa_DeleteObjectARB(mipmap->ShaderProg);
+ mipmap->ShaderProg = 0;
+
+ if (mipmap->IntegerShaderProg) {
+ _mesa_DeleteObjectARB(mipmap->IntegerShaderProg);
+ mipmap->IntegerShaderProg = 0;
+ }
+}
+
+/**
+ * Meta implementation of ctx->Driver.GenerateMipmap() in terms of polygon
+ * rendering using glsl shader programs.
+ */
+void
+_mesa_meta_glsl_GenerateMipmap(struct gl_context *ctx, GLenum target,
+ struct gl_texture_object *texObj)
+{
+ struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap;
+ struct vertex {
+ GLfloat x, y, tex[3];
+ };
+ struct vertex verts[4];
+ const GLuint baseLevel = texObj->BaseLevel;
+ const GLuint maxLevel = texObj->MaxLevel;
+ const GLint maxLevelSave = texObj->MaxLevel;
+ const GLboolean genMipmapSave = texObj->GenerateMipmap;
+ const GLenum srgbBufferSave = ctx->Color.sRGBEnabled;
+ const GLuint fboSave = ctx->DrawBuffer->Name;
+ const GLuint original_active_unit = ctx->Texture.CurrentUnit;
+ GLenum faceTarget;
+ GLuint dstLevel;
+ const GLuint border = 0;
+ const GLint slice = 0;
+ GLuint samplerSave;
+
+ if (_mesa_meta_check_generate_mipmap_fallback(ctx, target, texObj)) {
+ _mesa_generate_mipmap(ctx, target, texObj);
+ return;
+ }
+
+ if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
+ target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) {
+ faceTarget = target;
+ target = GL_TEXTURE_CUBE_MAP;
+ }
+ else {
+ faceTarget = target;
+ }
+
+ _mesa_meta_begin(ctx, MESA_META_ALL);
+ meta_glsl_GenerateMipmap_init(ctx, mipmap);
+ /* 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));
+ if (texObj->_IsIntegerFormat)
+ _mesa_UseProgramObjectARB(mipmap->IntegerShaderProg);
+ else
+ _mesa_UseProgramObjectARB(mipmap->ShaderProg);
+
+ _mesa_BindVertexArray(mipmap->ArrayObj);
+ _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
+
+ samplerSave = ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler ?
+ ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler->Name : 0;
+
+ if (original_active_unit != 0)
+ _mesa_BindTexture(target, texObj->Name);
+
+ if (!mipmap->FBO) {
+ _mesa_GenFramebuffersEXT(1, &mipmap->FBO);
+ }
+
+ if (!mipmap->Sampler) {
+ _mesa_GenSamplers(1, &mipmap->Sampler);
+ _mesa_BindSampler(ctx->Texture.CurrentUnit, mipmap->Sampler);
+ _mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
+ _mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ _mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ _mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ _mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
+ /* We don't want to encode or decode sRGB values; treat them as linear */
+ if (ctx->Extensions.EXT_texture_sRGB_decode) {
+ _mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_SRGB_DECODE_EXT,
+ GL_SKIP_DECODE_EXT);
+ }
+
+ } else {
+ _mesa_BindSampler(ctx->Texture.CurrentUnit, mipmap->Sampler);
+ }
+
+ _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, mipmap->FBO);
+
+ if (ctx->API == API_OPENGL || ctx->API == API_OPENGLES)
+ _mesa_TexParameteri(target, GL_GENERATE_MIPMAP, GL_FALSE);
+ else
+ assert(!genMipmapSave);
+
+ if (ctx->Extensions.EXT_framebuffer_sRGB) {
+ _mesa_set_enable(ctx, GL_FRAMEBUFFER_SRGB_EXT, GL_FALSE);
+ }
+
+ _mesa_set_enable(ctx, target, GL_TRUE);
+
+ /* setup texcoords (XXX what about border?) */
+ setup_texture_coords(faceTarget,
+ slice,
+ 0, 0, /* width, height never used here */
+ verts[0].tex,
+ verts[1].tex,
+ verts[2].tex,
+ verts[3].tex);
+
+ /* setup vertex positions */
+ verts[0].x = -1.0F;
+ verts[0].y = -1.0F;
+ verts[1].x = 1.0F;
+ verts[1].y = -1.0F;
+ verts[2].x = 1.0F;
+ verts[2].y = 1.0F;
+ verts[3].x = -1.0F;
+ verts[3].y = 1.0F;
+
+ /* upload vertex data */
+ _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts), verts,
+ GL_DYNAMIC_DRAW_ARB);
+
+ /* texture is already locked, unlock now */
+ _mesa_unlock_texture(ctx, texObj);
+
+ for (dstLevel = baseLevel + 1; dstLevel <= maxLevel; dstLevel++) {
+ const struct gl_texture_image *srcImage;
+ const GLuint srcLevel = dstLevel - 1;
+ GLsizei srcWidth, srcHeight, srcDepth;
+ GLsizei dstWidth, dstHeight, dstDepth;
+ GLenum status;
+
+ srcImage = _mesa_select_tex_image(ctx, texObj, faceTarget, srcLevel);
+ assert(srcImage->Border == 0); /* XXX we can fix this */
+
+ /* src size w/out border */
+ srcWidth = srcImage->Width - 2 * border;
+ srcHeight = srcImage->Height - 2 * border;
+ srcDepth = srcImage->Depth - 2 * border;
+
+ /* new dst size w/ border */
+ dstWidth = MAX2(1, srcWidth / 2) + 2 * border;
+ dstHeight = MAX2(1, srcHeight / 2) + 2 * border;
+ dstDepth = MAX2(1, srcDepth / 2) + 2 * border;
+
+ if (dstWidth == srcImage->Width &&
+ dstHeight == srcImage->Height &&
+ dstDepth == srcImage->Depth) {
+ /* all done */
+ break;
+ }
+
+ /* Allocate storage for the destination mipmap image(s) */
+
+ /* Set MaxLevel large enough to hold the new level when we allocate it */
+ _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, dstLevel);
+
+ if (!_mesa_prepare_mipmap_level(ctx, texObj, dstLevel,
+ dstWidth, dstHeight, dstDepth,
+ srcImage->Border,
+ srcImage->InternalFormat,
+ srcImage->TexFormat)) {
+ /* All done. We either ran out of memory or we would go beyond the
+ * last valid level of an immutable texture if we continued.
+ */
+ break;
+ }
+
+ /* limit minification to src level */
+ _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, srcLevel);
+
+ /* Set to draw into the current dstLevel */
+ if (target == GL_TEXTURE_1D) {
+ _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT,
+ GL_COLOR_ATTACHMENT0_EXT,
+ target,
+ texObj->Name,
+ dstLevel);
+ }
+ else if (target == GL_TEXTURE_3D) {
+ GLint zoffset = 0; /* XXX unfinished */
+ _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT,
+ GL_COLOR_ATTACHMENT0_EXT,
+ target,
+ texObj->Name,
+ dstLevel, zoffset);
+ }
+ else {
+ /* 2D / cube */
+ _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
+ GL_COLOR_ATTACHMENT0_EXT,
+ faceTarget,
+ texObj->Name,
+ dstLevel);
+ }
+
+ _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
+ /* sanity check */
+ status = _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
+ if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
+ _mesa_problem(ctx, "Unexpected incomplete framebuffer in "
+ "_mesa_meta_GenerateMipmap()");
+ break;
+ }
+
+ assert(dstWidth == ctx->DrawBuffer->Width);
+ assert(dstHeight == ctx->DrawBuffer->Height);
+
+ /* setup viewport */
+ _mesa_set_viewport(ctx, 0, 0, dstWidth, dstHeight);
+ _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
+ }
+
+ if (ctx->Extensions.EXT_framebuffer_sRGB && srgbBufferSave) {
+ _mesa_set_enable(ctx, GL_FRAMEBUFFER_SRGB_EXT, GL_TRUE);
+ }
+
+ _mesa_lock_texture(ctx, texObj); /* relock */
+
+ _mesa_BindSampler(ctx->Texture.CurrentUnit, samplerSave);
+
+ _mesa_meta_end(ctx);
+
+ _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, maxLevelSave);
+ if (genMipmapSave)
+ _mesa_TexParameteri(target, GL_GENERATE_MIPMAP, genMipmapSave);
+
+ _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboSave);
+}
+
/**
* Determine the GL data type to use for the temporary image read with
diff --git a/src/mesa/drivers/common/meta.h b/src/mesa/drivers/common/meta.h
index d8dfb56..ea1bc3a 100644
--- a/src/mesa/drivers/common/meta.h
+++ b/src/mesa/drivers/common/meta.h
@@ -112,6 +112,10 @@ _mesa_meta_GenerateMipmap(struct gl_context *ctx, GLenum target,
struct gl_texture_object *texObj);
extern void
+_mesa_meta_glsl_GenerateMipmap(struct gl_context *ctx, GLenum target,
+ struct gl_texture_object *texObj);
+
+extern void
_mesa_meta_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
struct gl_texture_image *texImage,
GLint xoffset, GLint yoffset, GLint zoffset,
diff --git a/src/mesa/drivers/dri/intel/intel_tex.c b/src/mesa/drivers/dri/intel/intel_tex.c
index 4abe988..256fdd4 100644
--- a/src/mesa/drivers/dri/intel/intel_tex.c
+++ b/src/mesa/drivers/dri/intel/intel_tex.c
@@ -219,4 +219,5 @@ intelInitTextureFuncs(struct dd_function_table *functions)
functions->AllocTextureStorage = intel_alloc_texture_storage;
functions->MapTextureImage = intel_map_texture_image;
functions->UnmapTextureImage = intel_unmap_texture_image;
+ functions->GenerateMipmap = _mesa_meta_glsl_GenerateMipmap;
}
--
1.7.7.6
More information about the mesa-dev
mailing list