1b8e80941Smrg/*
2b8e80941Smrg * Copyright 2015 VMware, Inc.
3b8e80941Smrg * All Rights Reserved.
4b8e80941Smrg *
5b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
6b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
7b8e80941Smrg * to deal in the Software without restriction, including without limitation
8b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
10b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
11b8e80941Smrg *
12b8e80941Smrg * The above copyright notice and this permission notice shall be included
13b8e80941Smrg * in all copies or substantial portions of the Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16b8e80941Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19b8e80941Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20b8e80941Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21b8e80941Smrg * OTHER DEALINGS IN THE SOFTWARE.
22b8e80941Smrg */
23b8e80941Smrg
24b8e80941Smrg#include "main/mtypes.h"
25b8e80941Smrg#include "main/buffers.h"
26b8e80941Smrg#include "main/errors.h"
27b8e80941Smrg#include "main/fbobject.h"
28b8e80941Smrg#include "main/get.h"
29b8e80941Smrg#include "main/teximage.h"
30b8e80941Smrg#include "main/texparam.h"
31b8e80941Smrg#include "st_copytex.h"
32b8e80941Smrg
33b8e80941Smrg
34b8e80941Smrg/**
35b8e80941Smrg * Copy a colorbuffer from the window system framebuffer (a window or
36b8e80941Smrg * pbuffer) to a texture.
37b8e80941Smrg * This is a helper used by the wglBindTexImageARB() function.
38b8e80941Smrg *
39b8e80941Smrg * \param srcBuffer  source buffer (GL_FRONT_LEFT, GL_BACK_LEFT, etc)
40b8e80941Smrg * \param fbWidth  width of the source framebuffer
41b8e80941Smrg * \param fbHeight  height of the source framebuffer
42b8e80941Smrg * \param texTarget  which texture target to copy to (GL_TEXTURE_1D/2D/CUBE_MAP)
43b8e80941Smrg * \param texLevel  which texture mipmap level to copy to
44b8e80941Smrg * \param cubeFace  which cube face to copy to (in [0,5])
45b8e80941Smrg * \param texFormat  what texture format to use, if texture doesn't exist
46b8e80941Smrg */
47b8e80941Smrgvoid
48b8e80941Smrgst_copy_framebuffer_to_texture(GLenum srcBuffer,
49b8e80941Smrg                               GLint fbWidth, GLint fbHeight,
50b8e80941Smrg                               GLenum texTarget, GLint texLevel,
51b8e80941Smrg                               GLuint cubeFace, GLenum texFormat)
52b8e80941Smrg{
53b8e80941Smrg   GLint readFBOSave, readBufSave, width, height;
54b8e80941Smrg
55b8e80941Smrg   assert(cubeFace < 6);
56b8e80941Smrg
57b8e80941Smrg   /* Save current FBO / readbuffer */
58b8e80941Smrg   _mesa_GetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &readFBOSave);
59b8e80941Smrg   _mesa_GetIntegerv(GL_READ_BUFFER, &readBufSave);
60b8e80941Smrg
61b8e80941Smrg   /* Read from the winsys buffer */
62b8e80941Smrg   _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, 0);
63b8e80941Smrg   _mesa_ReadBuffer(srcBuffer);
64b8e80941Smrg
65b8e80941Smrg   /* copy image from pbuffer to texture */
66b8e80941Smrg   switch (texTarget) {
67b8e80941Smrg   case GL_TEXTURE_1D:
68b8e80941Smrg      _mesa_GetTexLevelParameteriv(GL_TEXTURE_1D, texLevel,
69b8e80941Smrg                                   GL_TEXTURE_WIDTH, &width);
70b8e80941Smrg      if (width == fbWidth) {
71b8e80941Smrg         /* replace existing texture */
72b8e80941Smrg         _mesa_CopyTexSubImage1D(GL_TEXTURE_1D,
73b8e80941Smrg                                 texLevel,
74b8e80941Smrg                                 0,    /* xoffset */
75b8e80941Smrg                                 0, 0, /* x, y */
76b8e80941Smrg                                 fbWidth);
77b8e80941Smrg      } else {
78b8e80941Smrg         /* define initial texture */
79b8e80941Smrg         _mesa_CopyTexImage1D(GL_TEXTURE_1D,
80b8e80941Smrg                              texLevel,
81b8e80941Smrg                              texFormat,
82b8e80941Smrg                              0, 0, /* x, y */
83b8e80941Smrg                              fbWidth, 0);
84b8e80941Smrg      }
85b8e80941Smrg      break;
86b8e80941Smrg   case GL_TEXTURE_2D:
87b8e80941Smrg      _mesa_GetTexLevelParameteriv(GL_TEXTURE_2D, texLevel,
88b8e80941Smrg                                   GL_TEXTURE_WIDTH, &width);
89b8e80941Smrg      _mesa_GetTexLevelParameteriv(GL_TEXTURE_2D, texLevel,
90b8e80941Smrg                                   GL_TEXTURE_HEIGHT, &height);
91b8e80941Smrg      if (width == fbWidth && height == fbHeight) {
92b8e80941Smrg         /* replace existing texture */
93b8e80941Smrg         _mesa_CopyTexSubImage2D(GL_TEXTURE_2D,
94b8e80941Smrg                                 texLevel,
95b8e80941Smrg                                 0, 0, /* xoffset, yoffset */
96b8e80941Smrg                                 0, 0, /* x, y */
97b8e80941Smrg                                 fbWidth, fbHeight);
98b8e80941Smrg      } else {
99b8e80941Smrg         /* define initial texture */
100b8e80941Smrg         _mesa_CopyTexImage2D(GL_TEXTURE_2D,
101b8e80941Smrg                              texLevel,
102b8e80941Smrg                              texFormat,
103b8e80941Smrg                              0, 0, /* x, y */
104b8e80941Smrg                              fbWidth, fbHeight, 0);
105b8e80941Smrg      }
106b8e80941Smrg      break;
107b8e80941Smrg   case GL_TEXTURE_CUBE_MAP:
108b8e80941Smrg      {
109b8e80941Smrg         const GLenum target =
110b8e80941Smrg            GL_TEXTURE_CUBE_MAP_POSITIVE_X + cubeFace;
111b8e80941Smrg         _mesa_GetTexLevelParameteriv(target, texLevel,
112b8e80941Smrg                                      GL_TEXTURE_WIDTH, &width);
113b8e80941Smrg         _mesa_GetTexLevelParameteriv(target, texLevel,
114b8e80941Smrg                                      GL_TEXTURE_HEIGHT, &height);
115b8e80941Smrg         if (width == fbWidth && height == fbHeight) {
116b8e80941Smrg            /* replace existing texture */
117b8e80941Smrg            _mesa_CopyTexSubImage2D(target,
118b8e80941Smrg                                    texLevel,
119b8e80941Smrg                                    0, 0, /* xoffset, yoffset */
120b8e80941Smrg                                    0, 0, /* x, y */
121b8e80941Smrg                                    fbWidth, fbHeight);
122b8e80941Smrg         } else {
123b8e80941Smrg            /* define new texture */
124b8e80941Smrg            _mesa_CopyTexImage2D(target,
125b8e80941Smrg                                 texLevel,
126b8e80941Smrg                                 texFormat,
127b8e80941Smrg                                 0, 0, /* x, y */
128b8e80941Smrg                                 fbWidth, fbHeight, 0);
129b8e80941Smrg         }
130b8e80941Smrg      }
131b8e80941Smrg      break;
132b8e80941Smrg   default:
133b8e80941Smrg      _mesa_problem(NULL,
134b8e80941Smrg                    "unexpected target in st_copy_framebuffer_to_texture()\n");
135b8e80941Smrg   }
136b8e80941Smrg
137b8e80941Smrg   /* restore readbuffer */
138b8e80941Smrg   _mesa_ReadBuffer(readBufSave);
139b8e80941Smrg   _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, readFBOSave);
140b8e80941Smrg}
141