• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2010 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 #include "util/u_inlines.h"
29 #include "util/u_memory.h"
30 #include "util/simple_list.h"
31 
32 #include "tgsi/tgsi_parse.h"
33 
34 #include "rbug_screen.h"
35 #include "rbug_objects.h"
36 #include "rbug_context.h"
37 
38 
39 
40 struct pipe_resource *
rbug_resource_create(struct rbug_screen * rb_screen,struct pipe_resource * resource)41 rbug_resource_create(struct rbug_screen *rb_screen,
42                      struct pipe_resource *resource)
43 {
44    struct rbug_resource *rb_resource;
45 
46    if (!resource)
47       goto error;
48 
49    assert(resource->screen == rb_screen->screen);
50 
51    rb_resource = CALLOC_STRUCT(rbug_resource);
52    if (!rb_resource)
53       goto error;
54 
55    memcpy(&rb_resource->base, resource, sizeof(struct pipe_resource));
56 
57    pipe_reference_init(&rb_resource->base.reference, 1);
58    rb_resource->base.screen = &rb_screen->base;
59    rb_resource->resource = resource;
60 
61    if (resource->target != PIPE_BUFFER)
62       rbug_screen_add_to_list(rb_screen, resources, rb_resource);
63 
64    return &rb_resource->base;
65 
66 error:
67    pipe_resource_reference(&resource, NULL);
68    return NULL;
69 }
70 
71 void
rbug_resource_destroy(struct rbug_resource * rb_resource)72 rbug_resource_destroy(struct rbug_resource *rb_resource)
73 {
74    struct rbug_screen *rb_screen = rbug_screen(rb_resource->base.screen);
75 
76    if (rb_resource->base.target != PIPE_BUFFER)
77       rbug_screen_remove_from_list(rb_screen, resources, rb_resource);
78 
79    pipe_resource_reference(&rb_resource->resource, NULL);
80    FREE(rb_resource);
81 }
82 
83 
84 struct pipe_surface *
rbug_surface_create(struct rbug_context * rb_context,struct rbug_resource * rb_resource,struct pipe_surface * surface)85 rbug_surface_create(struct rbug_context *rb_context,
86                     struct rbug_resource *rb_resource,
87                     struct pipe_surface *surface)
88 {
89    struct rbug_surface *rb_surface;
90 
91    if (!surface)
92       goto error;
93 
94    assert(surface->texture == rb_resource->resource);
95 
96    rb_surface = CALLOC_STRUCT(rbug_surface);
97    if (!rb_surface)
98       goto error;
99 
100    memcpy(&rb_surface->base, surface, sizeof(struct pipe_surface));
101 
102    pipe_reference_init(&rb_surface->base.reference, 1);
103    rb_surface->base.texture = NULL;
104    rb_surface->base.context = &rb_context->base;
105    rb_surface->surface = surface; /* we own the surface already */
106    pipe_resource_reference(&rb_surface->base.texture, &rb_resource->base);
107 
108    return &rb_surface->base;
109 
110 error:
111    pipe_surface_reference(&surface, NULL);
112    return NULL;
113 }
114 
115 void
rbug_surface_destroy(struct rbug_context * rb_context,struct rbug_surface * rb_surface)116 rbug_surface_destroy(struct rbug_context *rb_context,
117                      struct rbug_surface *rb_surface)
118 {
119    pipe_resource_reference(&rb_surface->base.texture, NULL);
120    pipe_surface_reference(&rb_surface->surface, NULL);
121    FREE(rb_surface);
122 }
123 
124 
125 struct pipe_sampler_view *
rbug_sampler_view_create(struct rbug_context * rb_context,struct rbug_resource * rb_resource,struct pipe_sampler_view * view)126 rbug_sampler_view_create(struct rbug_context *rb_context,
127                          struct rbug_resource *rb_resource,
128                          struct pipe_sampler_view *view)
129 {
130    struct rbug_sampler_view *rb_view;
131 
132    if (!view)
133       goto error;
134 
135    assert(view->texture == rb_resource->resource);
136 
137    rb_view = MALLOC(sizeof(struct rbug_sampler_view));
138 
139    rb_view->base = *view;
140    rb_view->base.reference.count = 1;
141    rb_view->base.texture = NULL;
142    pipe_resource_reference(&rb_view->base.texture, &rb_resource->base);
143    rb_view->base.context = &rb_context->base;
144    rb_view->sampler_view = view;
145 
146    return &rb_view->base;
147 error:
148    return NULL;
149 }
150 
151 void
rbug_sampler_view_destroy(struct rbug_context * rb_context,struct rbug_sampler_view * rb_view)152 rbug_sampler_view_destroy(struct rbug_context *rb_context,
153                           struct rbug_sampler_view *rb_view)
154 {
155    pipe_resource_reference(&rb_view->base.texture, NULL);
156    pipe_sampler_view_reference(&rb_view->sampler_view, NULL);
157    FREE(rb_view);
158 }
159 
160 
161 struct pipe_transfer *
rbug_transfer_create(struct rbug_context * rb_context,struct rbug_resource * rb_resource,struct pipe_transfer * transfer)162 rbug_transfer_create(struct rbug_context *rb_context,
163                      struct rbug_resource *rb_resource,
164                      struct pipe_transfer *transfer)
165 {
166    struct rbug_transfer *rb_transfer;
167 
168    if (!transfer)
169       goto error;
170 
171    assert(transfer->resource == rb_resource->resource);
172 
173    rb_transfer = CALLOC_STRUCT(rbug_transfer);
174    if (!rb_transfer)
175       goto error;
176 
177    memcpy(&rb_transfer->base, transfer, sizeof(struct pipe_transfer));
178 
179    rb_transfer->base.resource = NULL;
180    rb_transfer->transfer = transfer;
181    rb_transfer->pipe = rb_context->pipe;
182 
183    pipe_resource_reference(&rb_transfer->base.resource, &rb_resource->base);
184    assert(rb_transfer->base.resource == &rb_resource->base);
185 
186    return &rb_transfer->base;
187 
188 error:
189    if (rb_resource->base.target == PIPE_BUFFER)
190       rb_context->pipe->buffer_unmap(rb_context->pipe, transfer);
191    else
192       rb_context->pipe->texture_unmap(rb_context->pipe, transfer);
193    return NULL;
194 }
195 
196 void
rbug_transfer_destroy(struct rbug_context * rb_context,struct rbug_transfer * rb_transfer)197 rbug_transfer_destroy(struct rbug_context *rb_context,
198                       struct rbug_transfer *rb_transfer)
199 {
200    pipe_resource_reference(&rb_transfer->base.resource, NULL);
201    FREE(rb_transfer);
202 }
203 
204 void *
rbug_shader_create(struct rbug_context * rb_context,const struct pipe_shader_state * state,void * result,enum rbug_shader_type type)205 rbug_shader_create(struct rbug_context *rb_context,
206                    const struct pipe_shader_state *state,
207                    void *result, enum rbug_shader_type type)
208 {
209    struct rbug_shader *rb_shader = CALLOC_STRUCT(rbug_shader);
210 
211    rb_shader->type = type;
212    rb_shader->shader = result;
213    if (state->tokens)
214            rb_shader->tokens = tgsi_dup_tokens(state->tokens);
215 
216    /* works on context as well since its just a macro */
217    rbug_screen_add_to_list(rb_context, shaders, rb_shader);
218 
219    return rb_shader;
220 }
221 
222 void
rbug_shader_destroy(struct rbug_context * rb_context,struct rbug_shader * rb_shader)223 rbug_shader_destroy(struct rbug_context *rb_context,
224                     struct rbug_shader *rb_shader)
225 {
226    struct pipe_context *pipe = rb_context->pipe;
227 
228    /* works on context as well since its just a macro */
229    rbug_screen_remove_from_list(rb_context, shaders, rb_shader);
230 
231    switch(rb_shader->type) {
232    case RBUG_SHADER_FRAGMENT:
233       if (rb_shader->replaced_shader)
234          pipe->delete_fs_state(pipe, rb_shader->replaced_shader);
235       pipe->delete_fs_state(pipe, rb_shader->shader);
236       break;
237    case RBUG_SHADER_VERTEX:
238       if (rb_shader->replaced_shader)
239          pipe->delete_vs_state(pipe, rb_shader->replaced_shader);
240       pipe->delete_vs_state(pipe, rb_shader->shader);
241       break;
242    case RBUG_SHADER_GEOM:
243       if (rb_shader->replaced_shader)
244          pipe->delete_gs_state(pipe, rb_shader->replaced_shader);
245       pipe->delete_gs_state(pipe, rb_shader->shader);
246       break;
247    default:
248       assert(0);
249    }
250 
251    FREE(rb_shader->replaced_tokens);
252    FREE(rb_shader->tokens);
253    FREE(rb_shader);
254 }
255