rbug_context.c 42.9 KB
Newer Older
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
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
/**************************************************************************
 *
 * 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 "pipe/p_context.h"
#include "util/u_memory.h"
#include "util/u_inlines.h"
32
#include "util/simple_list.h"
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
33

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
34 35
#include "rbug/rbug_context.h"

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
36 37 38 39 40 41 42
#include "rbug_context.h"
#include "rbug_objects.h"


static void
rbug_destroy(struct pipe_context *_pipe)
{
43
   struct rbug_screen *rb_screen = rbug_screen(_pipe->screen);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
44 45 46
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;

47 48
   rbug_screen_remove_from_list(rb_screen, contexts, rb_pipe);

49
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
50
   pipe->destroy(pipe);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
51
   rb_pipe->pipe = NULL;
52
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
53 54 55 56

   FREE(rb_pipe);
}

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
57 58 59 60 61 62 63 64
static void
rbug_draw_block_locked(struct rbug_context *rb_pipe, int flag)
{

   if (rb_pipe->draw_blocker & flag) {
      rb_pipe->draw_blocked |= flag;
   } else if ((rb_pipe->draw_rule.blocker & flag) &&
              (rb_pipe->draw_blocker & RBUG_BLOCK_RULE)) {
65
      unsigned k;
66
      bool block = false;
67 68
      unsigned sh;

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
69
      debug_printf("%s (%p %p) (%p %p) (%p %u) (%p %u)\n", __FUNCTION__,
70 71 72 73
                   (void *) rb_pipe->draw_rule.shader[PIPE_SHADER_FRAGMENT],
                   (void *) rb_pipe->curr.shader[PIPE_SHADER_FRAGMENT],
                   (void *) rb_pipe->draw_rule.shader[PIPE_SHADER_VERTEX],
                   (void *) rb_pipe->curr.shader[PIPE_SHADER_VERTEX],
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
74 75
                   (void *) rb_pipe->draw_rule.surf, 0,
                   (void *) rb_pipe->draw_rule.texture, 0);
76 77 78
      for (sh = 0; sh < PIPE_SHADER_TYPES; sh++) {
         if (rb_pipe->draw_rule.shader[sh] &&
             rb_pipe->draw_rule.shader[sh] == rb_pipe->curr.shader[sh])
79
            block = true;
80 81
      }

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
82 83
      if (rb_pipe->draw_rule.surf &&
          rb_pipe->draw_rule.surf == rb_pipe->curr.zsbuf)
84
            block = true;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
85 86 87
      if (rb_pipe->draw_rule.surf)
         for (k = 0; k < rb_pipe->curr.nr_cbufs; k++)
            if (rb_pipe->draw_rule.surf == rb_pipe->curr.cbufs[k])
88
               block = true;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
89
      if (rb_pipe->draw_rule.texture) {
Brian Paul's avatar
Brian Paul committed
90
         for (sh = 0; sh < ARRAY_SIZE(rb_pipe->curr.num_views); sh++) {
91 92
            for (k = 0; k < rb_pipe->curr.num_views[sh]; k++) {
               if (rb_pipe->draw_rule.texture == rb_pipe->curr.texs[sh][k]) {
93
                  block = true;
94 95 96
                  sh = PIPE_SHADER_TYPES; /* to break out of both loops */
                  break;
               }
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110
            }
         }
      }

      if (block)
         rb_pipe->draw_blocked |= (flag | RBUG_BLOCK_RULE);
   }

   if (rb_pipe->draw_blocked)
      rbug_notify_draw_blocked(rb_pipe);

   /* wait for rbug to clear the blocked flag */
   while (rb_pipe->draw_blocked & flag) {
      rb_pipe->draw_blocked |= flag;
111
      cnd_wait(&rb_pipe->draw_cond, &rb_pipe->draw_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
112 113 114 115
   }

}

116
static void
117 118
rbug_draw_vbo(struct pipe_context *_pipe, const struct pipe_draw_info *_info,
              const struct pipe_draw_indirect_info *_indirect)
119 120 121
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;
122 123 124 125 126
   struct pipe_draw_info info;

   info = *_info;
   if(_info->index_size && !_info->has_user_indices)
       info.index.resource = rbug_resource_unwrap(_info->index.resource);
127

128
   mtx_lock(&rb_pipe->draw_mutex);
129 130
   rbug_draw_block_locked(rb_pipe, RBUG_BLOCK_BEFORE);

131
   mtx_lock(&rb_pipe->call_mutex);
132 133 134 135
   /* XXX loop over PIPE_SHADER_x here */
   if (!(rb_pipe->curr.shader[PIPE_SHADER_FRAGMENT] && rb_pipe->curr.shader[PIPE_SHADER_FRAGMENT]->disabled) &&
       !(rb_pipe->curr.shader[PIPE_SHADER_GEOMETRY] && rb_pipe->curr.shader[PIPE_SHADER_GEOMETRY]->disabled) &&
       !(rb_pipe->curr.shader[PIPE_SHADER_VERTEX] && rb_pipe->curr.shader[PIPE_SHADER_VERTEX]->disabled))
136
      pipe->draw_vbo(pipe, &info, _indirect);
137
   mtx_unlock(&rb_pipe->call_mutex);
138 139

   rbug_draw_block_locked(rb_pipe, RBUG_BLOCK_AFTER);
140
   mtx_unlock(&rb_pipe->draw_mutex);
141 142
}

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
143 144
static struct pipe_query *
rbug_create_query(struct pipe_context *_pipe,
145 146
                  unsigned query_type,
                  unsigned index)
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
147 148 149
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;
150
   struct pipe_query *query;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
151

152
   mtx_lock(&rb_pipe->call_mutex);
153 154 155
   query = pipe->create_query(pipe,
                              query_type,
                              index);
156
   mtx_unlock(&rb_pipe->call_mutex);
157
   return query;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
158 159 160 161 162 163 164 165 166
}

static void
rbug_destroy_query(struct pipe_context *_pipe,
                   struct pipe_query *query)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;

167
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
168 169
   pipe->destroy_query(pipe,
                       query);
170
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
171 172
}

173
static bool
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
174 175 176 177 178
rbug_begin_query(struct pipe_context *_pipe,
                 struct pipe_query *query)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;
179
   bool ret;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
180

181
   mtx_lock(&rb_pipe->call_mutex);
182
   ret = pipe->begin_query(pipe, query);
183
   mtx_unlock(&rb_pipe->call_mutex);
184
   return ret;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
185 186
}

187
static bool
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
188 189 190 191 192
rbug_end_query(struct pipe_context *_pipe,
               struct pipe_query *query)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;
193
   bool ret;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
194

195
   mtx_lock(&rb_pipe->call_mutex);
196 197
   ret = pipe->end_query(pipe,
                         query);
198
   mtx_unlock(&rb_pipe->call_mutex);
199 200

   return ret;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
201 202
}

203
static bool
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
204 205
rbug_get_query_result(struct pipe_context *_pipe,
                      struct pipe_query *query,
206
                      bool wait,
207
                      union pipe_query_result *result)
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
208 209 210
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;
211
   bool ret;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
212

213
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
214 215 216 217
   ret = pipe->get_query_result(pipe,
                                query,
                                wait,
                                result);
218
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
219 220

   return ret;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
221 222
}

223
static void
224
rbug_set_active_query_state(struct pipe_context *_pipe, bool enable)
225 226 227 228
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;

229
   mtx_lock(&rb_pipe->call_mutex);
230
   pipe->set_active_query_state(pipe, enable);
231
   mtx_unlock(&rb_pipe->call_mutex);
232 233
}

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
234 235 236 237 238 239
static void *
rbug_create_blend_state(struct pipe_context *_pipe,
                        const struct pipe_blend_state *blend)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
240 241
   void *ret;

242
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
243 244
   ret = pipe->create_blend_state(pipe,
                                  blend);
245
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
246

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
247
   return ret;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
248 249 250 251 252 253 254 255 256
}

static void
rbug_bind_blend_state(struct pipe_context *_pipe,
                      void *blend)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;

257
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
258
   pipe->bind_blend_state(pipe,
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
259
                          blend);
260
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
261 262 263 264 265 266 267 268 269
}

static void
rbug_delete_blend_state(struct pipe_context *_pipe,
                        void *blend)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;

270
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
271 272
   pipe->delete_blend_state(pipe,
                            blend);
273
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
274 275 276 277 278 279 280 281
}

static void *
rbug_create_sampler_state(struct pipe_context *_pipe,
                          const struct pipe_sampler_state *sampler)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
282
   void *ret;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
283

284
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
285 286
   ret = pipe->create_sampler_state(pipe,
                                    sampler);
287
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
288 289

   return ret;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
290 291
}

292
static void
293 294
rbug_bind_sampler_states(struct pipe_context *_pipe,
                         enum pipe_shader_type shader,
295 296 297 298 299 300
                         unsigned start, unsigned count,
                         void **samplers)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;

301
   mtx_lock(&rb_pipe->call_mutex);
302
   pipe->bind_sampler_states(pipe, shader, start, count, samplers);
303
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
304 305 306 307 308 309 310 311 312
}

static void
rbug_delete_sampler_state(struct pipe_context *_pipe,
                          void *sampler)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;

313
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
314 315
   pipe->delete_sampler_state(pipe,
                              sampler);
316
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
317 318 319 320 321 322 323 324
}

static void *
rbug_create_rasterizer_state(struct pipe_context *_pipe,
                             const struct pipe_rasterizer_state *rasterizer)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
325 326
   void *ret;

327
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
328 329
   ret = pipe->create_rasterizer_state(pipe,
                                       rasterizer);
330
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
331

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
332
   return ret;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
333 334 335 336 337 338 339 340 341
}

static void
rbug_bind_rasterizer_state(struct pipe_context *_pipe,
                           void *rasterizer)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;

342
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
343 344
   pipe->bind_rasterizer_state(pipe,
                               rasterizer);
345
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
346 347 348 349 350 351 352 353 354
}

static void
rbug_delete_rasterizer_state(struct pipe_context *_pipe,
                             void *rasterizer)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;

355
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
356 357
   pipe->delete_rasterizer_state(pipe,
                                 rasterizer);
358
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
359 360 361 362 363 364 365 366
}

static void *
rbug_create_depth_stencil_alpha_state(struct pipe_context *_pipe,
                                      const struct pipe_depth_stencil_alpha_state *depth_stencil_alpha)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
367
   void *ret;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
368

369
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
370 371
   ret = pipe->create_depth_stencil_alpha_state(pipe,
                                                depth_stencil_alpha);
372
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
373 374

   return ret;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
375 376 377 378 379 380 381 382 383
}

static void
rbug_bind_depth_stencil_alpha_state(struct pipe_context *_pipe,
                                    void *depth_stencil_alpha)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;

384
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
385 386
   pipe->bind_depth_stencil_alpha_state(pipe,
                                        depth_stencil_alpha);
387
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
388 389 390 391 392 393 394 395 396
}

static void
rbug_delete_depth_stencil_alpha_state(struct pipe_context *_pipe,
                                      void *depth_stencil_alpha)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;

397
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
398 399
   pipe->delete_depth_stencil_alpha_state(pipe,
                                          depth_stencil_alpha);
400
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
401 402 403 404
}

static void *
rbug_create_fs_state(struct pipe_context *_pipe,
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
405
                     const struct pipe_shader_state *state)
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
406 407 408
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
409
   void *result;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
410

411
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
412
   result = pipe->create_fs_state(pipe, state);
413
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
414

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
415 416 417 418
   if (!result)
      return NULL;

   return rbug_shader_create(rb_pipe, state, result, RBUG_SHADER_FRAGMENT);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
419 420 421 422
}

static void
rbug_bind_fs_state(struct pipe_context *_pipe,
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
423
                   void *_fs)
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
424 425 426
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
427
   void *fs;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
428

429
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
430

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
431
   fs = rbug_shader_unwrap(_fs);
432
   rb_pipe->curr.shader[PIPE_SHADER_FRAGMENT] = rbug_shader(_fs);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
433 434
   pipe->bind_fs_state(pipe,
                       fs);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
435

436
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
437 438 439 440
}

static void
rbug_delete_fs_state(struct pipe_context *_pipe,
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
441
                     void *_fs)
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
442 443
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
444
   struct rbug_shader *rb_shader = rbug_shader(_fs);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
445

446
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
447
   rbug_shader_destroy(rb_pipe, rb_shader);
448
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
449 450 451 452
}

static void *
rbug_create_vs_state(struct pipe_context *_pipe,
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
453
                     const struct pipe_shader_state *state)
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
454 455 456
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
457 458
   void *result;

459
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
460
   result = pipe->create_vs_state(pipe, state);
461
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
462

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
463 464
   if (!result)
      return NULL;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
465

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
466
   return rbug_shader_create(rb_pipe, state, result, RBUG_SHADER_VERTEX);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
467 468 469 470
}

static void
rbug_bind_vs_state(struct pipe_context *_pipe,
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
471
                   void *_vs)
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
472 473 474
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
475
   void *vs;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
476

477
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
478

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
479
   vs = rbug_shader_unwrap(_vs);
480
   rb_pipe->curr.shader[PIPE_SHADER_VERTEX] = rbug_shader(_vs);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
481 482
   pipe->bind_vs_state(pipe,
                       vs);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
483

484
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
485 486 487 488
}

static void
rbug_delete_vs_state(struct pipe_context *_pipe,
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
489 490 491 492 493
                     void *_vs)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct rbug_shader *rb_shader = rbug_shader(_vs);

494
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
495
   rbug_shader_destroy(rb_pipe, rb_shader);
496
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
497 498 499 500 501 502 503 504 505 506
}

static void *
rbug_create_gs_state(struct pipe_context *_pipe,
                     const struct pipe_shader_state *state)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;
   void *result;

507
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
508
   result = pipe->create_gs_state(pipe, state);
509
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
510

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
511 512 513 514 515 516 517 518 519
   if (!result)
      return NULL;

   return rbug_shader_create(rb_pipe, state, result, RBUG_SHADER_GEOM);
}

static void
rbug_bind_gs_state(struct pipe_context *_pipe,
                   void *_gs)
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
520 521 522
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
523
   void *gs;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
524

525
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
526

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
527
   gs = rbug_shader_unwrap(_gs);
528
   rb_pipe->curr.shader[PIPE_SHADER_GEOMETRY] = rbug_shader(_gs);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
529 530
   pipe->bind_gs_state(pipe,
                       gs);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
531

532
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
533 534
}

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
535 536 537 538 539 540 541
static void
rbug_delete_gs_state(struct pipe_context *_pipe,
                     void *_gs)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct rbug_shader *rb_shader = rbug_shader(_gs);

542
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
543
   rbug_shader_destroy(rb_pipe, rb_shader);
544
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
545
}
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
546 547 548 549 550 551 552 553

static void *
rbug_create_vertex_elements_state(struct pipe_context *_pipe,
                                  unsigned num_elements,
                                  const struct pipe_vertex_element *vertex_elements)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
554
   void *ret;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
555

556
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
557
   ret = pipe->create_vertex_elements_state(pipe,
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
558 559
                                             num_elements,
                                             vertex_elements);
560
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
561 562

   return ret;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
563 564 565 566 567 568 569 570 571
}

static void
rbug_bind_vertex_elements_state(struct pipe_context *_pipe,
                                void *velems)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;

572
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
573 574
   pipe->bind_vertex_elements_state(pipe,
                                    velems);
575
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
576 577 578 579 580 581 582 583 584
}

static void
rbug_delete_vertex_elements_state(struct pipe_context *_pipe,
                                  void *velems)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;

585
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
586 587
   pipe->delete_vertex_elements_state(pipe,
                                      velems);
588
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
589 590 591 592 593 594 595 596 597
}

static void
rbug_set_blend_color(struct pipe_context *_pipe,
                     const struct pipe_blend_color *blend_color)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;

598
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
599 600
   pipe->set_blend_color(pipe,
                         blend_color);
601
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
602 603 604 605 606 607 608 609 610
}

static void
rbug_set_stencil_ref(struct pipe_context *_pipe,
                     const struct pipe_stencil_ref *stencil_ref)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;

611
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
612 613
   pipe->set_stencil_ref(pipe,
                         stencil_ref);
614
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
615 616 617 618 619 620 621 622 623
}

static void
rbug_set_clip_state(struct pipe_context *_pipe,
                    const struct pipe_clip_state *clip)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;

624
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
625 626
   pipe->set_clip_state(pipe,
                        clip);
627
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
628 629 630 631
}

static void
rbug_set_constant_buffer(struct pipe_context *_pipe,
632
                         enum pipe_shader_type shader,
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
633
                         uint index,
634
                         const struct pipe_constant_buffer *_cb)
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
635 636 637
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;
638
   struct pipe_constant_buffer cb;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
639 640

   /* XXX hmm? unwrap the input state */
641 642 643
   if (_cb) {
      cb = *_cb;
      cb.buffer = rbug_resource_unwrap(_cb->buffer);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
644 645
   }

646
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
647 648 649
   pipe->set_constant_buffer(pipe,
                             shader,
                             index,
650
                             _cb ? &cb : NULL);
651
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
652 653 654 655 656 657 658 659 660 661 662 663
}

static void
rbug_set_framebuffer_state(struct pipe_context *_pipe,
                           const struct pipe_framebuffer_state *_state)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;
   struct pipe_framebuffer_state unwrapped_state;
   struct pipe_framebuffer_state *state = NULL;
   unsigned i;

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
664
   /* must protect curr status */
665
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
666

667 668
   rb_pipe->curr.nr_cbufs = 0;
   memset(rb_pipe->curr.cbufs, 0, sizeof(rb_pipe->curr.cbufs));
669
   rb_pipe->curr.zsbuf = NULL;
670

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
671 672 673
   /* unwrap the input state */
   if (_state) {
      memcpy(&unwrapped_state, _state, sizeof(unwrapped_state));
674 675 676

      rb_pipe->curr.nr_cbufs = _state->nr_cbufs;
      for(i = 0; i < _state->nr_cbufs; i++) {
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
677
         unwrapped_state.cbufs[i] = rbug_surface_unwrap(_state->cbufs[i]);
678 679 680
         if (_state->cbufs[i])
            rb_pipe->curr.cbufs[i] = rbug_resource(_state->cbufs[i]->texture);
      }
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
681
      unwrapped_state.zsbuf = rbug_surface_unwrap(_state->zsbuf);
682 683
      if (_state->zsbuf)
         rb_pipe->curr.zsbuf = rbug_resource(_state->zsbuf->texture);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
684 685 686 687 688
      state = &unwrapped_state;
   }

   pipe->set_framebuffer_state(pipe,
                               state);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
689

690
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
691 692 693 694 695 696 697 698 699
}

static void
rbug_set_polygon_stipple(struct pipe_context *_pipe,
                         const struct pipe_poly_stipple *poly_stipple)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;

700
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
701 702
   pipe->set_polygon_stipple(pipe,
                             poly_stipple);
703
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
704 705 706
}

static void
707 708 709 710
rbug_set_scissor_states(struct pipe_context *_pipe,
                        unsigned start_slot,
                        unsigned num_scissors,
                        const struct pipe_scissor_state *scissor)
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
711 712 713 714
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;

715
   mtx_lock(&rb_pipe->call_mutex);
716
   pipe->set_scissor_states(pipe, start_slot, num_scissors, scissor);
717
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
718 719 720
}

static void
721 722 723 724
rbug_set_viewport_states(struct pipe_context *_pipe,
                         unsigned start_slot,
                         unsigned num_viewports,
                         const struct pipe_viewport_state *viewport)
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
725 726 727 728
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;

729
   mtx_lock(&rb_pipe->call_mutex);
730
   pipe->set_viewport_states(pipe, start_slot, num_viewports, viewport);
731
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
732 733 734
}

static void
735
rbug_set_sampler_views(struct pipe_context *_pipe,
736
                       enum pipe_shader_type shader,
737 738 739
                       unsigned start,
                       unsigned num,
                       struct pipe_sampler_view **_views)
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
740 741 742
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;
743
   struct pipe_sampler_view *unwrapped_views[PIPE_MAX_SHADER_SAMPLER_VIEWS];
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
744 745 746
   struct pipe_sampler_view **views = NULL;
   unsigned i;

747 748
   assert(start == 0); /* XXX fix */

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
749
   /* must protect curr status */
750
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
751

752 753 754
   rb_pipe->curr.num_views[shader] = 0;
   memset(rb_pipe->curr.views[shader], 0, sizeof(rb_pipe->curr.views[shader]));
   memset(rb_pipe->curr.texs[shader], 0, sizeof(rb_pipe->curr.texs[shader]));
755 756
   memset(unwrapped_views, 0, sizeof(unwrapped_views));

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
757
   if (_views) {
758
      rb_pipe->curr.num_views[shader] = num;
759
      for (i = 0; i < num; i++) {
760 761
         rb_pipe->curr.views[shader][i] = rbug_sampler_view(_views[i]);
         rb_pipe->curr.texs[shader][i] = rbug_resource(_views[i] ? _views[i]->texture : NULL);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
762
         unwrapped_views[i] = rbug_sampler_view_unwrap(_views[i]);
763
      }
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
764 765 766
      views = unwrapped_views;
   }

767
   pipe->set_sampler_views(pipe, shader, start, num, views);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
768

769
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
770 771 772 773
}

static void
rbug_set_vertex_buffers(struct pipe_context *_pipe,
774
                        unsigned start_slot, unsigned num_buffers,
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
775 776 777 778 779 780 781 782
                        const struct pipe_vertex_buffer *_buffers)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;
   struct pipe_vertex_buffer unwrapped_buffers[PIPE_MAX_SHADER_INPUTS];
   struct pipe_vertex_buffer *buffers = NULL;
   unsigned i;

783
   mtx_lock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
784

785
   if (num_buffers && _buffers) {
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
786
      memcpy(unwrapped_buffers, _buffers, num_buffers * sizeof(*_buffers));
787 788 789 790 791
      for (i = 0; i < num_buffers; i++) {
         if (!_buffers[i].is_user_buffer)
            unwrapped_buffers[i].buffer.resource =
               rbug_resource_unwrap(_buffers[i].buffer.resource);
      }
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
792 793 794
      buffers = unwrapped_buffers;
   }

795
   pipe->set_vertex_buffers(pipe, start_slot,
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
796 797
                            num_buffers,
                            buffers);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
798

799
   mtx_unlock(&rb_pipe->call_mutex);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
800
}
801 802 803 804 805 806 807 808

static void
rbug_set_sample_mask(struct pipe_context *_pipe,
                     unsigned sample_mask)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;

809
   mtx_lock(&rb_pipe->call_mutex);
810
   pipe->set_sample_mask(pipe, sample_mask);
811
   mtx_unlock(&rb_pipe->call_mutex);
812 813
}

814 815 816 817 818 819 820 821 822 823
static struct pipe_stream_output_target *
rbug_create_stream_output_target(struct pipe_context *_pipe,
                                 struct pipe_resource *_res,
                                 unsigned buffer_offset, unsigned buffer_size)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;
   struct pipe_resource *res = rbug_resource_unwrap(_res);
   struct pipe_stream_output_target *target;

824
   mtx_lock(&rb_pipe->call_mutex);
825 826
   target = pipe->create_stream_output_target(pipe, res, buffer_offset,
                                              buffer_size);
827
   mtx_unlock(&rb_pipe->call_mutex);
828 829 830 831 832 833 834 835 836 837
   return target;
}

static void
rbug_stream_output_target_destroy(struct pipe_context *_pipe,
                                  struct pipe_stream_output_target *target)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;

838
   mtx_lock(&rb_pipe->call_mutex);
839
   pipe->stream_output_target_destroy(pipe, target);
840
   mtx_unlock(&rb_pipe->call_mutex);
841 842 843 844 845 846 847 848 849 850 851
}

static void
rbug_set_stream_output_targets(struct pipe_context *_pipe,
                               unsigned num_targets,
                               struct pipe_stream_output_target **targets,
                               const unsigned *offsets)
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
   struct pipe_context *pipe = rb_pipe->pipe;

852
   mtx_lock(&rb_pipe->call_mutex);
853
   pipe->set_stream_output_targets(pipe, num_targets, targets, offsets);
854
   mtx_unlock(&rb_pipe->call_mutex);
855 856
}

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
857
static void
858 859
rbug_resource_copy_region(struct pipe_context *_pipe,
                          struct pipe_resource *_dst,
860
                          unsigned dst_level,
861 862 863 864
                          unsigned dstx,
                          unsigned dsty,
                          unsigned dstz,
                          struct pipe_resource *_src,
865 866
                          unsigned src_level,
                          const struct pipe_box *src_box)
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
867 868
{
   struct rbug_context *rb_pipe = rbug_context(_pipe);
869 870
   struct rbug_resource *rb_resource_dst = rbug_resource(_dst);
   struct rbug_resource *rb_resource_src = rbug_resource(_src);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
871
   struct pipe_context *pipe = rb_pipe->pipe;