13464ebd5Sriastradh
23464ebd5Sriastradh#include "main/context.h"
33464ebd5Sriastradh#include "main/fbobject.h"
43464ebd5Sriastradh#include "main/macros.h"
53464ebd5Sriastradh#include "main/teximage.h"
63464ebd5Sriastradh#include "main/renderbuffer.h"
73464ebd5Sriastradh#include "swrast/swrast.h"
8af69d88dSmrg#include "swrast/s_context.h"
9af69d88dSmrg#include "swrast/s_texfetch.h"
103464ebd5Sriastradh
113464ebd5Sriastradh
123464ebd5Sriastradh/*
133464ebd5Sriastradh * Render-to-texture code for GL_EXT_framebuffer_object
143464ebd5Sriastradh */
153464ebd5Sriastradh
163464ebd5Sriastradh
173464ebd5Sriastradhstatic void
18af69d88dSmrgdelete_texture_wrapper(struct gl_context *ctx, struct gl_renderbuffer *rb)
193464ebd5Sriastradh{
2001e04c3fSmrg   assert(rb->RefCount == 0);
213464ebd5Sriastradh   free(rb);
223464ebd5Sriastradh}
233464ebd5Sriastradh
243464ebd5Sriastradh/**
253464ebd5Sriastradh * Update the renderbuffer wrapper for rendering to a texture.
263464ebd5Sriastradh * For example, update the width, height of the RB based on the texture size,
273464ebd5Sriastradh * update the internal format info, etc.
283464ebd5Sriastradh */
293464ebd5Sriastradhstatic void
303464ebd5Sriastradhupdate_wrapper(struct gl_context *ctx, struct gl_renderbuffer_attachment *att)
313464ebd5Sriastradh{
32af69d88dSmrg   struct gl_renderbuffer *rb = att->Renderbuffer;
33af69d88dSmrg   struct swrast_renderbuffer *srb = swrast_renderbuffer(rb);
34af69d88dSmrg   struct swrast_texture_image *swImage;
35af69d88dSmrg   mesa_format format;
36af69d88dSmrg   GLuint zOffset;
373464ebd5Sriastradh
383464ebd5Sriastradh   (void) ctx;
393464ebd5Sriastradh
40af69d88dSmrg   swImage = swrast_texture_image(rb->TexImage);
41af69d88dSmrg   assert(swImage);
423464ebd5Sriastradh
43af69d88dSmrg   format = swImage->Base.TexFormat;
443464ebd5Sriastradh
453464ebd5Sriastradh   if (att->Texture->Target == GL_TEXTURE_1D_ARRAY_EXT) {
46af69d88dSmrg      zOffset = 0;
473464ebd5Sriastradh   }
483464ebd5Sriastradh   else {
49af69d88dSmrg      zOffset = att->Zoffset;
503464ebd5Sriastradh   }
513464ebd5Sriastradh
52af69d88dSmrg   /* Want to store linear values, not sRGB */
53af69d88dSmrg   rb->Format = _mesa_get_srgb_format_linear(format);
543464ebd5Sriastradh
55af69d88dSmrg   srb->Buffer = swImage->ImageSlices[zOffset];
563464ebd5Sriastradh}
573464ebd5Sriastradh
583464ebd5Sriastradh
593464ebd5Sriastradh
603464ebd5Sriastradh/**
613464ebd5Sriastradh * Called when rendering to a texture image begins, or when changing
623464ebd5Sriastradh * the dest mipmap level, cube face, etc.
633464ebd5Sriastradh * This is a fallback routine for software render-to-texture.
643464ebd5Sriastradh *
653464ebd5Sriastradh * Called via the glRenderbufferTexture1D/2D/3D() functions
663464ebd5Sriastradh * and elsewhere (such as glTexImage2D).
673464ebd5Sriastradh *
683464ebd5Sriastradh * The image we're rendering into is
693464ebd5Sriastradh * att->Texture->Image[att->CubeMapFace][att->TextureLevel];
703464ebd5Sriastradh * It'll never be NULL.
713464ebd5Sriastradh *
723464ebd5Sriastradh * \param fb  the framebuffer object the texture is being bound to
733464ebd5Sriastradh * \param att  the fb attachment point of the texture
743464ebd5Sriastradh *
7501e04c3fSmrg * \sa _mesa_FramebufferRenderbuffer_sw
763464ebd5Sriastradh */
773464ebd5Sriastradhvoid
783464ebd5Sriastradh_swrast_render_texture(struct gl_context *ctx,
793464ebd5Sriastradh                       struct gl_framebuffer *fb,
803464ebd5Sriastradh                       struct gl_renderbuffer_attachment *att)
813464ebd5Sriastradh{
82af69d88dSmrg   struct gl_renderbuffer *rb = att->Renderbuffer;
833464ebd5Sriastradh   (void) fb;
843464ebd5Sriastradh
85af69d88dSmrg   /* plug in our texture_renderbuffer-specific functions */
86af69d88dSmrg   rb->Delete = delete_texture_wrapper;
87af69d88dSmrg
883464ebd5Sriastradh   update_wrapper(ctx, att);
893464ebd5Sriastradh}
903464ebd5Sriastradh
913464ebd5Sriastradh
923464ebd5Sriastradhvoid
933464ebd5Sriastradh_swrast_finish_render_texture(struct gl_context *ctx,
94af69d88dSmrg                              struct gl_renderbuffer *rb)
953464ebd5Sriastradh{
963464ebd5Sriastradh   /* do nothing */
973464ebd5Sriastradh   /* The renderbuffer texture wrapper will get deleted by the
983464ebd5Sriastradh    * normal mechanism for deleting renderbuffers.
993464ebd5Sriastradh    */
1003464ebd5Sriastradh   (void) ctx;
101af69d88dSmrg   (void) rb;
1023464ebd5Sriastradh}
103