vrend_renderer.c 202 KB
Newer Older
Dave Airlie's avatar
Dave Airlie committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/**************************************************************************
 *
 * Copyright (C) 2014 Red Hat Inc.
 *
 * 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, sublicense,
 * 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 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 NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS 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.
 *
 **************************************************************************/
24 25 26
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Dave Airlie's avatar
Dave Airlie committed
27

28 29 30
#include <epoxy/gl.h>

#include <stdio.h>
Dave Airlie's avatar
Dave Airlie committed
31
#include <errno.h>
32 33 34 35 36 37 38 39
#include "pipe/p_shader_tokens.h"

#include "pipe/p_context.h"
#include "pipe/p_defines.h"
#include "pipe/p_screen.h"
#include "pipe/p_state.h"
#include "util/u_inlines.h"
#include "util/u_memory.h"
Dave Airlie's avatar
Dave Airlie committed
40 41
#include "util/u_dual_blend.h"

42
#include "os/os_thread.h"
43 44 45 46 47
#include "util/u_double_list.h"
#include "util/u_format.h"
#include "tgsi/tgsi_parse.h"

#include "vrend_object.h"
Dave Airlie's avatar
Dave Airlie committed
48
#include "vrend_shader.h"
49

Dave Airlie's avatar
Dave Airlie committed
50
#include "vrend_renderer.h"
51 52

#include "virgl_hw.h"
Dave Airlie's avatar
Dave Airlie committed
53

54
#include "tgsi/tgsi_text.h"
55 56 57 58 59

#ifdef HAVE_EVENTFD
#include <sys/eventfd.h>
#endif

Dave Airlie's avatar
Dave Airlie committed
60
/* debugging aid to dump shaders */
61 62
int vrend_dump_shaders;

Dave Airlie's avatar
Dave Airlie committed
63
struct vrend_if_cbs *vrend_clicbs;
64

Dave Airlie's avatar
Dave Airlie committed
65
struct vrend_fence {
66 67 68 69 70 71
   uint32_t fence_id;
   uint32_t ctx_id;
   GLsync syncobj;
   struct list_head fences;
};

Dave Airlie's avatar
Dave Airlie committed
72
struct vrend_query {
73 74
   struct list_head waiting_queries;

75
   GLuint id;
76
   GLuint type;
77
   GLuint index;
78 79
   GLuint gltype;
   int ctx_id;
Dave Airlie's avatar
Dave Airlie committed
80
   struct vrend_resource *res;
81 82 83 84 85 86 87 88
   uint64_t current_total;
};

struct global_error_state {
   enum virgl_errors last_error;
};

struct global_renderer_state {
89 90 91
   int gl_major_ver;
   int gl_minor_ver;

Dave Airlie's avatar
Dave Airlie committed
92 93
   struct vrend_context *current_ctx;
   struct vrend_context *current_hw_ctx;
94 95
   struct list_head waiting_query_list;

96 97 98
   bool inited;
   bool use_core_profile;

99 100 101 102 103 104 105 106 107
   bool have_mesa_invert;
   bool have_samplers;
   bool have_robustness;
   bool have_multisample;
   bool have_ms_scaled_blit;
   bool have_nv_prim_restart;
   bool have_gl_prim_restart;
   bool have_bit_encoding;
   bool have_vertex_attrib_binding;
108
   bool have_tf2;
109

110 111
   /* these appeared broken on at least one driver */
   bool use_explicit_locations;
Dave Airlie's avatar
Dave Airlie committed
112
   uint32_t max_uniform_blocks;
113
   struct list_head active_ctx_list;
114 115 116 117 118 119 120 121 122 123 124 125

   /* threaded sync */
   bool stop_sync_thread;
   int eventfd;

   pipe_mutex fence_mutex;
   struct list_head fence_list;
   struct list_head fence_wait_list;
   pipe_condvar fence_cond;

   pipe_thread sync_thread;
   virgl_gl_context sync_context;
126 127
};

Dave Airlie's avatar
Dave Airlie committed
128
static struct global_renderer_state vrend_state;
129

Dave Airlie's avatar
Dave Airlie committed
130
struct vrend_linked_shader_program {
131 132 133
   struct list_head head;
   struct list_head sl[PIPE_SHADER_TYPES];
   GLuint id;
134

135 136
   bool dual_src_linked;
   struct vrend_shader *ss[PIPE_SHADER_TYPES];
137

138 139
   uint32_t samplers_used_mask[PIPE_SHADER_TYPES];
   GLuint *samp_locs[PIPE_SHADER_TYPES];
140

141 142
   GLuint *shadow_samp_mask_locs[PIPE_SHADER_TYPES];
   GLuint *shadow_samp_add_locs[PIPE_SHADER_TYPES];
143

144
   GLuint *const_locs[PIPE_SHADER_TYPES];
145

146 147
   GLuint *attrib_locs;
   uint32_t shadow_samp_mask[PIPE_SHADER_TYPES];
148

149 150
   GLuint *ubo_locs[PIPE_SHADER_TYPES];
   GLuint vs_ws_adjust_loc;
Dave Airlie's avatar
Dave Airlie committed
151

152
   GLuint fs_stipple_loc;
Dave Airlie's avatar
Dave Airlie committed
153

154
   GLuint clip_locs[8];
155 156
};

Dave Airlie's avatar
Dave Airlie committed
157 158 159
struct vrend_shader {
   struct vrend_shader *next_variant;
   struct vrend_shader_selector *sel;
160 161 162 163 164

   GLchar *glsl_prog;
   GLuint id;
   GLuint compiled_fs_id;
   struct vrend_shader_key key;
165
   struct list_head programs;
166 167
};

Dave Airlie's avatar
Dave Airlie committed
168
struct vrend_shader_selector {
169 170 171 172
   struct pipe_reference reference;

   unsigned num_shaders;
   unsigned type;
173 174 175 176
   struct vrend_shader_info sinfo;

   struct vrend_shader *current;
   struct tgsi_token *tokens;
177 178 179 180

   char *tmp_buf;
   uint32_t buf_len;
   uint32_t buf_offset;
181 182
};

Dave Airlie's avatar
Dave Airlie committed
183 184
struct vrend_texture {
   struct vrend_resource base;
185 186 187 188 189 190 191 192
   struct pipe_sampler_state state;
   GLenum cur_swizzle_r;
   GLenum cur_swizzle_g;
   GLenum cur_swizzle_b;
   GLenum cur_swizzle_a;
   GLuint srgb_decode;
};

Dave Airlie's avatar
Dave Airlie committed
193
struct vrend_surface {
194 195 196 197 198
   struct pipe_reference reference;
   GLuint id;
   GLuint res_handle;
   GLuint format;
   GLuint val0, val1;
Dave Airlie's avatar
Dave Airlie committed
199
   struct vrend_resource *texture;
200 201
};

Dave Airlie's avatar
Dave Airlie committed
202 203 204
struct vrend_sampler_state {
   struct pipe_sampler_state base;
   GLuint id;
205 206
};

Dave Airlie's avatar
Dave Airlie committed
207
struct vrend_so_target {
208 209 210 211
   struct pipe_reference reference;
   GLuint res_handle;
   unsigned buffer_offset;
   unsigned buffer_size;
Dave Airlie's avatar
Dave Airlie committed
212
   struct vrend_resource *buffer;
213
   struct vrend_sub_context *sub_ctx;
214 215
};

Dave Airlie's avatar
Dave Airlie committed
216
struct vrend_sampler_view {
217 218 219 220 221 222 223 224 225 226 227 228
   struct pipe_reference reference;
   GLuint id;
   GLuint res_handle;
   GLuint format;
   GLuint val0, val1;
   GLuint gl_swizzle_r;
   GLuint gl_swizzle_g;
   GLuint gl_swizzle_b;
   GLuint gl_swizzle_a;
   GLuint cur_base, cur_max;
   GLenum depth_texture_mode;
   GLuint srgb_decode;
229 230 231 232 233
   GLuint swizzle_r:3;
   GLuint swizzle_g:3;
   GLuint swizzle_b:3;
   GLuint swizzle_a:3;
   struct vrend_resource *texture;
234 235
};

Dave Airlie's avatar
Dave Airlie committed
236
struct vrend_vertex_element {
237 238 239 240 241 242
   struct pipe_vertex_element base;
   GLenum type;
   GLboolean norm;
   GLuint nr_chan;
};

Dave Airlie's avatar
Dave Airlie committed
243
struct vrend_vertex_element_array {
244
   unsigned count;
Dave Airlie's avatar
Dave Airlie committed
245
   struct vrend_vertex_element elements[PIPE_MAX_ATTRIBS];
246
   GLuint id;
247 248
};

Dave Airlie's avatar
Dave Airlie committed
249 250
struct vrend_constants {
   unsigned int *consts;
251 252 253
   uint32_t num_consts;
};

Dave Airlie's avatar
Dave Airlie committed
254
struct vrend_shader_view {
255
   int num_views;
Dave Airlie's avatar
Dave Airlie committed
256
   struct vrend_sampler_view *views[PIPE_MAX_SHADER_SAMPLER_VIEWS];
257 258 259 260
   uint32_t res_id[PIPE_MAX_SHADER_SAMPLER_VIEWS];
   uint32_t old_ids[PIPE_MAX_SHADER_SAMPLER_VIEWS];
};

261 262 263 264 265
struct vrend_viewport {
   GLint cur_x, cur_y;
   GLsizei width, height;
   GLclampd near_val, far_val;
};
Dave Airlie's avatar
Dave Airlie committed
266

267 268 269 270 271 272 273 274 275 276
/* create a streamout object to support pause/resume */
struct vrend_streamout_object {
   GLuint id;
   uint32_t num_targets;
   uint32_t handles[16];
   struct list_head head;
   int xfb_state;
   struct vrend_so_target *so_targets[16];
};

277 278 279 280 281
#define XFB_STATE_OFF 0
#define XFB_STATE_STARTED_NEED_BEGIN 1
#define XFB_STATE_STARTED 2
#define XFB_STATE_PAUSED 3

282
struct vrend_sub_context {
Dave Airlie's avatar
Dave Airlie committed
283
   struct list_head head;
284 285 286

   virgl_gl_context gl_context;

Dave Airlie's avatar
Dave Airlie committed
287
   int sub_ctx_id;
288

Dave Airlie's avatar
Dave Airlie committed
289
   GLuint vaoid;
290 291
   uint32_t enabled_attribs_bitmask;

Dave Airlie's avatar
Dave Airlie committed
292
   struct list_head programs;
293
   struct util_hash_table *object_hash;
Dave Airlie's avatar
Dave Airlie committed
294 295

   struct vrend_vertex_element_array *ve;
296
   int num_vbos;
297
   int old_num_vbos; /* for cleaning up */
298 299
   struct pipe_vertex_buffer vbo[PIPE_MAX_ATTRIBS];
   uint32_t vbo_res_ids[PIPE_MAX_ATTRIBS];
300 301 302 303

   struct pipe_index_buffer ib;
   uint32_t index_buffer_res_id;

304
   bool vbo_dirty;
305 306 307
   bool shader_dirty;
   bool sampler_state_dirty;
   bool stencil_state_dirty;
308

309 310
   uint32_t long_shader_in_progress_handle[PIPE_SHADER_TYPES];
   struct vrend_shader_selector *shaders[PIPE_SHADER_TYPES];
Dave Airlie's avatar
Dave Airlie committed
311
   struct vrend_linked_shader_program *prog;
312

Dave Airlie's avatar
Dave Airlie committed
313
   struct vrend_shader_view views[PIPE_SHADER_TYPES];
314

Dave Airlie's avatar
Dave Airlie committed
315
   struct vrend_constants consts[PIPE_SHADER_TYPES];
316
   bool const_dirty[PIPE_SHADER_TYPES];
Dave Airlie's avatar
Dave Airlie committed
317 318 319 320
   struct vrend_sampler_state *sampler_state[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];

   struct pipe_constant_buffer cbs[PIPE_SHADER_TYPES][PIPE_MAX_CONSTANT_BUFFERS];
   uint32_t const_bufs_used_mask[PIPE_SHADER_TYPES];
321 322 323

   int num_sampler_states[PIPE_SHADER_TYPES];

Dave Airlie's avatar
Dave Airlie committed
324 325 326 327
   uint32_t fb_id;
   int nr_cbufs, old_nr_cbufs;
   struct vrend_surface *zsurf;
   struct vrend_surface *surf[8];
328

329
   struct vrend_viewport vps[PIPE_MAX_VIEWPORTS];
330 331
   float depth_transform, depth_scale;
   /* viewport is negative */
332 333
   uint32_t scissor_state_dirty;
   uint32_t viewport_state_dirty;
334

335 336
   uint32_t fb_height;

337
   struct pipe_scissor_state ss[PIPE_MAX_VIEWPORTS];
338 339 340 341 342

   struct pipe_blend_state blend_state;
   struct pipe_depth_stencil_alpha_state dsa_state;
   struct pipe_rasterizer_state rs_state;

Dave Airlie's avatar
Dave Airlie committed
343
   uint8_t stencil_refs[2];
344 345 346 347
   bool viewport_is_negative;
   /* this is set if the contents of the FBO look upside down when viewed
      with 0,0 as the bottom corner */
   bool inverted_fbo_content;
Dave Airlie's avatar
Dave Airlie committed
348 349 350 351 352 353

   GLuint blit_fb_ids[2];

   struct pipe_depth_stencil_alpha_state *dsa;

   struct pipe_clip_state ucp_state;
354

355 356 357 358 359
   bool blend_enabled;
   bool depth_test_enabled;
   bool alpha_test_enabled;
   bool stencil_test_enabled;

360
   GLuint program_id;
361 362 363

   struct pipe_rasterizer_state hw_rs_state;
   struct pipe_blend_state hw_blend_state;
364

365
   struct list_head streamout_list;
366
   struct vrend_streamout_object *current_so;
367 368

   struct pipe_blend_color blend_color;
Dave Airlie's avatar
Dave Airlie committed
369 370 371 372 373 374 375 376 377 378 379
};

struct vrend_context {
   char debug_name[64];

   struct list_head sub_ctxs;

   struct vrend_sub_context *sub;
   struct vrend_sub_context *sub0;

   int ctx_id;
380
   /* has this ctx gotten an error? */
381 382 383
   bool in_error;
   bool ctx_switch_pending;
   bool pstip_inited;
384

Dave Airlie's avatar
Dave Airlie committed
385 386
   GLuint pstipple_tex_id;

387 388 389 390
   enum virgl_ctx_errors last_error;

   /* resource bounds to this context */
   struct util_hash_table *res_hash;
391

392
   struct list_head active_nontimer_query_list;
393
   struct list_head ctx_entry;
394 395

   struct vrend_shader_cfg shader_cfg;
396 397
};

Dave Airlie's avatar
Dave Airlie committed
398 399 400 401 402
static struct vrend_resource *vrend_renderer_ctx_res_lookup(struct vrend_context *ctx, int res_handle);
static void vrend_update_viewport_state(struct vrend_context *ctx);
static void vrend_update_scissor_state(struct vrend_context *ctx);
static void vrend_destroy_query_object(void *obj_ptr);
static void vrend_finish_context_switch(struct vrend_context *ctx);
403
static void vrend_patch_blend_state(struct vrend_context *ctx);
Dave Airlie's avatar
Dave Airlie committed
404 405 406 407
static void vrend_update_frontface_state(struct vrend_context *ctx);
static void vrender_get_glsl_version(int *glsl_version);
static void vrend_destroy_resource_object(void *obj_ptr);
static void vrend_renderer_detach_res_ctx_p(struct vrend_context *ctx, int res_handle);
408
static void vrend_destroy_program(struct vrend_linked_shader_program *ent);
Dave Airlie's avatar
Dave Airlie committed
409 410 411
static void vrend_apply_sampler_state(struct vrend_context *ctx,
                                      struct vrend_resource *res,
                                      uint32_t shader_type,
412
                                      int id, int sampler_id, uint32_t srgb_decode);
Dave Airlie's avatar
Dave Airlie committed
413
void vrend_update_stencil_state(struct vrend_context *ctx);
414

Dave Airlie's avatar
Dave Airlie committed
415
static struct vrend_format_table tex_conv_table[VIRGL_FORMAT_MAX];
416

417
static inline bool vrend_format_can_sample(enum virgl_formats format)
Dave Airlie's avatar
Dave Airlie committed
418 419 420
{
   return tex_conv_table[format].bindings & VREND_BIND_SAMPLER;
}
421
static inline bool vrend_format_can_render(enum virgl_formats format)
422 423 424 425
{
   return tex_conv_table[format].bindings & VREND_BIND_RENDER;
}

426
static inline bool vrend_format_is_ds(enum virgl_formats format)
427 428 429 430
{
   return tex_conv_table[format].bindings & VREND_BIND_DEPTHSTENCIL;
}

Dave Airlie's avatar
Dave Airlie committed
431 432 433 434 435
bool vrend_is_ds_format(enum virgl_formats format)
{
   return vrend_format_is_ds(format);
}

436 437 438 439 440 441 442 443
static inline bool vrend_format_is_emulated_alpha(enum virgl_formats format)
{
   if (!vrend_state.use_core_profile)
      return false;
   return (format == VIRGL_FORMAT_A8_UNORM ||
           format == VIRGL_FORMAT_A16_UNORM);
}

Dave Airlie's avatar
Dave Airlie committed
444 445 446 447 448 449 450 451 452 453
static inline const char *pipe_shader_to_prefix(int shader_type)
{
   switch (shader_type) {
   case PIPE_SHADER_VERTEX: return "vs";
   case PIPE_SHADER_FRAGMENT: return "fs";
   case PIPE_SHADER_GEOMETRY: return "gs";
   };
   return NULL;
}

454
static const char *vrend_ctx_error_strings[] = { "None", "Unknown", "Illegal shader", "Illegal handle", "Illegal resource", "Illegal surface", "Illegal vertex format", "Illegal command buffer" };
455

Dave Airlie's avatar
Dave Airlie committed
456
static void __report_context_error(const char *fname, struct vrend_context *ctx, enum virgl_ctx_errors error, uint32_t value)
457
{
458
   ctx->in_error = true;
459 460 461 462 463
   ctx->last_error = error;
   fprintf(stderr,"%s: context error reported %d \"%s\" %s %d\n", fname, ctx->ctx_id, ctx->debug_name, vrend_ctx_error_strings[error], value);
}
#define report_context_error(ctx, error, value) __report_context_error(__func__, ctx, error, value)

Dave Airlie's avatar
Dave Airlie committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
void vrend_report_buffer_error(struct vrend_context *ctx, int cmd)
{
   report_context_error(ctx, VIRGL_ERROR_CTX_ILLEGAL_CMD_BUFFER, cmd);
}

#define CORE_PROFILE_WARN_NONE 0
#define CORE_PROFILE_WARN_STIPPLE 1
#define CORE_PROFILE_WARN_POLYGON_MODE 2
#define CORE_PROFILE_WARN_TWO_SIDE 3
#define CORE_PROFILE_WARN_CLAMP 4
#define CORE_PROFILE_WARN_SHADE_MODEL 5

static const char *vrend_core_profile_warn_strings[] = { "None", "Stipple", "Polygon Mode", "Two Side", "Clamping", "Shade Model" };

static void __report_core_warn(const char *fname, struct vrend_context *ctx, enum virgl_ctx_errors error, uint32_t value)
{
   fprintf(stderr,"%s: core profile violation reported %d \"%s\" %s %d\n", fname, ctx->ctx_id, ctx->debug_name, vrend_core_profile_warn_strings[error], value);
}
#define report_core_warn(ctx, error, value) __report_core_warn(__func__, ctx, error, value)
483
static inline bool should_invert_viewport(struct vrend_context *ctx)
484 485 486 487
{
   /* if we have a negative viewport then gallium wanted to invert it,
      however since we are rendering to GL FBOs we need to invert it
      again unless we are rendering upside down already
488
      - confused?
489
      so if gallium asks for a negative viewport */
Dave Airlie's avatar
Dave Airlie committed
490
   return !(ctx->sub->viewport_is_negative ^ ctx->sub->inverted_fbo_content);
491 492
}

Dave Airlie's avatar
Dave Airlie committed
493
static void vrend_destroy_surface(struct vrend_surface *surf)
494
{
Dave Airlie's avatar
Dave Airlie committed
495
   vrend_resource_reference(&surf->texture, NULL);
496 497 498
   free(surf);
}

499
static inline void
Dave Airlie's avatar
Dave Airlie committed
500
vrend_surface_reference(struct vrend_surface **ptr, struct vrend_surface *surf)
501
{
Dave Airlie's avatar
Dave Airlie committed
502
   struct vrend_surface *old_surf = *ptr;
503 504

   if (pipe_reference(&(*ptr)->reference, &surf->reference))
Dave Airlie's avatar
Dave Airlie committed
505
      vrend_destroy_surface(old_surf);
506 507 508
   *ptr = surf;
}

Dave Airlie's avatar
Dave Airlie committed
509
static void vrend_destroy_sampler_view(struct vrend_sampler_view *samp)
510
{
Dave Airlie's avatar
Dave Airlie committed
511
   vrend_resource_reference(&samp->texture, NULL);
512 513 514
   free(samp);
}

515
static inline void
Dave Airlie's avatar
Dave Airlie committed
516
vrend_sampler_view_reference(struct vrend_sampler_view **ptr, struct vrend_sampler_view *view)
517
{
Dave Airlie's avatar
Dave Airlie committed
518
   struct vrend_sampler_view *old_view = *ptr;
519 520

   if (pipe_reference(&(*ptr)->reference, &view->reference))
Dave Airlie's avatar
Dave Airlie committed
521
      vrend_destroy_sampler_view(old_view);
522 523 524
   *ptr = view;
}

Dave Airlie's avatar
Dave Airlie committed
525
static void vrend_destroy_so_target(struct vrend_so_target *target)
526
{
Dave Airlie's avatar
Dave Airlie committed
527
   vrend_resource_reference(&target->buffer, NULL);
528 529 530
   free(target);
}

531
static inline void
Dave Airlie's avatar
Dave Airlie committed
532
vrend_so_target_reference(struct vrend_so_target **ptr, struct vrend_so_target *target)
533
{
Dave Airlie's avatar
Dave Airlie committed
534
   struct vrend_so_target *old_target = *ptr;
535 536

   if (pipe_reference(&(*ptr)->reference, &target->reference))
Dave Airlie's avatar
Dave Airlie committed
537
      vrend_destroy_so_target(old_target);
538 539 540
   *ptr = target;
}

Dave Airlie's avatar
Dave Airlie committed
541
static void vrend_shader_destroy(struct vrend_shader *shader)
542
{
543 544 545
   struct vrend_linked_shader_program *ent, *tmp;

   LIST_FOR_EACH_ENTRY_SAFE(ent, tmp, &shader->programs, sl[shader->sel->type]) {
546
      vrend_destroy_program(ent);
547 548
   }

549 550 551 552 553
   glDeleteShader(shader->id);
   free(shader->glsl_prog);
   free(shader);
}

Dave Airlie's avatar
Dave Airlie committed
554
static void vrend_destroy_shader_selector(struct vrend_shader_selector *sel)
555
{
Dave Airlie's avatar
Dave Airlie committed
556 557
   struct vrend_shader *p = sel->current, *c;
   int i;
558 559
   while (p) {
      c = p->next_variant;
Dave Airlie's avatar
Dave Airlie committed
560
      vrend_shader_destroy(p);
561 562
      p = c;
   }
563 564 565
   if (sel->sinfo.so_names)
      for (i = 0; i < sel->sinfo.so_info.num_outputs; i++)
         free(sel->sinfo.so_names[i]);
566
   free(sel->tmp_buf);
Dave Airlie's avatar
Dave Airlie committed
567
   free(sel->sinfo.so_names);
568 569 570 571 572
   free(sel->sinfo.interpinfo);
   free(sel->tokens);
   free(sel);
}

573
static bool vrend_compile_shader(struct vrend_context *ctx,
574
                                 struct vrend_shader *shader)
575 576 577 578 579 580 581 582 583 584 585 586
{
   GLint param;
   glShaderSource(shader->id, 1, (const char **)&shader->glsl_prog, NULL);
   glCompileShader(shader->id);
   glGetShaderiv(shader->id, GL_COMPILE_STATUS, &param);
   if (param == GL_FALSE) {
      char infolog[65536];
      int len;
      glGetShaderInfoLog(shader->id, 65536, &len, infolog);
      report_context_error(ctx, VIRGL_ERROR_CTX_ILLEGAL_SHADER, 0);
      fprintf(stderr,"shader failed to compile\n%s\n", infolog);
      fprintf(stderr,"GLSL:\n%s\n", shader->glsl_prog);
587
      return false;
588
   }
589
   return true;
590 591
}

592
static inline void
Dave Airlie's avatar
Dave Airlie committed
593
vrend_shader_state_reference(struct vrend_shader_selector **ptr, struct vrend_shader_selector *shader)
594
{
Dave Airlie's avatar
Dave Airlie committed
595
   struct vrend_shader_selector *old_shader = *ptr;
596 597

   if (pipe_reference(&(*ptr)->reference, &shader->reference))
Dave Airlie's avatar
Dave Airlie committed
598
      vrend_destroy_shader_selector(old_shader);
599 600 601 602
   *ptr = shader;
}

void
Dave Airlie's avatar
Dave Airlie committed
603
vrend_insert_format(struct vrend_format_table *entry, uint32_t bindings)
604 605 606 607 608
{
   tex_conv_table[entry->format] = *entry;
   tex_conv_table[entry->format].bindings = bindings;
}

Dave Airlie's avatar
Dave Airlie committed
609 610 611
void
vrend_insert_format_swizzle(int override_format, struct vrend_format_table *entry, uint32_t bindings, uint8_t swizzle[4])
{
612
   int i;
Dave Airlie's avatar
Dave Airlie committed
613 614 615 616
   tex_conv_table[override_format] = *entry;
   tex_conv_table[override_format].bindings = bindings;
   tex_conv_table[override_format].flags = VREND_BIND_NEED_SWIZZLE;
   for (i = 0; i < 4; i++)
617
      tex_conv_table[override_format].swizzle[i] = swizzle[i];
Dave Airlie's avatar
Dave Airlie committed
618 619
}

620
static bool vrend_is_timer_query(GLenum gltype)
621
{
622 623
   return gltype == GL_TIMESTAMP ||
      gltype == GL_TIME_ELAPSED;
624 625
}

626
static void vrend_use_program(struct vrend_context *ctx, GLuint program_id)
627
{
628
   if (ctx->sub->program_id != program_id) {
629
      glUseProgram(program_id);
630
      ctx->sub->program_id = program_id;
631 632 633
   }
}

Dave Airlie's avatar
Dave Airlie committed
634 635 636 637 638 639 640 641 642 643 644 645 646
static void vrend_init_pstipple_texture(struct vrend_context *ctx)
{
   glGenTextures(1, &ctx->pstipple_tex_id);
   glBindTexture(GL_TEXTURE_2D, ctx->pstipple_tex_id);
   glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 32, 32, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

   ctx->pstip_inited = true;
}

647
static void vrend_blend_enable(struct vrend_context *ctx, bool blend_enable)
648
{
649 650
   if (ctx->sub->blend_enabled != blend_enable) {
      ctx->sub->blend_enabled = blend_enable;
651 652 653 654 655 656 657
      if (blend_enable)
         glEnable(GL_BLEND);
      else
         glDisable(GL_BLEND);
   }
}

658
static void vrend_depth_test_enable(struct vrend_context *ctx, bool depth_test_enable)
659
{
660 661
   if (ctx->sub->depth_test_enabled != depth_test_enable) {
      ctx->sub->depth_test_enabled = depth_test_enable;
662 663 664 665 666 667 668
      if (depth_test_enable)
         glEnable(GL_DEPTH_TEST);
      else
         glDisable(GL_DEPTH_TEST);
   }
}

669
static void vrend_alpha_test_enable(struct vrend_context *ctx, bool alpha_test_enable)
670
{
671
   if (vrend_state.use_core_profile) {
672 673
      /* handled in shaders */
      return;
Dave Airlie's avatar
Dave Airlie committed
674
   }
675 676
   if (ctx->sub->alpha_test_enabled != alpha_test_enable) {
      ctx->sub->alpha_test_enabled = alpha_test_enable;
677 678 679 680 681 682
      if (alpha_test_enable)
         glEnable(GL_ALPHA_TEST);
      else
         glDisable(GL_ALPHA_TEST);
   }
}
Dave Airlie's avatar
Dave Airlie committed
683

684
static void vrend_stencil_test_enable(struct vrend_context *ctx, bool stencil_test_enable)
685
{
686 687
   if (ctx->sub->stencil_test_enabled != stencil_test_enable) {
      ctx->sub->stencil_test_enabled = stencil_test_enable;
688 689 690 691 692 693 694
      if (stencil_test_enable)
         glEnable(GL_STENCIL_TEST);
      else
         glDisable(GL_STENCIL_TEST);
   }
}

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
static void dump_stream_out(struct pipe_stream_output_info *so)
{
   int i;
   if (!so)
      return;
   printf("streamout: %d\n", so->num_outputs);
   printf("strides: ");
   for (i = 0; i < 4; i++)
      printf("%d ", so->stride[i]);
   printf("\n");
   printf("outputs:\n");
   for (i = 0; i < so->num_outputs; i++) {
      printf("\t%d: reg: %d sc: %d, nc: %d ob: %d do: %d\n",
             i,
             so->output[i].register_index,
             so->output[i].start_component,
             so->output[i].num_components,
             so->output[i].output_buffer,
             so->output[i].dst_offset);
   }
}

static char *get_skip_str(int *skip_val)
{
   char *start_skip = NULL;
   if (*skip_val < 0) {
      *skip_val = 0;
      return NULL;
   }

   if (*skip_val == 1) {
      start_skip = strdup("gl_SkipComponents1");
      *skip_val -= 1;
   } else if (*skip_val == 2) {
      start_skip = strdup("gl_SkipComponents2");
      *skip_val -= 2;
   } else if (*skip_val == 3) {
      start_skip = strdup("gl_SkipComponents3");
      *skip_val -= 3;
   } else if (*skip_val >= 4) {
      start_skip = strdup("gl_SkipComponents4");
      *skip_val -= 4;
   }
   return start_skip;
}

Dave Airlie's avatar
Dave Airlie committed
741
static void set_stream_out_varyings(int prog_id, struct vrend_shader_info *sinfo)
742
{
Dave Airlie's avatar
Dave Airlie committed
743
   struct pipe_stream_output_info *so = &sinfo->so_info;
744
   char *varyings[PIPE_MAX_SHADER_OUTPUTS*2];
745
   int i, j;
746
   int n_outputs = 0;
747
   int last_buffer = 0;
748 749 750
   char *start_skip;
   int buf_offset = 0;
   int skip;
Dave Airlie's avatar
Dave Airlie committed
751
   if (!so->num_outputs)
752 753
      return;

754 755 756
   if (vrend_dump_shaders)
      dump_stream_out(so);

Dave Airlie's avatar
Dave Airlie committed
757
   for (i = 0; i < so->num_outputs; i++) {
758
      if (last_buffer != so->output[i].output_buffer) {
759 760 761 762 763 764 765 766 767

         skip = so->stride[last_buffer] - buf_offset;
         while (skip) {
            start_skip = get_skip_str(&skip);
            if (start_skip)
               varyings[n_outputs++] = start_skip;
         }
         for (j = last_buffer; j < so->output[i].output_buffer; j++)
            varyings[n_outputs++] = strdup("gl_NextBuffer");
Dave Airlie's avatar
Dave Airlie committed
768
         last_buffer = so->output[i].output_buffer;
769 770 771 772 773 774 775 776
         buf_offset = 0;
      }

      skip = so->output[i].dst_offset - buf_offset;
      while (skip) {
         start_skip = get_skip_str(&skip);
         if (start_skip)
            varyings[n_outputs++] = start_skip;
777
      }
778 779 780
      buf_offset = so->output[i].dst_offset;

      buf_offset += so->output[i].num_components;
781
      if (sinfo->so_names[i])
Dave Airlie's avatar
Dave Airlie committed
782
         varyings[n_outputs++] = strdup(sinfo->so_names[i]);
783 784
   }

785 786 787 788 789 790 791
   skip = so->stride[last_buffer] - buf_offset;
   while (skip) {
      start_skip = get_skip_str(&skip);
      if (start_skip)
         varyings[n_outputs++] = start_skip;
   }

792
   glTransformFeedbackVaryings(prog_id, n_outputs,
793 794
                               (const GLchar **)varyings, GL_INTERLEAVED_ATTRIBS_EXT);

795
   for (i = 0; i < n_outputs; i++)
796 797 798 799
      if (varyings[i])
         free(varyings[i]);
}

Dave Airlie's avatar
Dave Airlie committed
800 801 802 803
static struct vrend_linked_shader_program *add_shader_program(struct vrend_context *ctx,
                                                              struct vrend_shader *vs,
                                                              struct vrend_shader *fs,
                                                              struct vrend_shader *gs)
804
{
Dave Airlie's avatar
Dave Airlie committed
805
   struct vrend_linked_shader_program *sprog = CALLOC_STRUCT(vrend_linked_shader_program);
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
   char name[16];
   int i;
   GLuint prog_id;
   GLint lret;
   int id;

   if (!sprog)
      return NULL;

   /* need to rewrite VS code to add interpolation params */
   if ((gs && gs->compiled_fs_id != fs->id) ||
       (!gs && vs->compiled_fs_id != fs->id)) {
      bool ret;

      if (gs)
         vrend_patch_vertex_shader_interpolants(gs->glsl_prog,
                                                &gs->sel->sinfo,
                                                &fs->sel->sinfo, true, fs->key.flatshade);
      else
         vrend_patch_vertex_shader_interpolants(vs->glsl_prog,
                                                &vs->sel->sinfo,
                                                &fs->sel->sinfo, false, fs->key.flatshade);
      ret = vrend_compile_shader(ctx, gs ? gs : vs);
      if (ret == false) {
         glDeleteShader(gs ? gs->id : vs->id);
         free(sprog);
         return NULL;
      }
      if (gs)
         gs->compiled_fs_id = fs->id;
      else
         vs->compiled_fs_id = fs->id;
   }

   prog_id = glCreateProgram();
   glAttachShader(prog_id, vs->id);
   if (gs) {
      if (gs->id > 0)
         glAttachShader(prog_id, gs->id);
      set_stream_out_varyings(prog_id, &gs->sel->sinfo);
   }
   else
      set_stream_out_varyings(prog_id, &vs->sel->sinfo);
   glAttachShader(prog_id, fs->id);

   if (fs->sel->sinfo.num_outputs > 1) {
      if (util_blend_state_is_dual(&ctx->sub->blend_state, 0)) {
         glBindFragDataLocationIndexed(prog_id, 0, 0, "fsout_c0");
         glBindFragDataLocationIndexed(prog_id, 0, 1, "fsout_c1");
         sprog->dual_src_linked = true;
      } else {
         glBindFragDataLocationIndexed(prog_id, 0, 0, "fsout_c0");
         glBindFragDataLocationIndexed(prog_id, 1, 0, "fsout_c1");
         sprog->dual_src_linked = false;
      }
   } else
      sprog->dual_src_linked = false;

   if (vrend_state.have_vertex_attrib_binding) {
      for (i = 0; i < vs->sel->sinfo.num_inputs; i++) {
         snprintf(name, 10, "in_%d", i);
         glBindAttribLocation(prog_id, i, name);
      }
   }

   glLinkProgram(prog_id);

   glGetProgramiv(prog_id, GL_LINK_STATUS, &lret);
   if (lret == GL_FALSE) {
      char infolog[65536];
      int len;
      glGetProgramInfoLog(prog_id, 65536, &len, infolog);
      fprintf(stderr,"got error linking\n%s\n", infolog);
      /* dump shaders */
      report_context_error(ctx, VIRGL_ERROR_CTX_ILLEGAL_SHADER, 0);
      fprintf(stderr,"vert shader: %d GLSL\n%s\n", vs->id, vs->glsl_prog);
      if (gs)
         fprintf(stderr,"geom shader: %d GLSL\n%s\n", gs->id, gs->glsl_prog);
      fprintf(stderr,"frag shader: %d GLSL\n%s\n", fs->id, fs->glsl_prog);
      glDeleteProgram(prog_id);
      return NULL;
   }

   sprog->ss[PIPE_SHADER_VERTEX] = vs;
   sprog->ss[PIPE_SHADER_FRAGMENT] = fs;
   sprog->ss[PIPE_SHADER_GEOMETRY] = gs;

   list_add(&sprog->sl[PIPE_SHADER_VERTEX], &vs->programs);
   list_add(&sprog->sl[PIPE_SHADER_FRAGMENT], &fs->programs);
   if (gs)
896 897
      list_add(&sprog->sl[PIPE_SHADER_GEOMETRY], &gs->programs);

898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
   sprog->id = prog_id;

   list_add(&sprog->head, &ctx->sub->programs);

   if (fs->key.pstipple_tex)
      sprog->fs_stipple_loc = glGetUniformLocation(prog_id, "pstipple_sampler");
   else
      sprog->fs_stipple_loc = -1;
   sprog->vs_ws_adjust_loc = glGetUniformLocation(prog_id, "winsys_adjust");
   for (id = PIPE_SHADER_VERTEX; id <= (gs ? PIPE_SHADER_GEOMETRY : PIPE_SHADER_FRAGMENT); id++) {
      if (sprog->ss[id]->sel->sinfo.samplers_used_mask) {
         uint32_t mask = sprog->ss[id]->sel->sinfo.samplers_used_mask;
         int nsamp = util_bitcount(sprog->ss[id]->sel->sinfo.samplers_used_mask);
         int index;
         sprog->shadow_samp_mask[id] = sprog->ss[id]->sel->sinfo.shadow_samp_mask;
         if (sprog->ss[id]->sel->sinfo.shadow_samp_mask) {
            sprog->shadow_samp_mask_locs[id] = calloc(nsamp, sizeof(uint32_t));
            sprog->shadow_samp_add_locs[id] = calloc(nsamp, sizeof(uint32_t));
         } else {
            sprog->shadow_samp_mask_locs[id] = sprog->shadow_samp_add_locs[id] = NULL;
         }
         sprog->samp_locs[id] = calloc(nsamp, sizeof(uint32_t));
         if (sprog->samp_locs[id]) {
            const char *prefix = pipe_shader_to_prefix(id);
            index = 0;
            while(mask) {
               i = u_bit_scan(&mask);
               snprintf(name, 10, "%ssamp%d", prefix, i);
               sprog->samp_locs[id][index] = glGetUniformLocation(prog_id, name);
               if (sprog->ss[id]->sel->sinfo.shadow_samp_mask & (1 << i)) {
                  snprintf(name, 14, "%sshadmask%d", prefix, i);
                  sprog->shadow_samp_mask_locs[id][index] = glGetUniformLocation(prog_id, name);
                  snprintf(name, 14, "%sshadadd%d", prefix, i);
                  sprog->shadow_samp_add_locs[id][index] = glGetUniformLocation(prog_id, name);
               }
               index++;
            }
         }
      } else {
         sprog->samp_locs[id] = NULL;
         sprog->shadow_samp_mask_locs[id] = NULL;
         sprog->shadow_samp_add_locs[id] = NULL;
         sprog->shadow_samp_mask[id] = 0;
941
      }
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
      sprog->samplers_used_mask[id] = sprog->ss[id]->sel->sinfo.samplers_used_mask;
   }

   for (id = PIPE_SHADER_VERTEX; id <= (gs ? PIPE_SHADER_GEOMETRY : PIPE_SHADER_FRAGMENT); id++) {
      if (sprog->ss[id]->sel->sinfo.num_consts) {
         sprog->const_locs[id] = calloc(sprog->ss[id]->sel->sinfo.num_consts, sizeof(uint32_t));
         if (sprog->const_locs[id]) {
            const char *prefix = pipe_shader_to_prefix(id);
            for (i = 0; i < sprog->ss[id]->sel->sinfo.num_consts; i++) {
               snprintf(name, 16, "%sconst0[%d]", prefix, i);
               sprog->const_locs[id][i] = glGetUniformLocation(prog_id, name);
            }
         }
      } else
         sprog->const_locs[id] = NULL;
   }

   if (!vrend_state.have_vertex_attrib_binding) {
      if (vs->sel->sinfo.num_inputs) {
         sprog->attrib_locs = calloc(vs->sel->sinfo.num_inputs, sizeof(uint32_t));
         if (sprog->attrib_locs) {
            for (i = 0; i < vs->sel->sinfo.num_inputs; i++) {
               snprintf(name, 10, "in_%d", i);
               sprog->attrib_locs[i] = glGetAttribLocation(prog_id, name);
            }
         }
      } else
         sprog->attrib_locs = NULL;
   }

   for (id = PIPE_SHADER_VERTEX; id <= (gs ? PIPE_SHADER_GEOMETRY : PIPE_SHADER_FRAGMENT); id++) {
      if (sprog->ss[id]->sel->sinfo.num_ubos) {
         const char *prefix = pipe_shader_to_prefix(id);

         sprog->ubo_locs[id] = calloc(sprog->ss[id]->sel->sinfo.num_ubos, sizeof(uint32_t));
         for (i = 0; i < sprog->ss[id]->sel->sinfo.num_ubos; i++) {
            snprintf(name, 16, "%subo%d", prefix, i + 1);
            sprog->ubo_locs[id][i] = glGetUniformBlockIndex(prog_id, name);
         }
      } else
         sprog->ubo_locs[id] = NULL;
   }

   if (vs->sel->sinfo.num_ucp) {
      for (i = 0; i < vs->sel->sinfo.num_ucp; i++) {
         snprintf(name, 10, "clipp[%d]", i);
         sprog->clip_locs[i] = glGetUniformLocation(prog_id, name);
      }
   }
   return sprog;
992 993
}

Dave Airlie's avatar
Dave Airlie committed
994
static struct vrend_linked_shader_program *lookup_shader_program(struct vrend_context *ctx,
995
                                                                 GLuint vs_id, GLuint fs_id, GLuint gs_id, bool dual_src)
996
{
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
   struct vrend_linked_shader_program *ent;
   LIST_FOR_EACH_ENTRY(ent, &ctx->sub->programs, head) {
      if (ent->dual_src_linked != dual_src)
         continue;
      if (ent->ss[PIPE_SHADER_VERTEX]->id == vs_id && ent->ss[PIPE_SHADER_FRAGMENT]->id == fs_id) {
         if (!ent->ss[PIPE_SHADER_GEOMETRY] && gs_id == 0)
            return ent;
         if (ent->ss[PIPE_SHADER_GEOMETRY] && ent->ss[PIPE_SHADER_GEOMETRY]->id == gs_id)
            return ent;
      }
   }
   return NULL;
1009 1010
}

1011 1012
static void vrend_destroy_program(struct vrend_linked_shader_program *ent)
{
1013 1014 1015
   int i;
   glDeleteProgram(ent->id);
   list_del(&ent->head);
1016

1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
   for (i = PIPE_SHADER_VERTEX; i <= PIPE_SHADER_GEOMETRY; i++) {
      if (ent->ss[i])
         list_del(&ent->sl[i]);
      free(ent->shadow_samp_mask_locs[i]);
      free(ent->shadow_samp_add_locs[i]);
      free(ent->samp_locs[i]);
      free(ent->const_locs[i]);
      free(ent->ubo_locs[i]);
   }
   free(ent->attrib_locs);
   free(ent);
1028 1029
}

Dave Airlie's avatar
Dave Airlie committed
1030
static void vrend_free_programs(struct vrend_sub_context *sub)
1031
{
Dave Airlie's avatar
Dave Airlie committed
1032
   struct vrend_linked_shader_program *ent, *tmp;
1033

Dave Airlie's avatar
Dave Airlie committed
1034
   if (LIST_IS_EMPTY(&sub->programs))
1035
      return;
Dave Airlie's avatar
Dave Airlie committed
1036 1037

   LIST_FOR_EACH_ENTRY_SAFE(ent, tmp, &sub->programs, head) {
1038
      vrend_destroy_program(ent);
1039
   }
1040
}
1041

1042 1043 1044 1045 1046 1047
static void vrend_destroy_streamout_object(struct vrend_streamout_object *obj)
{
   int i;
   list_del(&obj->head);
   for (i = 0; i < obj->num_targets; i++)
      vrend_so_target_reference(&obj->so_targets[i], NULL);
1048 1049
   if (vrend_state.have_tf2)
      glDeleteTransformFeedbacks(1, &obj->id);
1050 1051 1052
   FREE(obj);
}

Dave Airlie's avatar
Dave Airlie committed
1053 1054 1055 1056
int vrend_create_surface(struct vrend_context *ctx,
                         uint32_t handle,
                         uint32_t res_handle, uint32_t format,
                         uint32_t val0, uint32_t val1)
1057
{
Dave Airlie's avatar
Dave Airlie committed
1058 1059 1060
   struct vrend_surface *surf;
   struct vrend_resource *res;
   uint32_t ret_handle;
1061

1062 1063 1064 1065
   if (format >= PIPE_FORMAT_COUNT) {
      return EINVAL;
   }

Dave Airlie's avatar
Dave Airlie committed
1066
   res = vrend_renderer_ctx_res_lookup(