2 * Copyright 2015 VMware, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
24 #include "main/mtypes.h"
25 #include "main/buffers.h"
26 #include "main/errors.h"
27 #include "main/fbobject.h"
29 #include "main/teximage.h"
30 #include "main/texparam.h"
31 #include "st_copytex.h"
35 * Copy a colorbuffer from the window system framebuffer (a window or
36 * pbuffer) to a texture.
37 * This is a helper used by the wglBindTexImageARB() function.
39 * \param srcBuffer source buffer (GL_FRONT_LEFT, GL_BACK_LEFT, etc)
40 * \param fbWidth width of the source framebuffer
41 * \param fbHeight height of the source framebuffer
42 * \param texTarget which texture target to copy to (GL_TEXTURE_1D/2D/CUBE_MAP)
43 * \param texLevel which texture mipmap level to copy to
44 * \param cubeFace which cube face to copy to (in [0,5])
45 * \param texFormat what texture format to use, if texture doesn't exist
48 st_copy_framebuffer_to_texture(GLenum srcBuffer
,
49 GLint fbWidth
, GLint fbHeight
,
50 GLenum texTarget
, GLint texLevel
,
51 GLuint cubeFace
, GLenum texFormat
)
53 GLint readFBOSave
, readBufSave
, width
, height
;
57 /* Save current FBO / readbuffer */
58 _mesa_GetIntegerv(GL_READ_FRAMEBUFFER_BINDING
, &readFBOSave
);
59 _mesa_GetIntegerv(GL_READ_BUFFER
, &readBufSave
);
61 /* Read from the winsys buffer */
62 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER
, 0);
63 _mesa_ReadBuffer(srcBuffer
);
65 /* copy image from pbuffer to texture */
68 _mesa_GetTexLevelParameteriv(GL_TEXTURE_1D
, texLevel
,
69 GL_TEXTURE_WIDTH
, &width
);
70 if (width
== fbWidth
) {
71 /* replace existing texture */
72 _mesa_CopyTexSubImage1D(GL_TEXTURE_1D
,
78 /* define initial texture */
79 _mesa_CopyTexImage1D(GL_TEXTURE_1D
,
87 _mesa_GetTexLevelParameteriv(GL_TEXTURE_2D
, texLevel
,
88 GL_TEXTURE_WIDTH
, &width
);
89 _mesa_GetTexLevelParameteriv(GL_TEXTURE_2D
, texLevel
,
90 GL_TEXTURE_HEIGHT
, &height
);
91 if (width
== fbWidth
&& height
== fbHeight
) {
92 /* replace existing texture */
93 _mesa_CopyTexSubImage2D(GL_TEXTURE_2D
,
95 0, 0, /* xoffset, yoffset */
99 /* define initial texture */
100 _mesa_CopyTexImage2D(GL_TEXTURE_2D
,
104 fbWidth
, fbHeight
, 0);
107 case GL_TEXTURE_CUBE_MAP
:
109 const GLenum target
=
110 GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ cubeFace
;
111 _mesa_GetTexLevelParameteriv(target
, texLevel
,
112 GL_TEXTURE_WIDTH
, &width
);
113 _mesa_GetTexLevelParameteriv(target
, texLevel
,
114 GL_TEXTURE_HEIGHT
, &height
);
115 if (width
== fbWidth
&& height
== fbHeight
) {
116 /* replace existing texture */
117 _mesa_CopyTexSubImage2D(target
,
119 0, 0, /* xoffset, yoffset */
123 /* define new texture */
124 _mesa_CopyTexImage2D(target
,
128 fbWidth
, fbHeight
, 0);
134 "unexpected target in st_copy_framebuffer_to_texture()\n");
137 /* restore readbuffer */
138 _mesa_ReadBuffer(readBufSave
);
139 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER
, readFBOSave
);