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