xref: /OK3568_Linux_fs/external/xserver/glamor/glamor_vbo.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright © 2014 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * 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 OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 /**
25  * @file glamor_vbo.c
26  *
27  * Helpers for managing streamed vertex bufffers used in glamor.
28  */
29 
30 #include "glamor_priv.h"
31 
32 /** Default size of the VBO, in bytes.
33  *
34  * If a single request is larger than this size, we'll resize the VBO
35  * and return an appropriate mapping, but we'll resize back down after
36  * that to avoid hogging that memory forever.  We don't anticipate
37  * normal usage actually requiring larger VBO sizes.
38  */
39 #define GLAMOR_VBO_SIZE (512 * 1024)
40 
41 /**
42  * Returns a pointer to @size bytes of VBO storage, which should be
43  * accessed by the GL using vbo_offset within the VBO.
44  */
45 void *
glamor_get_vbo_space(ScreenPtr screen,unsigned size,char ** vbo_offset)46 glamor_get_vbo_space(ScreenPtr screen, unsigned size, char **vbo_offset)
47 {
48     glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
49     void *data;
50 
51     glamor_make_current(glamor_priv);
52 
53     glBindBuffer(GL_ARRAY_BUFFER, glamor_priv->vbo);
54 
55     if (glamor_priv->has_buffer_storage) {
56         if (glamor_priv->vbo_size < glamor_priv->vbo_offset + size) {
57             if (glamor_priv->vbo_size)
58                 glUnmapBuffer(GL_ARRAY_BUFFER);
59 
60             if (size > glamor_priv->vbo_size) {
61                 glamor_priv->vbo_size = MAX(GLAMOR_VBO_SIZE, size);
62 
63                 /* We aren't allowed to resize glBufferStorage()
64                  * buffers, so we need to gen a new one.
65                  */
66                 glDeleteBuffers(1, &glamor_priv->vbo);
67                 glGenBuffers(1, &glamor_priv->vbo);
68                 glBindBuffer(GL_ARRAY_BUFFER, glamor_priv->vbo);
69 
70                 assert(glGetError() == GL_NO_ERROR);
71                 glBufferStorage(GL_ARRAY_BUFFER, glamor_priv->vbo_size, NULL,
72                                 GL_MAP_WRITE_BIT |
73                                 GL_MAP_PERSISTENT_BIT |
74                                 GL_MAP_COHERENT_BIT);
75 
76                 if (glGetError() != GL_NO_ERROR) {
77                     /* If the driver failed our coherent mapping, fall
78                      * back to the ARB_mbr path.
79                      */
80                     glamor_priv->has_buffer_storage = false;
81                     glamor_priv->vbo_size = 0;
82 
83                     return glamor_get_vbo_space(screen, size, vbo_offset);
84                 }
85             }
86 
87             glamor_priv->vbo_offset = 0;
88             glamor_priv->vb = glMapBufferRange(GL_ARRAY_BUFFER,
89                                                0, glamor_priv->vbo_size,
90                                                GL_MAP_WRITE_BIT |
91                                                GL_MAP_INVALIDATE_BUFFER_BIT |
92                                                GL_MAP_PERSISTENT_BIT |
93                                                GL_MAP_COHERENT_BIT);
94         }
95         *vbo_offset = (void *)(uintptr_t)glamor_priv->vbo_offset;
96         data = glamor_priv->vb + glamor_priv->vbo_offset;
97         glamor_priv->vbo_offset += size;
98     } else if (glamor_priv->has_map_buffer_range) {
99         /* Avoid GL errors on GL 4.5 / ES 3.0 with mapping size == 0,
100          * which callers may sometimes pass us (for example, if
101          * clipping leads to zero rectangles left).  Prior to that
102          * version, Mesa would sometimes throw errors on unmapping a
103          * zero-size mapping.
104          */
105         if (size == 0)
106             return NULL;
107 
108         if (glamor_priv->vbo_size < glamor_priv->vbo_offset + size) {
109             glamor_priv->vbo_size = MAX(GLAMOR_VBO_SIZE, size);
110             glamor_priv->vbo_offset = 0;
111             glBufferData(GL_ARRAY_BUFFER,
112                          glamor_priv->vbo_size, NULL, GL_STREAM_DRAW);
113         }
114 
115         data = glMapBufferRange(GL_ARRAY_BUFFER,
116                                 glamor_priv->vbo_offset,
117                                 size,
118                                 GL_MAP_WRITE_BIT |
119                                 GL_MAP_UNSYNCHRONIZED_BIT |
120                                 GL_MAP_INVALIDATE_RANGE_BIT);
121         *vbo_offset = (char *)(uintptr_t)glamor_priv->vbo_offset;
122         glamor_priv->vbo_offset += size;
123         glamor_priv->vbo_mapped = TRUE;
124     } else {
125         /* Return a pointer to the statically allocated non-VBO
126          * memory. We'll upload it through glBufferData() later.
127          */
128         if (glamor_priv->vbo_size < size) {
129             glamor_priv->vbo_size = MAX(GLAMOR_VBO_SIZE, size);
130             free(glamor_priv->vb);
131             glamor_priv->vb = xnfalloc(glamor_priv->vbo_size);
132         }
133         *vbo_offset = NULL;
134         /* We point to the start of glamor_priv->vb every time, and
135          * the vbo_offset determines the size of the glBufferData().
136          */
137         glamor_priv->vbo_offset = size;
138         data = glamor_priv->vb;
139     }
140 
141     return data;
142 }
143 
144 void
glamor_put_vbo_space(ScreenPtr screen)145 glamor_put_vbo_space(ScreenPtr screen)
146 {
147     glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
148 
149     glamor_make_current(glamor_priv);
150 
151     if (glamor_priv->has_buffer_storage) {
152         /* If we're in the ARB_buffer_storage path, we have a
153          * persistent mapping, so we can leave it around until we
154          * reach the end of the buffer.
155          */
156     } else if (glamor_priv->has_map_buffer_range) {
157         if (glamor_priv->vbo_mapped) {
158             glUnmapBuffer(GL_ARRAY_BUFFER);
159             glamor_priv->vbo_mapped = FALSE;
160         }
161     } else {
162         glBufferData(GL_ARRAY_BUFFER, glamor_priv->vbo_offset,
163                      glamor_priv->vb, GL_DYNAMIC_DRAW);
164     }
165 
166     glBindBuffer(GL_ARRAY_BUFFER, 0);
167 }
168 
169 void
glamor_init_vbo(ScreenPtr screen)170 glamor_init_vbo(ScreenPtr screen)
171 {
172     glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
173 
174     glamor_make_current(glamor_priv);
175 
176     glGenBuffers(1, &glamor_priv->vbo);
177     glGenVertexArrays(1, &glamor_priv->vao);
178     glBindVertexArray(glamor_priv->vao);
179 }
180 
181 void
glamor_fini_vbo(ScreenPtr screen)182 glamor_fini_vbo(ScreenPtr screen)
183 {
184     glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
185 
186     glamor_make_current(glamor_priv);
187 
188     glDeleteVertexArrays(1, &glamor_priv->vao);
189     glamor_priv->vao = 0;
190     if (!glamor_priv->has_map_buffer_range)
191         free(glamor_priv->vb);
192 }
193