2 #include "main/context.h"
3 #include "main/colormac.h"
4 #include "main/fbobject.h"
5 #include "main/macros.h"
6 #include "main/teximage.h"
7 #include "main/renderbuffer.h"
8 #include "swrast/swrast.h"
9 #include "swrast/s_context.h"
10 #include "swrast/s_texfetch.h"
14 * Render-to-texture code for GL_EXT_framebuffer_object
19 delete_texture_wrapper(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
21 ASSERT(rb
->RefCount
== 0);
27 * This function creates a renderbuffer object which wraps a texture image.
28 * The new renderbuffer is plugged into the given attachment point.
29 * This allows rendering into the texture as if it were a renderbuffer.
32 wrap_texture(struct gl_context
*ctx
, struct gl_renderbuffer_attachment
*att
)
34 struct gl_renderbuffer
*rb
;
35 const GLuint name
= 0;
37 ASSERT(att
->Type
== GL_TEXTURE
);
38 ASSERT(att
->Renderbuffer
== NULL
);
40 rb
= ctx
->Driver
.NewRenderbuffer(ctx
, name
);
42 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "wrap_texture");
46 /* init base gl_renderbuffer fields */
47 _mesa_init_renderbuffer(rb
, name
);
48 /* plug in our texture_renderbuffer-specific functions */
49 rb
->Delete
= delete_texture_wrapper
;
50 rb
->AllocStorage
= NULL
; /* illegal! */
52 /* update attachment point */
53 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
57 * Update the renderbuffer wrapper for rendering to a texture.
58 * For example, update the width, height of the RB based on the texture size,
59 * update the internal format info, etc.
62 update_wrapper(struct gl_context
*ctx
, struct gl_renderbuffer_attachment
*att
)
64 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
65 struct swrast_renderbuffer
*srb
= swrast_renderbuffer(rb
);
66 struct swrast_texture_image
*swImage
;
72 swImage
= swrast_texture_image(_mesa_get_attachment_teximage(att
));
75 format
= swImage
->Base
.TexFormat
;
77 if (att
->Texture
->Target
== GL_TEXTURE_1D_ARRAY_EXT
) {
81 zOffset
= att
->Zoffset
;
84 rb
->Width
= swImage
->Base
.Width
;
85 rb
->Height
= swImage
->Base
.Height
;
86 rb
->InternalFormat
= swImage
->Base
.InternalFormat
;
87 rb
->_BaseFormat
= _mesa_get_format_base_format(format
);
89 /* Want to store linear values, not sRGB */
90 rb
->Format
= _mesa_get_srgb_format_linear(format
);
92 srb
->Buffer
= swImage
->ImageSlices
[zOffset
];
98 * Called when rendering to a texture image begins, or when changing
99 * the dest mipmap level, cube face, etc.
100 * This is a fallback routine for software render-to-texture.
102 * Called via the glRenderbufferTexture1D/2D/3D() functions
103 * and elsewhere (such as glTexImage2D).
105 * The image we're rendering into is
106 * att->Texture->Image[att->CubeMapFace][att->TextureLevel];
107 * It'll never be NULL.
109 * \param fb the framebuffer object the texture is being bound to
110 * \param att the fb attachment point of the texture
112 * \sa _mesa_framebuffer_renderbuffer
115 _swrast_render_texture(struct gl_context
*ctx
,
116 struct gl_framebuffer
*fb
,
117 struct gl_renderbuffer_attachment
*att
)
121 if (!att
->Renderbuffer
) {
122 wrap_texture(ctx
, att
);
124 update_wrapper(ctx
, att
);
129 _swrast_finish_render_texture(struct gl_context
*ctx
,
130 struct gl_renderbuffer_attachment
*att
)
133 /* The renderbuffer texture wrapper will get deleted by the
134 * normal mechanism for deleting renderbuffers.