static void delete_samplers(struct gl_context *ctx, GLsizei count, const GLuint *samplers) { FLUSH_VERTICES(ctx, 0); _mesa_HashLockMutex(ctx->Shared->SamplerObjects); for (GLsizei i = 0; i < count; i++) { if (samplers[i]) { GLuint j; struct gl_sampler_object *sampObj = lookup_samplerobj_locked(ctx, samplers[i]); if (sampObj) { /* If the sampler is currently bound, unbind it. */ for (j = 0; j < ctx->Const.MaxCombinedTextureImageUnits; j++) { if (ctx->Texture.Unit[j].Sampler == sampObj) { FLUSH_VERTICES(ctx, _NEW_TEXTURE_OBJECT); _mesa_reference_sampler_object(ctx, &ctx->Texture.Unit[j].Sampler, NULL); } } /* The ID is immediately freed for re-use */ _mesa_HashRemoveLocked(ctx->Shared->SamplerObjects, samplers[i]); /* But the object exists until its reference count goes to zero */ _mesa_reference_sampler_object(ctx, &sampObj, NULL); } } } _mesa_HashUnlockMutex(ctx->Shared->SamplerObjects); }
/** * Free dynamically-allocted texture data attached to the given context. */ void _mesa_free_texture_data(struct gl_context *ctx) { GLuint u, tgt; /* unreference current textures */ for (u = 0; u < ARRAY_SIZE(ctx->Texture.Unit); u++) { /* The _Current texture could account for another reference */ _mesa_reference_texobj(&ctx->Texture.Unit[u]._Current, NULL); for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) { _mesa_reference_texobj(&ctx->Texture.Unit[u].CurrentTex[tgt], NULL); } } /* Free proxy texture objects */ for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) ctx->Driver.DeleteTexture(ctx, ctx->Texture.ProxyTex[tgt]); /* GL_ARB_texture_buffer_object */ _mesa_reference_buffer_object(ctx, &ctx->Texture.BufferObject, NULL); for (u = 0; u < ARRAY_SIZE(ctx->Texture.Unit); u++) { _mesa_reference_sampler_object(ctx, &ctx->Texture.Unit[u].Sampler, NULL); } }
void GLAPIENTRY _mesa_BindSampler(GLuint unit, GLuint sampler) { struct gl_sampler_object *sampObj; GET_CURRENT_CONTEXT(ctx); if (unit >= ctx->Const.MaxCombinedTextureImageUnits) { _mesa_error(ctx, GL_INVALID_VALUE, "glBindSampler(unit %u)", unit); return; } if (sampler == 0) { /* Use the default sampler object, the one contained in the texture * object. */ sampObj = NULL; } else { /* user-defined sampler object */ sampObj = _mesa_lookup_samplerobj(ctx, sampler); if (!sampObj) { _mesa_error(ctx, GL_INVALID_OPERATION, "glBindSampler(sampler)"); return; } } if (ctx->Texture.Unit[unit].Sampler != sampObj) { FLUSH_VERTICES(ctx, _NEW_TEXTURE); } /* bind new sampler */ _mesa_reference_sampler_object(ctx, &ctx->Texture.Unit[unit].Sampler, sampObj); }
void _mesa_bind_sampler(struct gl_context *ctx, GLuint unit, struct gl_sampler_object *sampObj) { if (ctx->Texture.Unit[unit].Sampler != sampObj) { FLUSH_VERTICES(ctx, _NEW_TEXTURE_OBJECT); } _mesa_reference_sampler_object(ctx, &ctx->Texture.Unit[unit].Sampler, sampObj); }
void GLAPIENTRY _mesa_DeleteSamplers(GLsizei count, const GLuint *samplers) { GET_CURRENT_CONTEXT(ctx); GLsizei i; FLUSH_VERTICES(ctx, 0); if (count < 0) { _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteSamplers(count)"); return; } mtx_lock(&ctx->Shared->Mutex); for (i = 0; i < count; i++) { if (samplers[i]) { GLuint j; struct gl_sampler_object *sampObj = _mesa_lookup_samplerobj(ctx, samplers[i]); if (sampObj) { /* If the sampler is currently bound, unbind it. */ for (j = 0; j < ctx->Const.MaxCombinedTextureImageUnits; j++) { if (ctx->Texture.Unit[j].Sampler == sampObj) { FLUSH_VERTICES(ctx, _NEW_TEXTURE); _mesa_reference_sampler_object(ctx, &ctx->Texture.Unit[j].Sampler, NULL); } } /* The ID is immediately freed for re-use */ _mesa_HashRemove(ctx->Shared->SamplerObjects, samplers[i]); /* But the object exists until its reference count goes to zero */ _mesa_reference_sampler_object(ctx, &sampObj, NULL); } } } mtx_unlock(&ctx->Shared->Mutex); }
void _mesa_meta_glsl_generate_mipmap_cleanup(struct gl_context *ctx, struct gen_mipmap_state *mipmap) { if (mipmap->VAO == 0) return; _mesa_DeleteVertexArrays(1, &mipmap->VAO); mipmap->VAO = 0; _mesa_reference_buffer_object(ctx, &mipmap->buf_obj, NULL); _mesa_reference_sampler_object(ctx, &mipmap->samp_obj, NULL); _mesa_reference_framebuffer(&mipmap->fb, NULL); _mesa_meta_blit_shader_table_cleanup(ctx, &mipmap->shaders); }
void _mesa_meta_fb_tex_blit_end(struct gl_context *ctx, GLenum target, struct fb_tex_blit_state *blit) { struct gl_texture_object *const texObj = _mesa_get_current_tex_object(ctx, target); /* Restore texture object state, the texture binding will * be restored by _mesa_meta_end(). */ if (target != GL_TEXTURE_RECTANGLE_ARB) { _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_BASE_LEVEL, &blit->baseLevelSave, false); _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_MAX_LEVEL, &blit->maxLevelSave, false); } /* If ARB_stencil_texturing is not supported, the mode won't have changed. */ if (texObj->StencilSampling != blit->stencilSamplingSave) { /* GLint so the compiler won't complain about type signedness mismatch * in the call to _mesa_texture_parameteriv below. */ const GLint param = blit->stencilSamplingSave ? GL_STENCIL_INDEX : GL_DEPTH_COMPONENT; _mesa_texture_parameteriv(ctx, texObj, GL_DEPTH_STENCIL_TEXTURE_MODE, ¶m, false); } _mesa_bind_sampler(ctx, ctx->Texture.CurrentUnit, blit->samp_obj_save); _mesa_reference_sampler_object(ctx, &blit->samp_obj_save, NULL); _mesa_reference_sampler_object(ctx, &blit->samp_obj, NULL); if (blit->tempTex) _mesa_DeleteTextures(1, &blit->tempTex); }
void _mesa_meta_fb_tex_blit_begin(struct gl_context *ctx, struct fb_tex_blit_state *blit) { /* None of the existing callers preinitialize fb_tex_blit_state to zeros, * and both use stack variables. If samp_obj_save is not NULL, * _mesa_reference_sampler_object will try to dereference it. Leaving * random garbage in samp_obj_save can only lead to crashes. * * Since the state isn't persistent across calls, we won't catch ref * counting problems. */ blit->samp_obj_save = NULL; _mesa_reference_sampler_object(ctx, &blit->samp_obj_save, ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler); blit->tempTex = 0; }
static ALWAYS_INLINE void bind_samplers(struct gl_context *ctx, GLuint first, GLsizei count, const GLuint *samplers, bool no_error) { GLsizei i; FLUSH_VERTICES(ctx, 0); if (samplers) { /* Note that the error semantics for multi-bind commands differ from * those of other GL commands. * * The Issues section in the ARB_multi_bind spec says: * * "(11) Typically, OpenGL specifies that if an error is generated by * a command, that command has no effect. This is somewhat * unfortunate for multi-bind commands, because it would require * a first pass to scan the entire list of bound objects for * errors and then a second pass to actually perform the * bindings. Should we have different error semantics? * * RESOLVED: Yes. In this specification, when the parameters for * one of the <count> binding points are invalid, that binding * point is not updated and an error will be generated. However, * other binding points in the same command will be updated if * their parameters are valid and no other error occurs." */ _mesa_HashLockMutex(ctx->Shared->SamplerObjects); for (i = 0; i < count; i++) { const GLuint unit = first + i; struct gl_sampler_object * const currentSampler = ctx->Texture.Unit[unit].Sampler; struct gl_sampler_object *sampObj; if (samplers[i] != 0) { if (currentSampler && currentSampler->Name == samplers[i]) sampObj = currentSampler; else sampObj = lookup_samplerobj_locked(ctx, samplers[i]); /* The ARB_multi_bind spec says: * * "An INVALID_OPERATION error is generated if any value * in <samplers> is not zero or the name of an existing * sampler object (per binding)." */ if (!no_error && !sampObj) { _mesa_error(ctx, GL_INVALID_OPERATION, "glBindSamplers(samplers[%d]=%u is not zero or " "the name of an existing sampler object)", i, samplers[i]); continue; } } else { sampObj = NULL; } /* Bind the new sampler */ if (sampObj != currentSampler) { _mesa_reference_sampler_object(ctx, &ctx->Texture.Unit[unit].Sampler, sampObj); ctx->NewState |= _NEW_TEXTURE_OBJECT; } } _mesa_HashUnlockMutex(ctx->Shared->SamplerObjects); } else { /* Unbind all samplers in the range <first> through <first>+<count>-1 */ for (i = 0; i < count; i++) { const GLuint unit = first + i; if (ctx->Texture.Unit[unit].Sampler) { _mesa_reference_sampler_object(ctx, &ctx->Texture.Unit[unit].Sampler, NULL); ctx->NewState |= _NEW_TEXTURE_OBJECT; } } } }
/** * Called via ctx->Driver.GenerateMipmap() * Note: We don't yet support 3D textures, or texture borders. */ void _mesa_meta_GenerateMipmap(struct gl_context *ctx, GLenum target, struct gl_texture_object *texObj) { struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap; 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 GLboolean use_glsl_version = ctx->Extensions.ARB_vertex_shader && ctx->Extensions.ARB_fragment_shader; GLenum faceTarget; GLuint dstLevel; struct gl_sampler_object *samp_obj_save = NULL; GLint swizzle[4]; GLboolean swizzleSaved = GL_FALSE; /* GLint so the compiler won't complain about type signedness mismatch in * the calls to _mesa_texture_parameteriv below. */ static const GLint always_false = GL_FALSE; static const GLint always_true = GL_TRUE; if (fallback_required(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 & ~MESA_META_DRAW_BUFFERS); _mesa_ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); /* Choose between glsl version and fixed function version of * GenerateMipmap function. */ if (use_glsl_version) { _mesa_meta_setup_vertex_objects(ctx, &mipmap->VAO, &mipmap->buf_obj, true, 2, 4, 0); _mesa_meta_setup_blit_shader(ctx, target, false, &mipmap->shaders); } else { _mesa_meta_setup_ff_tnl_for_blit(ctx, &mipmap->VAO, &mipmap->buf_obj, 3); _mesa_set_enable(ctx, target, GL_TRUE); } _mesa_reference_sampler_object(ctx, &samp_obj_save, ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler); /* We may have been called from glGenerateTextureMipmap with CurrentUnit * still set to 0, so we don't know when we can skip binding the texture. * Assume that _mesa_BindTexture will be fast if we're rebinding the same * texture. */ _mesa_BindTexture(target, texObj->Name); if (mipmap->samp_obj == NULL) { mipmap->samp_obj = ctx->Driver.NewSamplerObject(ctx, 0xDEADBEEF); if (mipmap->samp_obj == NULL) { /* This is a bit lazy. Flag out of memory, and then don't bother to * clean up. Once out of memory is flagged, the only realistic next * move is to destroy the context. That will trigger all the right * clean up. */ _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenerateMipmap"); return; } _mesa_set_sampler_filters(ctx, mipmap->samp_obj, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR); _mesa_set_sampler_wrap(ctx, mipmap->samp_obj, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); /* We don't want to encode or decode sRGB values; treat them as linear. */ _mesa_set_sampler_srgb_decode(ctx, mipmap->samp_obj, GL_SKIP_DECODE_EXT); } _mesa_bind_sampler(ctx, ctx->Texture.CurrentUnit, mipmap->samp_obj); assert(mipmap->fb != NULL); _mesa_bind_framebuffers(ctx, mipmap->fb, mipmap->fb); _mesa_texture_parameteriv(ctx, texObj, GL_GENERATE_MIPMAP, &always_false, false); if (texObj->_Swizzle != SWIZZLE_NOOP) { static const GLint swizzleNoop[4] = { GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA }; memcpy(swizzle, texObj->Swizzle, sizeof(swizzle)); swizzleSaved = GL_TRUE; _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_SWIZZLE_RGBA, swizzleNoop, false); } /* Silence valgrind warnings about reading uninitialized stack. */ memset(verts, 0, sizeof(verts)); /* 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; /* texture is already locked, unlock now */ _mesa_unlock_texture(ctx, texObj); _mesa_prepare_mipmap_levels(ctx, texObj, baseLevel, maxLevel); for (dstLevel = baseLevel + 1; dstLevel <= maxLevel; dstLevel++) { const struct gl_texture_image *srcImage; struct gl_texture_image *dstImage; const GLuint srcLevel = dstLevel - 1; GLuint layer; GLsizei srcWidth, srcHeight, srcDepth; GLsizei dstWidth, dstHeight, dstDepth; srcImage = _mesa_select_tex_image(texObj, faceTarget, srcLevel); assert(srcImage->Border == 0); /* src size */ srcWidth = srcImage->Width; if (target == GL_TEXTURE_1D_ARRAY) { srcHeight = 1; srcDepth = srcImage->Height; } else { srcHeight = srcImage->Height; srcDepth = srcImage->Depth; } /* new dst size */ dstWidth = minify(srcWidth, 1); dstHeight = minify(srcHeight, 1); dstDepth = target == GL_TEXTURE_3D ? minify(srcDepth, 1) : srcDepth; if (dstWidth == srcWidth && dstHeight == srcHeight && dstDepth == srcDepth) { /* 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_texture_parameteriv(ctx, texObj, GL_TEXTURE_MAX_LEVEL, (GLint *) &dstLevel, false); dstImage = _mesa_select_tex_image(texObj, faceTarget, dstLevel); /* All done. We either ran out of memory or we would go beyond the last * valid level of an immutable texture if we continued. */ if (dstImage == NULL) break; /* limit minification to src level */ _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_MAX_LEVEL, (GLint *) &srcLevel, false); /* setup viewport */ _mesa_set_viewport(ctx, 0, 0, 0, dstWidth, dstHeight); _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0); for (layer = 0; layer < dstDepth; ++layer) { /* Setup texture coordinates */ _mesa_meta_setup_texture_coords(faceTarget, layer, 0, 0, /* xoffset, yoffset */ srcWidth, srcHeight, /* img size */ srcWidth, srcHeight, srcDepth, verts[0].tex, verts[1].tex, verts[2].tex, verts[3].tex); /* upload vertex data */ _mesa_buffer_data(ctx, mipmap->buf_obj, GL_NONE, sizeof(verts), verts, GL_DYNAMIC_DRAW, __func__); _mesa_meta_framebuffer_texture_image(ctx, ctx->DrawBuffer, GL_COLOR_ATTACHMENT0, dstImage, layer); /* sanity check */ if (_mesa_check_framebuffer_status(ctx, ctx->DrawBuffer) != GL_FRAMEBUFFER_COMPLETE) { _mesa_problem(ctx, "Unexpected incomplete framebuffer in " "_mesa_meta_GenerateMipmap()"); break; } assert(dstWidth == ctx->DrawBuffer->Width); if (target == GL_TEXTURE_1D_ARRAY) { assert(dstHeight == 1); } else { assert(dstHeight == ctx->DrawBuffer->Height); } _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4); } } _mesa_lock_texture(ctx, texObj); /* relock */ _mesa_bind_sampler(ctx, ctx->Texture.CurrentUnit, samp_obj_save); _mesa_reference_sampler_object(ctx, &samp_obj_save, NULL); _mesa_meta_end(ctx); _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_MAX_LEVEL, &maxLevelSave, false); if (genMipmapSave) _mesa_texture_parameteriv(ctx, texObj, GL_GENERATE_MIPMAP, &always_true, false); if (swizzleSaved) _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_SWIZZLE_RGBA, swizzle, false); }
void GLAPIENTRY _mesa_BindSamplers(GLuint first, GLsizei count, const GLuint *samplers) { GET_CURRENT_CONTEXT(ctx); GLint i; /* The ARB_multi_bind spec says: * * "An INVALID_OPERATION error is generated if <first> + <count> is * greater than the number of texture image units supported by * the implementation." */ if (first + count > ctx->Const.MaxCombinedTextureImageUnits) { _mesa_error(ctx, GL_INVALID_OPERATION, "glBindSamplers(first=%u + count=%d > the value of " "GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS=%u)", first, count, ctx->Const.MaxCombinedTextureImageUnits); return; } FLUSH_VERTICES(ctx, 0); if (samplers) { /* Note that the error semantics for multi-bind commands differ from * those of other GL commands. * * The Issues section in the ARB_multi_bind spec says: * * "(11) Typically, OpenGL specifies that if an error is generated by * a command, that command has no effect. This is somewhat * unfortunate for multi-bind commands, because it would require * a first pass to scan the entire list of bound objects for * errors and then a second pass to actually perform the * bindings. Should we have different error semantics? * * RESOLVED: Yes. In this specification, when the parameters for * one of the <count> binding points are invalid, that binding * point is not updated and an error will be generated. However, * other binding points in the same command will be updated if * their parameters are valid and no other error occurs." */ begin_samplerobj_lookups(ctx); for (i = 0; i < count; i++) { const GLuint unit = first + i; struct gl_sampler_object * const currentSampler = ctx->Texture.Unit[unit].Sampler; struct gl_sampler_object *sampObj; if (samplers[i] != 0) { if (currentSampler && currentSampler->Name == samplers[i]) sampObj = currentSampler; else sampObj = lookup_samplerobj_locked(ctx, samplers[i]); /* The ARB_multi_bind spec says: * * "An INVALID_OPERATION error is generated if any value * in <samplers> is not zero or the name of an existing * sampler object (per binding)." */ if (!sampObj) { _mesa_error(ctx, GL_INVALID_OPERATION, "glBindSamplers(samplers[%d]=%u is not zero or " "the name of an existing sampler object)", i, samplers[i]); continue; } } else { sampObj = NULL; } /* Bind the new sampler */ if (sampObj != currentSampler) { _mesa_reference_sampler_object(ctx, &ctx->Texture.Unit[unit].Sampler, sampObj); ctx->NewState |= _NEW_TEXTURE; } } end_samplerobj_lookups(ctx); } else { /* Unbind all samplers in the range <first> through <first>+<count>-1 */ for (i = 0; i < count; i++) { const GLuint unit = first + i; if (ctx->Texture.Unit[unit].Sampler) { _mesa_reference_sampler_object(ctx, &ctx->Texture.Unit[unit].Sampler, NULL); ctx->NewState |= _NEW_TEXTURE; } } } }