llvmpipe: handle NULL color buffer pointers
[mesa.git] / src / gallium / drivers / ilo / ilo_context.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2012-2013 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28 #include "util/u_upload_mgr.h"
29 #include "intel_chipset.h"
30
31 #include "ilo_3d.h"
32 #include "ilo_blit.h"
33 #include "ilo_blitter.h"
34 #include "ilo_cp.h"
35 #include "ilo_gpgpu.h"
36 #include "ilo_query.h"
37 #include "ilo_resource.h"
38 #include "ilo_screen.h"
39 #include "ilo_shader.h"
40 #include "ilo_state.h"
41 #include "ilo_transfer.h"
42 #include "ilo_video.h"
43 #include "ilo_context.h"
44
45 static void
46 ilo_context_cp_flushed(struct ilo_cp *cp, void *data)
47 {
48 struct ilo_context *ilo = ilo_context(data);
49
50 if (ilo->last_cp_bo)
51 intel_bo_unreference(ilo->last_cp_bo);
52
53 /* remember the just flushed bo, on which fences could wait */
54 ilo->last_cp_bo = cp->bo;
55 intel_bo_reference(ilo->last_cp_bo);
56
57 ilo_3d_cp_flushed(ilo->hw3d);
58 }
59
60 static void
61 ilo_flush(struct pipe_context *pipe,
62 struct pipe_fence_handle **f,
63 unsigned flags)
64 {
65 struct ilo_context *ilo = ilo_context(pipe);
66
67 if (f) {
68 struct ilo_fence *fence;
69
70 fence = CALLOC_STRUCT(ilo_fence);
71 if (fence) {
72 pipe_reference_init(&fence->reference, 1);
73
74 /* reference the batch bo that we want to wait on */
75 if (ilo_cp_empty(ilo->cp))
76 fence->bo = ilo->last_cp_bo;
77 else
78 fence->bo = ilo->cp->bo;
79
80 if (fence->bo)
81 intel_bo_reference(fence->bo);
82 }
83
84 *f = (struct pipe_fence_handle *) fence;
85 }
86
87 ilo_cp_flush(ilo->cp,
88 (flags & PIPE_FLUSH_END_OF_FRAME) ? "frame end" : "user request");
89 }
90
91 static void
92 ilo_context_destroy(struct pipe_context *pipe)
93 {
94 struct ilo_context *ilo = ilo_context(pipe);
95
96 ilo_cleanup_states(ilo);
97
98 if (ilo->last_cp_bo)
99 intel_bo_unreference(ilo->last_cp_bo);
100
101 if (ilo->uploader)
102 u_upload_destroy(ilo->uploader);
103
104 if (ilo->blitter)
105 ilo_blitter_destroy(ilo->blitter);
106 if (ilo->hw3d)
107 ilo_3d_destroy(ilo->hw3d);
108 if (ilo->shader_cache)
109 ilo_shader_cache_destroy(ilo->shader_cache);
110 if (ilo->cp)
111 ilo_cp_destroy(ilo->cp);
112
113 util_slab_destroy(&ilo->transfer_mempool);
114
115 FREE(ilo);
116 }
117
118 static struct pipe_context *
119 ilo_context_create(struct pipe_screen *screen, void *priv)
120 {
121 struct ilo_screen *is = ilo_screen(screen);
122 struct ilo_context *ilo;
123
124 ilo = CALLOC_STRUCT(ilo_context);
125 if (!ilo)
126 return NULL;
127
128 ilo->winsys = is->winsys;
129 ilo->dev = &is->dev;
130
131 /*
132 * initialize first, otherwise it may not be safe to call
133 * ilo_context_destroy() on errors
134 */
135 util_slab_create(&ilo->transfer_mempool,
136 sizeof(struct ilo_transfer), 64, UTIL_SLAB_SINGLETHREADED);
137
138 ilo->cp = ilo_cp_create(ilo->winsys, is->dev.has_llc);
139 ilo->shader_cache = ilo_shader_cache_create();
140 if (ilo->cp)
141 ilo->hw3d = ilo_3d_create(ilo->cp, ilo->dev);
142
143 if (!ilo->cp || !ilo->shader_cache || !ilo->hw3d) {
144 ilo_context_destroy(&ilo->base);
145 return NULL;
146 }
147
148 ilo->uploader = u_upload_create(&ilo->base, 1024 * 1024, 16,
149 PIPE_BIND_CONSTANT_BUFFER | PIPE_BIND_INDEX_BUFFER);
150 if (!ilo->uploader) {
151 ilo_context_destroy(&ilo->base);
152 return NULL;
153 }
154
155 ilo_cp_set_flush_callback(ilo->cp,
156 ilo_context_cp_flushed, (void *) ilo);
157
158 ilo->base.screen = screen;
159 ilo->base.priv = priv;
160
161 ilo->base.destroy = ilo_context_destroy;
162 ilo->base.flush = ilo_flush;
163
164 ilo_init_3d_functions(ilo);
165 ilo_init_query_functions(ilo);
166 ilo_init_state_functions(ilo);
167 ilo_init_blit_functions(ilo);
168 ilo_init_transfer_functions(ilo);
169 ilo_init_video_functions(ilo);
170 ilo_init_gpgpu_functions(ilo);
171
172 ilo_init_states(ilo);
173
174 /* this must be called last as u_blitter is a client of the pipe context */
175 ilo->blitter = ilo_blitter_create(ilo);
176 if (!ilo->blitter) {
177 ilo_context_destroy(&ilo->base);
178 return NULL;
179 }
180
181 return &ilo->base;
182 }
183
184 /**
185 * Initialize context-related functions.
186 */
187 void
188 ilo_init_context_functions(struct ilo_screen *is)
189 {
190 is->base.context_create = ilo_context_create;
191 }