Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

draw_pipe_aaline.c 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /**************************************************************************
  2. *
  3. * Copyright 2007 VMware, Inc.
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  21. * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
  22. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. /**
  28. * AA line stage: AA lines are converted to texture mapped triangles.
  29. *
  30. * Authors: Brian Paul
  31. */
  32. #include "pipe/p_context.h"
  33. #include "pipe/p_defines.h"
  34. #include "pipe/p_shader_tokens.h"
  35. #include "util/u_inlines.h"
  36. #include "util/u_format.h"
  37. #include "util/u_math.h"
  38. #include "util/u_memory.h"
  39. #include "util/u_sampler.h"
  40. #include "tgsi/tgsi_transform.h"
  41. #include "tgsi/tgsi_dump.h"
  42. #include "draw_context.h"
  43. #include "draw_private.h"
  44. #include "draw_pipe.h"
  45. /** Approx number of new tokens for instructions in aa_transform_inst() */
  46. #define NUM_NEW_TOKENS 53
  47. /**
  48. * Size for the alpha texture used for antialiasing
  49. */
  50. #define TEXTURE_SIZE_LOG2 5 /* 32 x 32 */
  51. /**
  52. * Max texture level for the alpha texture used for antialiasing
  53. *
  54. * Don't use the 1x1 and 2x2 mipmap levels.
  55. */
  56. #define MAX_TEXTURE_LEVEL (TEXTURE_SIZE_LOG2 - 2)
  57. /**
  58. * Subclass of pipe_shader_state to carry extra fragment shader info.
  59. */
  60. struct aaline_fragment_shader
  61. {
  62. struct pipe_shader_state state;
  63. void *driver_fs;
  64. void *aaline_fs;
  65. uint sampler_unit;
  66. int generic_attrib; /**< texcoord/generic used for texture */
  67. };
  68. /**
  69. * Subclass of draw_stage
  70. */
  71. struct aaline_stage
  72. {
  73. struct draw_stage stage;
  74. float half_line_width;
  75. /** For AA lines, this is the vertex attrib slot for the new texcoords */
  76. uint tex_slot;
  77. /** position, not necessarily output zero */
  78. uint pos_slot;
  79. void *sampler_cso;
  80. struct pipe_resource *texture;
  81. struct pipe_sampler_view *sampler_view;
  82. uint num_samplers;
  83. uint num_sampler_views;
  84. /*
  85. * Currently bound state
  86. */
  87. struct aaline_fragment_shader *fs;
  88. struct {
  89. void *sampler[PIPE_MAX_SAMPLERS];
  90. struct pipe_sampler_view *sampler_views[PIPE_MAX_SHADER_SAMPLER_VIEWS];
  91. } state;
  92. /*
  93. * Driver interface/override functions
  94. */
  95. void * (*driver_create_fs_state)(struct pipe_context *,
  96. const struct pipe_shader_state *);
  97. void (*driver_bind_fs_state)(struct pipe_context *, void *);
  98. void (*driver_delete_fs_state)(struct pipe_context *, void *);
  99. void (*driver_bind_sampler_states)(struct pipe_context *,
  100. enum pipe_shader_type, unsigned,
  101. unsigned, void **);
  102. void (*driver_set_sampler_views)(struct pipe_context *,
  103. enum pipe_shader_type shader,
  104. unsigned start, unsigned count,
  105. struct pipe_sampler_view **);
  106. };
  107. /**
  108. * Subclass of tgsi_transform_context, used for transforming the
  109. * user's fragment shader to add the special AA instructions.
  110. */
  111. struct aa_transform_context {
  112. struct tgsi_transform_context base;
  113. uint tempsUsed; /**< bitmask */
  114. int colorOutput; /**< which output is the primary color */
  115. uint samplersUsed; /**< bitfield of samplers used */
  116. bool hasSview;
  117. int freeSampler; /** an available sampler for the pstipple */
  118. int maxInput, maxGeneric; /**< max input index found */
  119. int colorTemp, texTemp; /**< temp registers */
  120. };
  121. /**
  122. * TGSI declaration transform callback.
  123. * Look for a free sampler, a free input attrib, and two free temp regs.
  124. */
  125. static void
  126. aa_transform_decl(struct tgsi_transform_context *ctx,
  127. struct tgsi_full_declaration *decl)
  128. {
  129. struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
  130. if (decl->Declaration.File == TGSI_FILE_OUTPUT &&
  131. decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
  132. decl->Semantic.Index == 0) {
  133. aactx->colorOutput = decl->Range.First;
  134. }
  135. else if (decl->Declaration.File == TGSI_FILE_SAMPLER) {
  136. uint i;
  137. for (i = decl->Range.First;
  138. i <= decl->Range.Last; i++) {
  139. aactx->samplersUsed |= 1u << i;
  140. }
  141. }
  142. else if (decl->Declaration.File == TGSI_FILE_SAMPLER_VIEW) {
  143. aactx->hasSview = true;
  144. }
  145. else if (decl->Declaration.File == TGSI_FILE_INPUT) {
  146. if ((int) decl->Range.Last > aactx->maxInput)
  147. aactx->maxInput = decl->Range.Last;
  148. if (decl->Semantic.Name == TGSI_SEMANTIC_GENERIC &&
  149. (int) decl->Semantic.Index > aactx->maxGeneric) {
  150. aactx->maxGeneric = decl->Semantic.Index;
  151. }
  152. }
  153. else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) {
  154. uint i;
  155. for (i = decl->Range.First;
  156. i <= decl->Range.Last; i++) {
  157. aactx->tempsUsed |= (1 << i);
  158. }
  159. }
  160. ctx->emit_declaration(ctx, decl);
  161. }
  162. /**
  163. * Find the lowest zero bit in the given word, or -1 if bitfield is all ones.
  164. */
  165. static int
  166. free_bit(uint bitfield)
  167. {
  168. return ffs(~bitfield) - 1;
  169. }
  170. /**
  171. * TGSI transform prolog callback.
  172. */
  173. static void
  174. aa_transform_prolog(struct tgsi_transform_context *ctx)
  175. {
  176. struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
  177. uint i;
  178. STATIC_ASSERT(sizeof(aactx->samplersUsed) * 8 >= PIPE_MAX_SAMPLERS);
  179. /* find free sampler */
  180. aactx->freeSampler = free_bit(aactx->samplersUsed);
  181. if (aactx->freeSampler < 0 || aactx->freeSampler >= PIPE_MAX_SAMPLERS)
  182. aactx->freeSampler = PIPE_MAX_SAMPLERS - 1;
  183. /* find two free temp regs */
  184. for (i = 0; i < 32; i++) {
  185. if ((aactx->tempsUsed & (1 << i)) == 0) {
  186. /* found a free temp */
  187. if (aactx->colorTemp < 0)
  188. aactx->colorTemp = i;
  189. else if (aactx->texTemp < 0)
  190. aactx->texTemp = i;
  191. else
  192. break;
  193. }
  194. }
  195. assert(aactx->colorTemp >= 0);
  196. assert(aactx->texTemp >= 0);
  197. /* declare new generic input/texcoord */
  198. tgsi_transform_input_decl(ctx, aactx->maxInput + 1,
  199. TGSI_SEMANTIC_GENERIC, aactx->maxGeneric + 1,
  200. TGSI_INTERPOLATE_LINEAR);
  201. /* declare new sampler */
  202. tgsi_transform_sampler_decl(ctx, aactx->freeSampler);
  203. /* if the src shader has SVIEW decl's for each SAMP decl, we
  204. * need to continue the trend and ensure there is a matching
  205. * SVIEW for the new SAMP we just created
  206. */
  207. if (aactx->hasSview) {
  208. tgsi_transform_sampler_view_decl(ctx,
  209. aactx->freeSampler,
  210. TGSI_TEXTURE_2D,
  211. TGSI_RETURN_TYPE_FLOAT);
  212. }
  213. /* declare new temp regs */
  214. tgsi_transform_temp_decl(ctx, aactx->texTemp);
  215. tgsi_transform_temp_decl(ctx, aactx->colorTemp);
  216. }
  217. /**
  218. * TGSI transform epilog callback.
  219. */
  220. static void
  221. aa_transform_epilog(struct tgsi_transform_context *ctx)
  222. {
  223. struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
  224. if (aactx->colorOutput != -1) {
  225. /* insert texture sampling code for antialiasing. */
  226. /* TEX texTemp, input_coord, sampler, 2D */
  227. tgsi_transform_tex_inst(ctx,
  228. TGSI_FILE_TEMPORARY, aactx->texTemp,
  229. TGSI_FILE_INPUT, aactx->maxInput + 1,
  230. TGSI_TEXTURE_2D, aactx->freeSampler);
  231. /* MOV rgb */
  232. tgsi_transform_op1_inst(ctx, TGSI_OPCODE_MOV,
  233. TGSI_FILE_OUTPUT, aactx->colorOutput,
  234. TGSI_WRITEMASK_XYZ,
  235. TGSI_FILE_TEMPORARY, aactx->colorTemp);
  236. /* MUL alpha */
  237. tgsi_transform_op2_inst(ctx, TGSI_OPCODE_MUL,
  238. TGSI_FILE_OUTPUT, aactx->colorOutput,
  239. TGSI_WRITEMASK_W,
  240. TGSI_FILE_TEMPORARY, aactx->colorTemp,
  241. TGSI_FILE_TEMPORARY, aactx->texTemp);
  242. }
  243. }
  244. /**
  245. * TGSI instruction transform callback.
  246. * Replace writes to result.color w/ a temp reg.
  247. */
  248. static void
  249. aa_transform_inst(struct tgsi_transform_context *ctx,
  250. struct tgsi_full_instruction *inst)
  251. {
  252. struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
  253. uint i;
  254. /*
  255. * Look for writes to result.color and replace with colorTemp reg.
  256. */
  257. for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
  258. struct tgsi_full_dst_register *dst = &inst->Dst[i];
  259. if (dst->Register.File == TGSI_FILE_OUTPUT &&
  260. dst->Register.Index == aactx->colorOutput) {
  261. dst->Register.File = TGSI_FILE_TEMPORARY;
  262. dst->Register.Index = aactx->colorTemp;
  263. }
  264. }
  265. ctx->emit_instruction(ctx, inst);
  266. }
  267. /**
  268. * Generate the frag shader we'll use for drawing AA lines.
  269. * This will be the user's shader plus some texture/modulate instructions.
  270. */
  271. static boolean
  272. generate_aaline_fs(struct aaline_stage *aaline)
  273. {
  274. struct pipe_context *pipe = aaline->stage.draw->pipe;
  275. const struct pipe_shader_state *orig_fs = &aaline->fs->state;
  276. struct pipe_shader_state aaline_fs;
  277. struct aa_transform_context transform;
  278. const uint newLen = tgsi_num_tokens(orig_fs->tokens) + NUM_NEW_TOKENS;
  279. aaline_fs = *orig_fs; /* copy to init */
  280. aaline_fs.tokens = tgsi_alloc_tokens(newLen);
  281. if (aaline_fs.tokens == NULL)
  282. return FALSE;
  283. memset(&transform, 0, sizeof(transform));
  284. transform.colorOutput = -1;
  285. transform.maxInput = -1;
  286. transform.maxGeneric = -1;
  287. transform.colorTemp = -1;
  288. transform.texTemp = -1;
  289. transform.base.prolog = aa_transform_prolog;
  290. transform.base.epilog = aa_transform_epilog;
  291. transform.base.transform_instruction = aa_transform_inst;
  292. transform.base.transform_declaration = aa_transform_decl;
  293. tgsi_transform_shader(orig_fs->tokens,
  294. (struct tgsi_token *) aaline_fs.tokens,
  295. newLen, &transform.base);
  296. #if 0 /* DEBUG */
  297. debug_printf("draw_aaline, orig shader:\n");
  298. tgsi_dump(orig_fs->tokens, 0);
  299. debug_printf("draw_aaline, new shader:\n");
  300. tgsi_dump(aaline_fs.tokens, 0);
  301. #endif
  302. aaline->fs->sampler_unit = transform.freeSampler;
  303. aaline->fs->aaline_fs = aaline->driver_create_fs_state(pipe, &aaline_fs);
  304. if (aaline->fs->aaline_fs == NULL)
  305. goto fail;
  306. aaline->fs->generic_attrib = transform.maxGeneric + 1;
  307. FREE((void *)aaline_fs.tokens);
  308. return TRUE;
  309. fail:
  310. FREE((void *)aaline_fs.tokens);
  311. return FALSE;
  312. }
  313. /**
  314. * Create the texture map we'll use for antialiasing the lines.
  315. */
  316. static boolean
  317. aaline_create_texture(struct aaline_stage *aaline)
  318. {
  319. struct pipe_context *pipe = aaline->stage.draw->pipe;
  320. struct pipe_screen *screen = pipe->screen;
  321. struct pipe_resource texTemp;
  322. struct pipe_sampler_view viewTempl;
  323. uint level;
  324. memset(&texTemp, 0, sizeof(texTemp));
  325. texTemp.target = PIPE_TEXTURE_2D;
  326. texTemp.format = PIPE_FORMAT_A8_UNORM; /* XXX verify supported by driver! */
  327. texTemp.last_level = MAX_TEXTURE_LEVEL;
  328. texTemp.width0 = 1 << TEXTURE_SIZE_LOG2;
  329. texTemp.height0 = 1 << TEXTURE_SIZE_LOG2;
  330. texTemp.depth0 = 1;
  331. texTemp.array_size = 1;
  332. texTemp.bind = PIPE_BIND_SAMPLER_VIEW;
  333. aaline->texture = screen->resource_create(screen, &texTemp);
  334. if (!aaline->texture)
  335. return FALSE;
  336. u_sampler_view_default_template(&viewTempl,
  337. aaline->texture,
  338. aaline->texture->format);
  339. aaline->sampler_view = pipe->create_sampler_view(pipe,
  340. aaline->texture,
  341. &viewTempl);
  342. if (!aaline->sampler_view) {
  343. return FALSE;
  344. }
  345. /* Fill in mipmap images.
  346. * Basically each level is solid opaque, except for the outermost
  347. * texels which are zero. Special case the 1x1 and 2x2 levels
  348. * (though, those levels shouldn't be used - see the max_lod setting).
  349. */
  350. for (level = 0; level <= MAX_TEXTURE_LEVEL; level++) {
  351. struct pipe_transfer *transfer;
  352. struct pipe_box box;
  353. const uint size = u_minify(aaline->texture->width0, level);
  354. ubyte *data;
  355. uint i, j;
  356. assert(aaline->texture->width0 == aaline->texture->height0);
  357. u_box_origin_2d( size, size, &box );
  358. /* This texture is new, no need to flush.
  359. */
  360. data = pipe->transfer_map(pipe,
  361. aaline->texture,
  362. level,
  363. PIPE_TRANSFER_WRITE,
  364. &box, &transfer);
  365. if (!data)
  366. return FALSE;
  367. for (i = 0; i < size; i++) {
  368. for (j = 0; j < size; j++) {
  369. ubyte d;
  370. if (size == 1) {
  371. d = 255;
  372. }
  373. else if (size == 2) {
  374. d = 200; /* tuneable */
  375. }
  376. else if (i == 0 || j == 0 || i == size - 1 || j == size - 1) {
  377. d = 35; /* edge texel */
  378. }
  379. else {
  380. d = 255;
  381. }
  382. data[i * transfer->stride + j] = d;
  383. }
  384. }
  385. /* unmap */
  386. pipe->transfer_unmap(pipe, transfer);
  387. }
  388. return TRUE;
  389. }
  390. /**
  391. * Create the sampler CSO that'll be used for antialiasing.
  392. * By using a mipmapped texture, we don't have to generate a different
  393. * texture image for each line size.
  394. */
  395. static boolean
  396. aaline_create_sampler(struct aaline_stage *aaline)
  397. {
  398. struct pipe_sampler_state sampler;
  399. struct pipe_context *pipe = aaline->stage.draw->pipe;
  400. memset(&sampler, 0, sizeof(sampler));
  401. sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
  402. sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
  403. sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
  404. sampler.min_mip_filter = PIPE_TEX_MIPFILTER_LINEAR;
  405. sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;
  406. sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
  407. sampler.normalized_coords = 1;
  408. sampler.min_lod = 0.0f;
  409. sampler.max_lod = MAX_TEXTURE_LEVEL;
  410. aaline->sampler_cso = pipe->create_sampler_state(pipe, &sampler);
  411. if (aaline->sampler_cso == NULL)
  412. return FALSE;
  413. return TRUE;
  414. }
  415. /**
  416. * When we're about to draw our first AA line in a batch, this function is
  417. * called to tell the driver to bind our modified fragment shader.
  418. */
  419. static boolean
  420. bind_aaline_fragment_shader(struct aaline_stage *aaline)
  421. {
  422. struct draw_context *draw = aaline->stage.draw;
  423. struct pipe_context *pipe = draw->pipe;
  424. if (!aaline->fs->aaline_fs &&
  425. !generate_aaline_fs(aaline))
  426. return FALSE;
  427. draw->suspend_flushing = TRUE;
  428. aaline->driver_bind_fs_state(pipe, aaline->fs->aaline_fs);
  429. draw->suspend_flushing = FALSE;
  430. return TRUE;
  431. }
  432. static inline struct aaline_stage *
  433. aaline_stage( struct draw_stage *stage )
  434. {
  435. return (struct aaline_stage *) stage;
  436. }
  437. /**
  438. * Draw a wide line by drawing a quad, using geometry which will
  439. * fullfill GL's antialiased line requirements.
  440. */
  441. static void
  442. aaline_line(struct draw_stage *stage, struct prim_header *header)
  443. {
  444. const struct aaline_stage *aaline = aaline_stage(stage);
  445. const float half_width = aaline->half_line_width;
  446. struct prim_header tri;
  447. struct vertex_header *v[8];
  448. uint texPos = aaline->tex_slot;
  449. uint posPos = aaline->pos_slot;
  450. float *pos, *tex;
  451. float dx = header->v[1]->data[posPos][0] - header->v[0]->data[posPos][0];
  452. float dy = header->v[1]->data[posPos][1] - header->v[0]->data[posPos][1];
  453. double a = atan2(dy, dx);
  454. float c_a = (float) cos(a), s_a = (float) sin(a);
  455. uint i;
  456. /* XXX the ends of lines aren't quite perfect yet, but probably passable */
  457. dx = 0.5F * half_width;
  458. dy = half_width;
  459. /* allocate/dup new verts */
  460. for (i = 0; i < 8; i++) {
  461. v[i] = dup_vert(stage, header->v[i/4], i);
  462. }
  463. /*
  464. * Quad strip for line from v0 to v1 (*=endpoints):
  465. *
  466. * 1 3 5 7
  467. * +---+---------------------+---+
  468. * | |
  469. * | *v0 v1* |
  470. * | |
  471. * +---+---------------------+---+
  472. * 0 2 4 6
  473. */
  474. /* new verts */
  475. pos = v[0]->data[posPos];
  476. pos[0] += (-dx * c_a - dy * s_a);
  477. pos[1] += (-dx * s_a + dy * c_a);
  478. pos = v[1]->data[posPos];
  479. pos[0] += (-dx * c_a - -dy * s_a);
  480. pos[1] += (-dx * s_a + -dy * c_a);
  481. pos = v[2]->data[posPos];
  482. pos[0] += ( dx * c_a - dy * s_a);
  483. pos[1] += ( dx * s_a + dy * c_a);
  484. pos = v[3]->data[posPos];
  485. pos[0] += ( dx * c_a - -dy * s_a);
  486. pos[1] += ( dx * s_a + -dy * c_a);
  487. pos = v[4]->data[posPos];
  488. pos[0] += (-dx * c_a - dy * s_a);
  489. pos[1] += (-dx * s_a + dy * c_a);
  490. pos = v[5]->data[posPos];
  491. pos[0] += (-dx * c_a - -dy * s_a);
  492. pos[1] += (-dx * s_a + -dy * c_a);
  493. pos = v[6]->data[posPos];
  494. pos[0] += ( dx * c_a - dy * s_a);
  495. pos[1] += ( dx * s_a + dy * c_a);
  496. pos = v[7]->data[posPos];
  497. pos[0] += ( dx * c_a - -dy * s_a);
  498. pos[1] += ( dx * s_a + -dy * c_a);
  499. /* new texcoords */
  500. tex = v[0]->data[texPos];
  501. ASSIGN_4V(tex, 0, 0, 0, 1);
  502. tex = v[1]->data[texPos];
  503. ASSIGN_4V(tex, 0, 1, 0, 1);
  504. tex = v[2]->data[texPos];
  505. ASSIGN_4V(tex, .5, 0, 0, 1);
  506. tex = v[3]->data[texPos];
  507. ASSIGN_4V(tex, .5, 1, 0, 1);
  508. tex = v[4]->data[texPos];
  509. ASSIGN_4V(tex, .5, 0, 0, 1);
  510. tex = v[5]->data[texPos];
  511. ASSIGN_4V(tex, .5, 1, 0, 1);
  512. tex = v[6]->data[texPos];
  513. ASSIGN_4V(tex, 1, 0, 0, 1);
  514. tex = v[7]->data[texPos];
  515. ASSIGN_4V(tex, 1, 1, 0, 1);
  516. /* emit 6 tris for the quad strip */
  517. tri.v[0] = v[2]; tri.v[1] = v[1]; tri.v[2] = v[0];
  518. stage->next->tri( stage->next, &tri );
  519. tri.v[0] = v[3]; tri.v[1] = v[1]; tri.v[2] = v[2];
  520. stage->next->tri( stage->next, &tri );
  521. tri.v[0] = v[4]; tri.v[1] = v[3]; tri.v[2] = v[2];
  522. stage->next->tri( stage->next, &tri );
  523. tri.v[0] = v[5]; tri.v[1] = v[3]; tri.v[2] = v[4];
  524. stage->next->tri( stage->next, &tri );
  525. tri.v[0] = v[6]; tri.v[1] = v[5]; tri.v[2] = v[4];
  526. stage->next->tri( stage->next, &tri );
  527. tri.v[0] = v[7]; tri.v[1] = v[5]; tri.v[2] = v[6];
  528. stage->next->tri( stage->next, &tri );
  529. }
  530. static void
  531. aaline_first_line(struct draw_stage *stage, struct prim_header *header)
  532. {
  533. auto struct aaline_stage *aaline = aaline_stage(stage);
  534. struct draw_context *draw = stage->draw;
  535. struct pipe_context *pipe = draw->pipe;
  536. const struct pipe_rasterizer_state *rast = draw->rasterizer;
  537. uint num_samplers;
  538. uint num_sampler_views;
  539. void *r;
  540. assert(draw->rasterizer->line_smooth);
  541. if (draw->rasterizer->line_width <= 2.2)
  542. aaline->half_line_width = 1.1f;
  543. else
  544. aaline->half_line_width = 0.5f * draw->rasterizer->line_width;
  545. /*
  546. * Bind (generate) our fragprog, sampler and texture
  547. */
  548. if (!bind_aaline_fragment_shader(aaline)) {
  549. stage->line = draw_pipe_passthrough_line;
  550. stage->line(stage, header);
  551. return;
  552. }
  553. draw_aaline_prepare_outputs(draw, draw->pipeline.aaline);
  554. /* how many samplers? */
  555. /* we'll use sampler/texture[aaline->sampler_unit] for the alpha texture */
  556. num_samplers = MAX2(aaline->num_samplers, aaline->fs->sampler_unit + 1);
  557. num_sampler_views = MAX2(num_samplers, aaline->num_sampler_views);
  558. aaline->state.sampler[aaline->fs->sampler_unit] = aaline->sampler_cso;
  559. pipe_sampler_view_reference(&aaline->state.sampler_views[aaline->fs->sampler_unit],
  560. aaline->sampler_view);
  561. draw->suspend_flushing = TRUE;
  562. aaline->driver_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
  563. num_samplers, aaline->state.sampler);
  564. aaline->driver_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,
  565. num_sampler_views, aaline->state.sampler_views);
  566. /* Disable triangle culling, stippling, unfilled mode etc. */
  567. r = draw_get_rasterizer_no_cull(draw, rast->scissor, rast->flatshade);
  568. pipe->bind_rasterizer_state(pipe, r);
  569. draw->suspend_flushing = FALSE;
  570. /* now really draw first line */
  571. stage->line = aaline_line;
  572. stage->line(stage, header);
  573. }
  574. static void
  575. aaline_flush(struct draw_stage *stage, unsigned flags)
  576. {
  577. struct draw_context *draw = stage->draw;
  578. struct aaline_stage *aaline = aaline_stage(stage);
  579. struct pipe_context *pipe = draw->pipe;
  580. stage->line = aaline_first_line;
  581. stage->next->flush( stage->next, flags );
  582. /* restore original frag shader, texture, sampler state */
  583. draw->suspend_flushing = TRUE;
  584. aaline->driver_bind_fs_state(pipe, aaline->fs ? aaline->fs->driver_fs : NULL);
  585. aaline->driver_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
  586. aaline->num_samplers,
  587. aaline->state.sampler);
  588. aaline->driver_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,
  589. aaline->num_samplers,
  590. aaline->state.sampler_views);
  591. /* restore original rasterizer state */
  592. if (draw->rast_handle) {
  593. pipe->bind_rasterizer_state(pipe, draw->rast_handle);
  594. }
  595. draw->suspend_flushing = FALSE;
  596. draw_remove_extra_vertex_attribs(draw);
  597. }
  598. static void
  599. aaline_reset_stipple_counter(struct draw_stage *stage)
  600. {
  601. stage->next->reset_stipple_counter( stage->next );
  602. }
  603. static void
  604. aaline_destroy(struct draw_stage *stage)
  605. {
  606. struct aaline_stage *aaline = aaline_stage(stage);
  607. struct pipe_context *pipe = stage->draw->pipe;
  608. uint i;
  609. for (i = 0; i < PIPE_MAX_SHADER_SAMPLER_VIEWS; i++) {
  610. pipe_sampler_view_reference(&aaline->state.sampler_views[i], NULL);
  611. }
  612. if (aaline->sampler_cso)
  613. pipe->delete_sampler_state(pipe, aaline->sampler_cso);
  614. if (aaline->texture)
  615. pipe_resource_reference(&aaline->texture, NULL);
  616. if (aaline->sampler_view) {
  617. pipe_sampler_view_reference(&aaline->sampler_view, NULL);
  618. }
  619. draw_free_temp_verts( stage );
  620. /* restore the old entry points */
  621. pipe->create_fs_state = aaline->driver_create_fs_state;
  622. pipe->bind_fs_state = aaline->driver_bind_fs_state;
  623. pipe->delete_fs_state = aaline->driver_delete_fs_state;
  624. pipe->bind_sampler_states = aaline->driver_bind_sampler_states;
  625. pipe->set_sampler_views = aaline->driver_set_sampler_views;
  626. FREE( stage );
  627. }
  628. static struct aaline_stage *
  629. draw_aaline_stage(struct draw_context *draw)
  630. {
  631. struct aaline_stage *aaline = CALLOC_STRUCT(aaline_stage);
  632. if (!aaline)
  633. return NULL;
  634. aaline->stage.draw = draw;
  635. aaline->stage.name = "aaline";
  636. aaline->stage.next = NULL;
  637. aaline->stage.point = draw_pipe_passthrough_point;
  638. aaline->stage.line = aaline_first_line;
  639. aaline->stage.tri = draw_pipe_passthrough_tri;
  640. aaline->stage.flush = aaline_flush;
  641. aaline->stage.reset_stipple_counter = aaline_reset_stipple_counter;
  642. aaline->stage.destroy = aaline_destroy;
  643. if (!draw_alloc_temp_verts( &aaline->stage, 8 ))
  644. goto fail;
  645. return aaline;
  646. fail:
  647. aaline->stage.destroy(&aaline->stage);
  648. return NULL;
  649. }
  650. static struct aaline_stage *
  651. aaline_stage_from_pipe(struct pipe_context *pipe)
  652. {
  653. struct draw_context *draw = (struct draw_context *) pipe->draw;
  654. if (draw) {
  655. return aaline_stage(draw->pipeline.aaline);
  656. } else {
  657. return NULL;
  658. }
  659. }
  660. /**
  661. * This function overrides the driver's create_fs_state() function and
  662. * will typically be called by the state tracker.
  663. */
  664. static void *
  665. aaline_create_fs_state(struct pipe_context *pipe,
  666. const struct pipe_shader_state *fs)
  667. {
  668. struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
  669. struct aaline_fragment_shader *aafs = NULL;
  670. if (!aaline)
  671. return NULL;
  672. aafs = CALLOC_STRUCT(aaline_fragment_shader);
  673. if (!aafs)
  674. return NULL;
  675. aafs->state.tokens = tgsi_dup_tokens(fs->tokens);
  676. /* pass-through */
  677. aafs->driver_fs = aaline->driver_create_fs_state(pipe, fs);
  678. return aafs;
  679. }
  680. static void
  681. aaline_bind_fs_state(struct pipe_context *pipe, void *fs)
  682. {
  683. struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
  684. struct aaline_fragment_shader *aafs = (struct aaline_fragment_shader *) fs;
  685. if (!aaline) {
  686. return;
  687. }
  688. /* save current */
  689. aaline->fs = aafs;
  690. /* pass-through */
  691. aaline->driver_bind_fs_state(pipe, (aafs ? aafs->driver_fs : NULL));
  692. }
  693. static void
  694. aaline_delete_fs_state(struct pipe_context *pipe, void *fs)
  695. {
  696. struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
  697. struct aaline_fragment_shader *aafs = (struct aaline_fragment_shader *) fs;
  698. if (!aafs) {
  699. return;
  700. }
  701. if (aaline) {
  702. /* pass-through */
  703. aaline->driver_delete_fs_state(pipe, aafs->driver_fs);
  704. if (aafs->aaline_fs)
  705. aaline->driver_delete_fs_state(pipe, aafs->aaline_fs);
  706. }
  707. FREE((void*)aafs->state.tokens);
  708. FREE(aafs);
  709. }
  710. static void
  711. aaline_bind_sampler_states(struct pipe_context *pipe,
  712. enum pipe_shader_type shader,
  713. unsigned start, unsigned num, void **sampler)
  714. {
  715. struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
  716. assert(start == 0);
  717. if (!aaline) {
  718. return;
  719. }
  720. if (shader == PIPE_SHADER_FRAGMENT) {
  721. /* save current */
  722. memcpy(aaline->state.sampler, sampler, num * sizeof(void *));
  723. aaline->num_samplers = num;
  724. }
  725. /* pass-through */
  726. aaline->driver_bind_sampler_states(pipe, shader, start, num, sampler);
  727. }
  728. static void
  729. aaline_set_sampler_views(struct pipe_context *pipe,
  730. enum pipe_shader_type shader,
  731. unsigned start, unsigned num,
  732. struct pipe_sampler_view **views)
  733. {
  734. struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
  735. uint i;
  736. if (!aaline) {
  737. return;
  738. }
  739. if (shader == PIPE_SHADER_FRAGMENT) {
  740. /* save current */
  741. for (i = 0; i < num; i++) {
  742. pipe_sampler_view_reference(&aaline->state.sampler_views[start + i],
  743. views[i]);
  744. }
  745. aaline->num_sampler_views = num;
  746. }
  747. /* pass-through */
  748. aaline->driver_set_sampler_views(pipe, shader, start, num, views);
  749. }
  750. void
  751. draw_aaline_prepare_outputs(struct draw_context *draw,
  752. struct draw_stage *stage)
  753. {
  754. struct aaline_stage *aaline = aaline_stage(stage);
  755. const struct pipe_rasterizer_state *rast = draw->rasterizer;
  756. /* update vertex attrib info */
  757. aaline->pos_slot = draw_current_shader_position_output(draw);
  758. if (!rast->line_smooth)
  759. return;
  760. /* allocate the extra post-transformed vertex attribute */
  761. aaline->tex_slot = draw_alloc_extra_vertex_attrib(draw,
  762. TGSI_SEMANTIC_GENERIC,
  763. aaline->fs->generic_attrib);
  764. }
  765. /**
  766. * Called by drivers that want to install this AA line prim stage
  767. * into the draw module's pipeline. This will not be used if the
  768. * hardware has native support for AA lines.
  769. */
  770. boolean
  771. draw_install_aaline_stage(struct draw_context *draw, struct pipe_context *pipe)
  772. {
  773. struct aaline_stage *aaline;
  774. pipe->draw = (void *) draw;
  775. /*
  776. * Create / install AA line drawing / prim stage
  777. */
  778. aaline = draw_aaline_stage( draw );
  779. if (!aaline)
  780. goto fail;
  781. /* save original driver functions */
  782. aaline->driver_create_fs_state = pipe->create_fs_state;
  783. aaline->driver_bind_fs_state = pipe->bind_fs_state;
  784. aaline->driver_delete_fs_state = pipe->delete_fs_state;
  785. aaline->driver_bind_sampler_states = pipe->bind_sampler_states;
  786. aaline->driver_set_sampler_views = pipe->set_sampler_views;
  787. /* create special texture, sampler state */
  788. if (!aaline_create_texture(aaline))
  789. goto fail;
  790. if (!aaline_create_sampler(aaline))
  791. goto fail;
  792. /* override the driver's functions */
  793. pipe->create_fs_state = aaline_create_fs_state;
  794. pipe->bind_fs_state = aaline_bind_fs_state;
  795. pipe->delete_fs_state = aaline_delete_fs_state;
  796. pipe->bind_sampler_states = aaline_bind_sampler_states;
  797. pipe->set_sampler_views = aaline_set_sampler_views;
  798. /* Install once everything is known to be OK:
  799. */
  800. draw->pipeline.aaline = &aaline->stage;
  801. return TRUE;
  802. fail:
  803. if (aaline)
  804. aaline->stage.destroy( &aaline->stage );
  805. return FALSE;
  806. }