• 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 
31 #include "tgsi/tgsi_parse.h"
32 
33 #include "rbug_screen.h"
34 #include "rbug_objects.h"
35 #include "rbug_context.h"
36 
37 
38 
39 struct pipe_resource *
rbug_resource_create(struct rbug_screen * rb_screen,struct pipe_resource * resource)40 rbug_resource_create(struct rbug_screen *rb_screen,
41                      struct pipe_resource *resource)
42 {
43    struct rbug_resource *rb_resource;
44 
45    if (!resource)
46       goto error;
47 
48    assert(resource->screen == rb_screen->screen);
49 
50    rb_resource = CALLOC_STRUCT(rbug_resource);
51    if (!rb_resource)
52       goto error;
53 
54    memcpy(&rb_resource->base, resource, sizeof(struct pipe_resource));
55 
56    pipe_reference_init(&rb_resource->base.reference, 1);
57    rb_resource->base.screen = &rb_screen->base;
58    rb_resource->resource = resource;
59 
60    if (resource->target != PIPE_BUFFER)
61       rbug_screen_add_to_list(rb_screen, resources, rb_resource);
62 
63    return &rb_resource->base;
64 
65 error:
66    pipe_resource_reference(&resource, NULL);
67    return NULL;
68 }
69 
70 void
rbug_resource_destroy(struct rbug_resource * rb_resource)71 rbug_resource_destroy(struct rbug_resource *rb_resource)
72 {
73    struct rbug_screen *rb_screen = rbug_screen(rb_resource->base.screen);
74 
75    if (rb_resource->base.target != PIPE_BUFFER)
76       rbug_screen_remove_from_list(rb_screen, resources, rb_resource);
77 
78    pipe_resource_reference(&rb_resource->resource, NULL);
79    FREE(rb_resource);
80 }
81 
82 
83 struct pipe_surface *
rbug_surface_create(struct rbug_context * rb_context,struct rbug_resource * rb_resource,struct pipe_surface * surface)84 rbug_surface_create(struct rbug_context *rb_context,
85                     struct rbug_resource *rb_resource,
86                     struct pipe_surface *surface)
87 {
88    struct rbug_surface *rb_surface;
89 
90    if (!surface)
91       goto error;
92 
93    assert(surface->texture == rb_resource->resource);
94 
95    rb_surface = CALLOC_STRUCT(rbug_surface);
96    if (!rb_surface)
97       goto error;
98 
99    memcpy(&rb_surface->base, surface, sizeof(struct pipe_surface));
100 
101    pipe_reference_init(&rb_surface->base.reference, 1);
102    rb_surface->base.texture = NULL;
103    rb_surface->base.context = &rb_context->base;
104    rb_surface->surface = surface; /* we own the surface already */
105    pipe_resource_reference(&rb_surface->base.texture, &rb_resource->base);
106 
107    return &rb_surface->base;
108 
109 error:
110    pipe_surface_reference(&surface, NULL);
111    return NULL;
112 }
113 
114 void
rbug_surface_destroy(struct rbug_context * rb_context,struct rbug_surface * rb_surface)115 rbug_surface_destroy(struct rbug_context *rb_context,
116                      struct rbug_surface *rb_surface)
117 {
118    pipe_resource_reference(&rb_surface->base.texture, NULL);
119    pipe_surface_reference(&rb_surface->surface, NULL);
120    FREE(rb_surface);
121 }
122 
123 
124 struct pipe_sampler_view *
rbug_sampler_view_create(struct rbug_context * rb_context,struct rbug_resource * rb_resource,struct pipe_sampler_view * view)125 rbug_sampler_view_create(struct rbug_context *rb_context,
126                          struct rbug_resource *rb_resource,
127                          struct pipe_sampler_view *view)
128 {
129    struct rbug_sampler_view *rb_view;
130 
131    if (!view)
132       goto error;
133 
134    assert(view->texture == rb_resource->resource);
135 
136    rb_view = MALLOC(sizeof(struct rbug_sampler_view));
137 
138    rb_view->base = *view;
139    rb_view->base.reference.count = 1;
140    rb_view->base.texture = NULL;
141    pipe_resource_reference(&rb_view->base.texture, &rb_resource->base);
142    rb_view->base.context = &rb_context->base;
143    rb_view->sampler_view = view;
144 
145    return &rb_view->base;
146 error:
147    return NULL;
148 }
149 
150 void
rbug_sampler_view_destroy(struct rbug_context * rb_context,struct rbug_sampler_view * rb_view)151 rbug_sampler_view_destroy(struct rbug_context *rb_context,
152                           struct rbug_sampler_view *rb_view)
153 {
154    pipe_resource_reference(&rb_view->base.texture, NULL);
155    pipe_sampler_view_reference(&rb_view->sampler_view, NULL);
156    FREE(rb_view);
157 }
158 
159 
160 struct pipe_transfer *
rbug_transfer_create(struct rbug_context * rb_context,struct rbug_resource * rb_resource,struct pipe_transfer * transfer)161 rbug_transfer_create(struct rbug_context *rb_context,
162                      struct rbug_resource *rb_resource,
163                      struct pipe_transfer *transfer)
164 {
165    struct rbug_transfer *rb_transfer;
166 
167    if (!transfer)
168       goto error;
169 
170    assert(transfer->resource == rb_resource->resource);
171 
172    rb_transfer = CALLOC_STRUCT(rbug_transfer);
173    if (!rb_transfer)
174       goto error;
175 
176    memcpy(&rb_transfer->base, transfer, sizeof(struct pipe_transfer));
177 
178    rb_transfer->base.resource = NULL;
179    rb_transfer->transfer = transfer;
180    rb_transfer->pipe = rb_context->pipe;
181 
182    pipe_resource_reference(&rb_transfer->base.resource, &rb_resource->base);
183    assert(rb_transfer->base.resource == &rb_resource->base);
184 
185    return &rb_transfer->base;
186 
187 error:
188    if (rb_resource->base.target == PIPE_BUFFER)
189       rb_context->pipe->buffer_unmap(rb_context->pipe, transfer);
190    else
191       rb_context->pipe->texture_unmap(rb_context->pipe, transfer);
192    return NULL;
193 }
194 
195 void
rbug_transfer_destroy(struct rbug_context * rb_context,struct rbug_transfer * rb_transfer)196 rbug_transfer_destroy(struct rbug_context *rb_context,
197                       struct rbug_transfer *rb_transfer)
198 {
199    pipe_resource_reference(&rb_transfer->base.resource, NULL);
200    FREE(rb_transfer);
201 }
202 
203 void *
rbug_shader_create(struct rbug_context * rb_context,const struct pipe_shader_state * state,void * result,enum rbug_shader_type type)204 rbug_shader_create(struct rbug_context *rb_context,
205                    const struct pipe_shader_state *state,
206                    void *result, enum rbug_shader_type type)
207 {
208    struct rbug_shader *rb_shader = CALLOC_STRUCT(rbug_shader);
209 
210    rb_shader->type = type;
211    rb_shader->shader = result;
212    if (state->tokens)
213            rb_shader->tokens = tgsi_dup_tokens(state->tokens);
214 
215    /* works on context as well since its just a macro */
216    rbug_screen_add_to_list(rb_context, shaders, rb_shader);
217 
218    return rb_shader;
219 }
220 
221 void
rbug_shader_destroy(struct rbug_context * rb_context,struct rbug_shader * rb_shader)222 rbug_shader_destroy(struct rbug_context *rb_context,
223                     struct rbug_shader *rb_shader)
224 {
225    struct pipe_context *pipe = rb_context->pipe;
226 
227    /* works on context as well since its just a macro */
228    rbug_screen_remove_from_list(rb_context, shaders, rb_shader);
229 
230    switch(rb_shader->type) {
231    case RBUG_SHADER_FRAGMENT:
232       if (rb_shader->replaced_shader)
233          pipe->delete_fs_state(pipe, rb_shader->replaced_shader);
234       pipe->delete_fs_state(pipe, rb_shader->shader);
235       break;
236    case RBUG_SHADER_VERTEX:
237       if (rb_shader->replaced_shader)
238          pipe->delete_vs_state(pipe, rb_shader->replaced_shader);
239       pipe->delete_vs_state(pipe, rb_shader->shader);
240       break;
241    case RBUG_SHADER_GEOM:
242       if (rb_shader->replaced_shader)
243          pipe->delete_gs_state(pipe, rb_shader->replaced_shader);
244       pipe->delete_gs_state(pipe, rb_shader->shader);
245       break;
246    default:
247       assert(0);
248    }
249 
250    FREE(rb_shader->replaced_tokens);
251    FREE(rb_shader->tokens);
252    FREE(rb_shader);
253 }
254