summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/rbug/rbug_objects.c
blob: 25d55a383c2f4388ecef64fb433ac824e8134bf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**************************************************************************
 *
 * Copyright 2010 VMware, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#include "util/u_inlines.h"
#include "util/u_memory.h"
#include "util/simple_list.h"

#include "tgsi/tgsi_parse.h"

#include "rbug_screen.h"
#include "rbug_objects.h"
#include "rbug_context.h"



struct pipe_resource *
rbug_resource_create(struct rbug_screen *rb_screen,
                     struct pipe_resource *resource)
{
   struct rbug_resource *rb_resource;

   if(!resource)
      goto error;

   assert(resource->screen == rb_screen->screen);

   rb_resource = CALLOC_STRUCT(rbug_resource);
   if(!rb_resource)
      goto error;

   memcpy(&rb_resource->base, resource, sizeof(struct pipe_resource));

   pipe_reference_init(&rb_resource->base.reference, 1);
   rb_resource->base.screen = &rb_screen->base;
   rb_resource->resource = resource;

   if (resource->target != PIPE_BUFFER)
      rbug_screen_add_to_list(rb_screen, resources, rb_resource);

   return &rb_resource->base;

error:
   pipe_resource_reference(&resource, NULL);
   return NULL;
}

void
rbug_resource_destroy(struct rbug_resource *rb_resource)
{
   struct rbug_screen *rb_screen = rbug_screen(rb_resource->base.screen);

   if (rb_resource->base.target != PIPE_BUFFER)
      rbug_screen_remove_from_list(rb_screen, resources, rb_resource);

   pipe_resource_reference(&rb_resource->resource, NULL);
   FREE(rb_resource);
}


struct pipe_surface *
rbug_surface_create(struct rbug_context *rb_context,
                    struct rbug_resource *rb_resource,
                    struct pipe_surface *surface)
{
   struct rbug_surface *rb_surface;

   if(!surface)
      goto error;

   assert(surface->texture == rb_resource->resource);

   rb_surface = CALLOC_STRUCT(rbug_surface);
   if(!rb_surface)
      goto error;

   memcpy(&rb_surface->base, surface, sizeof(struct pipe_surface));

   pipe_reference_init(&rb_surface->base.reference, 1);
   rb_surface->base.texture = NULL;
   rb_surface->base.context = &rb_context->base;
   rb_surface->surface = surface; /* we own the surface already */
   pipe_resource_reference(&rb_surface->base.texture, &rb_resource->base);

   return &rb_surface->base;

error:
   pipe_surface_reference(&surface, NULL);
   return NULL;
}

void
rbug_surface_destroy(struct rbug_context *rb_context,
                     struct rbug_surface *rb_surface)
{
   pipe_resource_reference(&rb_surface->base.texture, NULL);
   pipe_surface_reference(&rb_surface->surface, NULL);
   FREE(rb_surface);
}


struct pipe_sampler_view *
rbug_sampler_view_create(struct rbug_context *rb_context,
                         struct rbug_resource *rb_resource,
                         struct pipe_sampler_view *view)
{
   struct rbug_sampler_view *rb_view;

   if (!view)
      goto error;

   assert(view->texture == rb_resource->resource);

   rb_view = MALLOC(sizeof(struct rbug_sampler_view));

   rb_view->base = *view;
   rb_view->base.reference.count = 1;
   rb_view->base.texture = NULL;
   pipe_resource_reference(&rb_view->base.texture, &rb_resource->base);
   rb_view->base.context = &rb_context->base;
   rb_view->sampler_view = view;

   return &rb_view->base;
error:
   return NULL;
}

void
rbug_sampler_view_destroy(struct rbug_context *rb_context,
                          struct rbug_sampler_view *rb_view)
{
   pipe_resource_reference(&rb_view->base.texture, NULL);
   pipe_sampler_view_reference(&rb_view->sampler_view, NULL);
   FREE(rb_view);
}


struct pipe_transfer *
rbug_transfer_create(struct rbug_context *rb_context,
                     struct rbug_resource *rb_resource,
                     struct pipe_transfer *transfer)
{
   struct rbug_transfer *rb_transfer;

   if(!transfer)
      goto error;

   assert(transfer->resource == rb_resource->resource);

   rb_transfer = CALLOC_STRUCT(rbug_transfer);
   if(!rb_transfer)
      goto error;

   memcpy(&rb_transfer->base, transfer, sizeof(struct pipe_transfer));

   rb_transfer->base.resource = NULL;
   rb_transfer->transfer = transfer;
   rb_transfer->pipe = rb_context->pipe;

   pipe_resource_reference(&rb_transfer->base.resource, &rb_resource->base);
   assert(rb_transfer->base.resource == &rb_resource->base);

   return &rb_transfer->base;

error:
   rb_context->pipe->transfer_unmap(rb_context->pipe, transfer);
   return NULL;
}

void
rbug_transfer_destroy(struct rbug_context *rb_context,
                      struct rbug_transfer *rb_transfer)
{
   pipe_resource_reference(&rb_transfer->base.resource, NULL);
   FREE(rb_transfer);
}

void *
rbug_shader_create(struct rbug_context *rb_context,
                   const struct pipe_shader_state *state,
                   void *result, enum rbug_shader_type type)
{
   struct rbug_shader *rb_shader = CALLOC_STRUCT(rbug_shader);

   rb_shader->type = type;
   rb_shader->shader = result;
   rb_shader->tokens = tgsi_dup_tokens(state->tokens);

   /* works on context as well since its just a macro */
   rbug_screen_add_to_list(rb_context, shaders, rb_shader);

   return rb_shader;
}

void
rbug_shader_destroy(struct rbug_context *rb_context,
                    struct rbug_shader *rb_shader)
{
   struct pipe_context *pipe = rb_context->pipe;

   /* works on context as well since its just a macro */
   rbug_screen_remove_from_list(rb_context, shaders, rb_shader);

   switch(rb_shader->type) {
   case RBUG_SHADER_FRAGMENT:
      if (rb_shader->replaced_shader)
         pipe->delete_fs_state(pipe, rb_shader->replaced_shader);
      pipe->delete_fs_state(pipe, rb_shader->shader);
      break;
   case RBUG_SHADER_VERTEX:
      if (rb_shader->replaced_shader)
         pipe->delete_vs_state(pipe, rb_shader->replaced_shader);
      pipe->delete_vs_state(pipe, rb_shader->shader);
      break;
   case RBUG_SHADER_GEOM:
      if (rb_shader->replaced_shader)
         pipe->delete_gs_state(pipe, rb_shader->replaced_shader);
      pipe->delete_gs_state(pipe, rb_shader->shader);
      break;
   default:
      assert(0);
   }

   FREE(rb_shader->replaced_tokens);
   FREE(rb_shader->tokens);
   FREE(rb_shader);
}