2 * Copyright © 2006, 2009 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Eric Anholt <eric@anholt.net>
25 * Chris Wilson <chris@chris-wilson.co.uk>
28 #ifndef CAIRO_DRM_I915_PRIVATE_H
29 #define CAIRO_DRM_I915_PRIVATE_H
31 #include "cairo-types-private.h"
33 #include "cairo-drm-private.h"
34 #include "cairo-drm-intel-private.h"
35 #include "cairo-drm-intel-command-private.h"
36 #include "cairo-drm-intel-ioctl-private.h"
37 #include "cairo-freelist-private.h"
41 #define I915_VERBOSE 1
43 #define I915_MAX_TEX_INDIRECT 4
44 #define I915_MAX_TEX_INSN 32
45 #define I915_MAX_ALU_INSN 64
46 #define I915_MAX_DECL_INSN 27
47 #define I915_MAX_TEMPORARY 16
49 /* Each instruction is 3 dwords long, though most don't require all
50 * this space. Maximum of 123 instructions. Smaller maxes per insn
53 #define _3DSTATE_PIXEL_SHADER_PROGRAM (CMD_3D|(0x1d<<24)|(0x5<<16))
55 #define REG_TYPE_R 0 /* temporary regs, no need to
56 * dcl, must be written before
57 * read -- Preserved between
60 #define REG_TYPE_T 1 /* Interpolated values, must be
63 * 0..7: texture coord,
66 * 10: fog parameter in w.
68 #define REG_TYPE_CONST 2 /* Restriction: only one const
69 * can be referenced per
70 * instruction, though it may be
71 * selected for multiple inputs.
72 * Constants not initialized
75 #define REG_TYPE_S 3 /* sampler */
76 #define REG_TYPE_OC 4 /* output color (rgba) */
77 #define REG_TYPE_OD 5 /* output depth (w), xyz are
78 * temporaries. If not written,
79 * interpolated depth is used?
81 #define REG_TYPE_U 6 /* unpreserved temporaries */
82 #define REG_TYPE_MASK 0x7
83 #define REG_TYPE_SHIFT 4
84 #define REG_NR_MASK 0xf
98 #define T_FOG_W 10 /* interpolated fog is in W coord */
100 /* Arithmetic instructions */
102 /* .replicate_swizzle == selection and replication of a particular
103 * scalar channel, ie., .xxxx, .yyyy, .zzzz or .wwww
105 #define A0_NOP (0x0<<24) /* no operation */
106 #define A0_ADD (0x1<<24) /* dst = src0 + src1 */
107 #define A0_MOV (0x2<<24) /* dst = src0 */
108 #define A0_MUL (0x3<<24) /* dst = src0 * src1 */
109 #define A0_MAD (0x4<<24) /* dst = src0 * src1 + src2 */
110 #define A0_DP2ADD (0x5<<24) /* dst.xyzw = src0.xy dot src1.xy + src2.replicate_swizzle */
111 #define A0_DP3 (0x6<<24) /* dst.xyzw = src0.xyz dot src1.xyz */
112 #define A0_DP4 (0x7<<24) /* dst.xyzw = src0.xyzw dot src1.xyzw */
113 #define A0_FRC (0x8<<24) /* dst = src0 - floor(src0) */
114 #define A0_RCP (0x9<<24) /* dst.xyzw = 1/(src0.replicate_swizzle) */
115 #define A0_RSQ (0xa<<24) /* dst.xyzw = 1/(sqrt(abs(src0.replicate_swizzle))) */
116 #define A0_EXP (0xb<<24) /* dst.xyzw = exp2(src0.replicate_swizzle) */
117 #define A0_LOG (0xc<<24) /* dst.xyzw = log2(abs(src0.replicate_swizzle)) */
118 #define A0_CMP (0xd<<24) /* dst = (src0 >= 0.0) ? src1 : src2 */
119 #define A0_MIN (0xe<<24) /* dst = (src0 < src1) ? src0 : src1 */
120 #define A0_MAX (0xf<<24) /* dst = (src0 >= src1) ? src0 : src1 */
121 #define A0_FLR (0x10<<24) /* dst = floor(src0) */
122 #define A0_MOD (0x11<<24) /* dst = src0 fmod 1.0 */
123 #define A0_TRC (0x12<<24) /* dst = int(src0) */
124 #define A0_SGE (0x13<<24) /* dst = src0 >= src1 ? 1.0 : 0.0 */
125 #define A0_SLT (0x14<<24) /* dst = src0 < src1 ? 1.0 : 0.0 */
126 #define A0_DEST_SATURATE (1<<22)
127 #define A0_DEST_TYPE_SHIFT 19
128 /* Allow: R, OC, OD, U */
129 #define A0_DEST_NR_SHIFT 14
130 /* Allow R: 0..15, OC,OD: 0..0, U: 0..2 */
131 #define A0_DEST_CHANNEL_X (1<<10)
132 #define A0_DEST_CHANNEL_Y (2<<10)
133 #define A0_DEST_CHANNEL_Z (4<<10)
134 #define A0_DEST_CHANNEL_W (8<<10)
135 #define A0_DEST_CHANNEL_ALL (0xf<<10)
136 #define A0_DEST_CHANNEL_SHIFT 10
137 #define A0_SRC0_TYPE_SHIFT 7
138 #define A0_SRC0_NR_SHIFT 2
140 #define A0_DEST_CHANNEL_XY (A0_DEST_CHANNEL_X|A0_DEST_CHANNEL_Y)
141 #define A0_DEST_CHANNEL_XYZ (A0_DEST_CHANNEL_XY|A0_DEST_CHANNEL_Z)
150 #define A1_SRC0_CHANNEL_X_NEGATE (1<<31)
151 #define A1_SRC0_CHANNEL_X_SHIFT 28
152 #define A1_SRC0_CHANNEL_Y_NEGATE (1<<27)
153 #define A1_SRC0_CHANNEL_Y_SHIFT 24
154 #define A1_SRC0_CHANNEL_Z_NEGATE (1<<23)
155 #define A1_SRC0_CHANNEL_Z_SHIFT 20
156 #define A1_SRC0_CHANNEL_W_NEGATE (1<<19)
157 #define A1_SRC0_CHANNEL_W_SHIFT 16
158 #define A1_SRC1_TYPE_SHIFT 13
159 #define A1_SRC1_NR_SHIFT 8
160 #define A1_SRC1_CHANNEL_X_NEGATE (1<<7)
161 #define A1_SRC1_CHANNEL_X_SHIFT 4
162 #define A1_SRC1_CHANNEL_Y_NEGATE (1<<3)
163 #define A1_SRC1_CHANNEL_Y_SHIFT 0
165 #define A2_SRC1_CHANNEL_Z_NEGATE (1<<31)
166 #define A2_SRC1_CHANNEL_Z_SHIFT 28
167 #define A2_SRC1_CHANNEL_W_NEGATE (1<<27)
168 #define A2_SRC1_CHANNEL_W_SHIFT 24
169 #define A2_SRC2_TYPE_SHIFT 21
170 #define A2_SRC2_NR_SHIFT 16
171 #define A2_SRC2_CHANNEL_X_NEGATE (1<<15)
172 #define A2_SRC2_CHANNEL_X_SHIFT 12
173 #define A2_SRC2_CHANNEL_Y_NEGATE (1<<11)
174 #define A2_SRC2_CHANNEL_Y_SHIFT 8
175 #define A2_SRC2_CHANNEL_Z_NEGATE (1<<7)
176 #define A2_SRC2_CHANNEL_Z_SHIFT 4
177 #define A2_SRC2_CHANNEL_W_NEGATE (1<<3)
178 #define A2_SRC2_CHANNEL_W_SHIFT 0
180 /* Texture instructions */
181 #define T0_TEXLD (0x15<<24) /* Sample texture using predeclared
182 * sampler and address, and output
183 * filtered texel data to destination
185 #define T0_TEXLDP (0x16<<24) /* Same as texld but performs a
186 * perspective divide of the texture
187 * coordinate .xyz values by .w before
189 #define T0_TEXLDB (0x17<<24) /* Same as texld but biases the
190 * computed LOD by w. Only S4.6 two's
191 * comp is used. This implies that a
192 * float to fixed conversion is
194 #define T0_TEXKILL (0x18<<24) /* Does not perform a sampling
195 * operation. Simply kills the pixel
196 * if any channel of the address
197 * register is < 0.0. */
198 #define T0_DEST_TYPE_SHIFT 19
199 /* Allow: R, OC, OD, U */
200 /* Note: U (unpreserved) regs do not retain their values between
201 * phases (cannot be used for feedback)
203 * Note: oC and OD registers can only be used as the destination of a
204 * texture instruction once per phase (this is an implementation
207 #define T0_DEST_NR_SHIFT 14
208 /* Allow R: 0..15, OC,OD: 0..0, U: 0..2 */
209 #define T0_SAMPLER_NR_SHIFT 0 /* This field ignored for TEXKILL */
210 #define T0_SAMPLER_NR_MASK (0xf<<0)
212 #define T1_ADDRESS_REG_TYPE_SHIFT 24 /* Reg to use as texture coord */
213 /* Allow R, T, OC, OD -- R, OC, OD are 'dependent' reads, new program phase */
214 #define T1_ADDRESS_REG_NR_SHIFT 17
217 /* Declaration instructions */
218 #define D0_DCL (0x19<<24) /* Declare a t (interpolated attrib)
219 * register or an s (sampler)
221 #define D0_SAMPLE_TYPE_SHIFT 22
222 #define D0_SAMPLE_TYPE_2D (0x0<<22)
223 #define D0_SAMPLE_TYPE_CUBE (0x1<<22)
224 #define D0_SAMPLE_TYPE_VOLUME (0x2<<22)
225 #define D0_SAMPLE_TYPE_MASK (0x3<<22)
227 #define D0_TYPE_SHIFT 19
229 #define D0_NR_SHIFT 14
230 /* Allow T: 0..10, S: 0..15 */
231 #define D0_CHANNEL_X (1<<10)
232 #define D0_CHANNEL_Y (2<<10)
233 #define D0_CHANNEL_Z (4<<10)
234 #define D0_CHANNEL_W (8<<10)
235 #define D0_CHANNEL_ALL (0xf<<10)
236 #define D0_CHANNEL_NONE (0<<10)
238 #define D0_CHANNEL_XY (D0_CHANNEL_X|D0_CHANNEL_Y)
239 #define D0_CHANNEL_XYZ (D0_CHANNEL_XY|D0_CHANNEL_Z)
241 /* I915 Errata: Do not allow (xz), (xw), (xzw) combinations for diffuse
242 * or specular declarations.
244 * For T dcls, only allow: (x), (xy), (xyz), (w), (xyzw)
246 * Must be zero for S (sampler) dcls
252 /* MASK_* are the unshifted bitmasks of the destination mask in arithmetic
259 #define MASK_XYZ (MASK_X | MASK_Y | MASK_Z)
260 #define MASK_XYZW (MASK_XYZ | MASK_W)
261 #define MASK_SATURATE 0x10
263 /* Temporary, undeclared regs. Preserved between phases */
264 #define FS_R0 ((REG_TYPE_R << REG_TYPE_SHIFT) | 0)
265 #define FS_R1 ((REG_TYPE_R << REG_TYPE_SHIFT) | 1)
266 #define FS_R2 ((REG_TYPE_R << REG_TYPE_SHIFT) | 2)
267 #define FS_R3 ((REG_TYPE_R << REG_TYPE_SHIFT) | 3)
269 /* Texture coordinate regs. Must be declared. */
270 #define FS_T0 ((REG_TYPE_T << REG_TYPE_SHIFT) | 0)
271 #define FS_T1 ((REG_TYPE_T << REG_TYPE_SHIFT) | 1)
272 #define FS_T2 ((REG_TYPE_T << REG_TYPE_SHIFT) | 2)
273 #define FS_T3 ((REG_TYPE_T << REG_TYPE_SHIFT) | 3)
274 #define FS_T4 ((REG_TYPE_T << REG_TYPE_SHIFT) | 4)
275 #define FS_T5 ((REG_TYPE_T << REG_TYPE_SHIFT) | 5)
276 #define FS_T6 ((REG_TYPE_T << REG_TYPE_SHIFT) | 6)
277 #define FS_T7 ((REG_TYPE_T << REG_TYPE_SHIFT) | 7)
278 #define FS_T8 ((REG_TYPE_T << REG_TYPE_SHIFT) | 8)
279 #define FS_T9 ((REG_TYPE_T << REG_TYPE_SHIFT) | 9)
280 #define FS_T10 ((REG_TYPE_T << REG_TYPE_SHIFT) | 10)
282 /* Constant values */
283 #define FS_C0 ((REG_TYPE_CONST << REG_TYPE_SHIFT) | 0)
284 #define FS_C1 ((REG_TYPE_CONST << REG_TYPE_SHIFT) | 1)
285 #define FS_C2 ((REG_TYPE_CONST << REG_TYPE_SHIFT) | 2)
286 #define FS_C3 ((REG_TYPE_CONST << REG_TYPE_SHIFT) | 3)
287 #define FS_C4 ((REG_TYPE_CONST << REG_TYPE_SHIFT) | 4)
288 #define FS_C5 ((REG_TYPE_CONST << REG_TYPE_SHIFT) | 5)
289 #define FS_C6 ((REG_TYPE_CONST << REG_TYPE_SHIFT) | 6)
290 #define FS_C7 ((REG_TYPE_CONST << REG_TYPE_SHIFT) | 7)
293 #define FS_S0 ((REG_TYPE_S << REG_TYPE_SHIFT) | 0)
294 #define FS_S1 ((REG_TYPE_S << REG_TYPE_SHIFT) | 1)
295 #define FS_S2 ((REG_TYPE_S << REG_TYPE_SHIFT) | 2)
296 #define FS_S3 ((REG_TYPE_S << REG_TYPE_SHIFT) | 3)
299 #define FS_OC ((REG_TYPE_OC << REG_TYPE_SHIFT) | 0)
302 #define FS_OD ((REG_TYPE_OD << REG_TYPE_SHIFT) | 0)
304 /* Unpreserved temporary regs */
305 #define FS_U0 ((REG_TYPE_U << REG_TYPE_SHIFT) | 0)
306 #define FS_U1 ((REG_TYPE_U << REG_TYPE_SHIFT) | 1)
307 #define FS_U2 ((REG_TYPE_U << REG_TYPE_SHIFT) | 2)
308 #define FS_U3 ((REG_TYPE_U << REG_TYPE_SHIFT) | 3)
310 #define X_CHANNEL_SHIFT (REG_TYPE_SHIFT + 3)
311 #define Y_CHANNEL_SHIFT (X_CHANNEL_SHIFT + 4)
312 #define Z_CHANNEL_SHIFT (Y_CHANNEL_SHIFT + 4)
313 #define W_CHANNEL_SHIFT (Z_CHANNEL_SHIFT + 4)
315 #define REG_CHANNEL_MASK 0xf
317 #define REG_NR(reg) ((reg) & REG_NR_MASK)
318 #define REG_TYPE(reg) (((reg) >> REG_TYPE_SHIFT) & REG_TYPE_MASK)
319 #define REG_X(reg) (((reg) >> X_CHANNEL_SHIFT) & REG_CHANNEL_MASK)
320 #define REG_Y(reg) (((reg) >> Y_CHANNEL_SHIFT) & REG_CHANNEL_MASK)
321 #define REG_Z(reg) (((reg) >> Z_CHANNEL_SHIFT) & REG_CHANNEL_MASK)
322 #define REG_W(reg) (((reg) >> W_CHANNEL_SHIFT) & REG_CHANNEL_MASK)
324 enum i915_fs_channel {
332 NEG_X_CHANNEL_VAL = X_CHANNEL_VAL | 0x8,
333 NEG_Y_CHANNEL_VAL = Y_CHANNEL_VAL | 0x8,
334 NEG_Z_CHANNEL_VAL = Z_CHANNEL_VAL | 0x8,
335 NEG_W_CHANNEL_VAL = W_CHANNEL_VAL | 0x8,
336 NEG_ONE_CHANNEL_VAL = ONE_CHANNEL_VAL | 0x8
339 #define i915_fs_operand(reg, x, y, z, w) \
341 (x##_CHANNEL_VAL << X_CHANNEL_SHIFT) | \
342 (y##_CHANNEL_VAL << Y_CHANNEL_SHIFT) | \
343 (z##_CHANNEL_VAL << Z_CHANNEL_SHIFT) | \
344 (w##_CHANNEL_VAL << W_CHANNEL_SHIFT)
347 * Construct an operand description for using a register with no swizzling
349 #define i915_fs_operand_reg(reg) \
350 i915_fs_operand(reg, X, Y, Z, W)
352 #define i915_fs_operand_reg_negate(reg) \
353 i915_fs_operand(reg, NEG_X, NEG_Y, NEG_Z, NEG_W)
356 * Returns an operand containing (0.0, 0.0, 0.0, 0.0).
358 #define i915_fs_operand_zero() i915_fs_operand(FS_R0, ZERO, ZERO, ZERO, ZERO)
361 * Returns an unused operand
363 #define i915_fs_operand_none() i915_fs_operand_zero()
366 * Returns an operand containing (1.0, 1.0, 1.0, 1.0).
368 #define i915_fs_operand_one() i915_fs_operand(FS_R0, ONE, ONE, ONE, ONE)
370 #define i915_get_hardware_channel_val(val, shift, negate) \
371 (((val & 0x7) << shift) | ((val & 0x8) ? negate : 0))
374 * Outputs a fragment shader command to declare a sampler or texture register.
376 #define i915_fs_dcl(reg) \
378 OUT_DWORD (D0_DCL | \
379 (REG_TYPE(reg) << D0_TYPE_SHIFT) | \
380 (REG_NR(reg) << D0_NR_SHIFT) | \
381 ((REG_TYPE(reg) != REG_TYPE_S) ? D0_CHANNEL_ALL : 0)); \
386 #define i915_fs_texld(dest_reg, sampler_reg, address_reg) \
388 OUT_DWORD (T0_TEXLD | \
389 (REG_TYPE(dest_reg) << T0_DEST_TYPE_SHIFT) | \
390 (REG_NR(dest_reg) << T0_DEST_NR_SHIFT) | \
391 (REG_NR(sampler_reg) << T0_SAMPLER_NR_SHIFT)); \
392 OUT_DWORD((REG_TYPE(address_reg) << T1_ADDRESS_REG_TYPE_SHIFT) | \
393 (REG_NR(address_reg) << T1_ADDRESS_REG_NR_SHIFT)); \
397 #define i915_fs_arith_masked(op, dest_reg, dest_mask, operand0, operand1, operand2) \
398 _i915_fs_arith_masked(A0_##op, dest_reg, dest_mask, operand0, operand1, operand2)
400 #define i915_fs_arith(op, dest_reg, operand0, operand1, operand2) \
401 _i915_fs_arith(A0_##op, dest_reg, operand0, operand1, operand2)
403 #define _i915_fs_arith_masked(cmd, dest_reg, dest_mask, operand0, operand1, operand2) \
405 /* Set up destination register and write mask */ \
407 (REG_TYPE(dest_reg) << A0_DEST_TYPE_SHIFT) | \
408 (REG_NR(dest_reg) << A0_DEST_NR_SHIFT) | \
409 (((dest_mask) & ~MASK_SATURATE) << A0_DEST_CHANNEL_SHIFT) | \
410 (((dest_mask) & MASK_SATURATE) ? A0_DEST_SATURATE : 0) | \
411 /* Set up operand 0 */ \
412 (REG_TYPE(operand0) << A0_SRC0_TYPE_SHIFT) | \
413 (REG_NR(operand0) << A0_SRC0_NR_SHIFT)); \
414 OUT_DWORD (i915_get_hardware_channel_val(REG_X(operand0), \
415 A1_SRC0_CHANNEL_X_SHIFT, \
416 A1_SRC0_CHANNEL_X_NEGATE) | \
417 i915_get_hardware_channel_val(REG_Y(operand0), \
418 A1_SRC0_CHANNEL_Y_SHIFT, \
419 A1_SRC0_CHANNEL_Y_NEGATE) | \
420 i915_get_hardware_channel_val(REG_Z(operand0), \
421 A1_SRC0_CHANNEL_Z_SHIFT, \
422 A1_SRC0_CHANNEL_Z_NEGATE) | \
423 i915_get_hardware_channel_val(REG_W(operand0), \
424 A1_SRC0_CHANNEL_W_SHIFT, \
425 A1_SRC0_CHANNEL_W_NEGATE) | \
426 /* Set up operand 1 */ \
427 (REG_TYPE(operand1) << A1_SRC1_TYPE_SHIFT) | \
428 (REG_NR(operand1) << A1_SRC1_NR_SHIFT) | \
429 i915_get_hardware_channel_val(REG_X(operand1), \
430 A1_SRC1_CHANNEL_X_SHIFT, \
431 A1_SRC1_CHANNEL_X_NEGATE) | \
432 i915_get_hardware_channel_val(REG_Y(operand1), \
433 A1_SRC1_CHANNEL_Y_SHIFT, \
434 A1_SRC1_CHANNEL_Y_NEGATE)); \
435 OUT_DWORD (i915_get_hardware_channel_val(REG_Z(operand1), \
436 A2_SRC1_CHANNEL_Z_SHIFT, \
437 A2_SRC1_CHANNEL_Z_NEGATE) | \
438 i915_get_hardware_channel_val(REG_W(operand1), \
439 A2_SRC1_CHANNEL_W_SHIFT, \
440 A2_SRC1_CHANNEL_W_NEGATE) | \
441 /* Set up operand 2 */ \
442 (REG_TYPE(operand2) << A2_SRC2_TYPE_SHIFT) | \
443 (REG_NR(operand2) << A2_SRC2_NR_SHIFT) | \
444 i915_get_hardware_channel_val(REG_X(operand2), \
445 A2_SRC2_CHANNEL_X_SHIFT, \
446 A2_SRC2_CHANNEL_X_NEGATE) | \
447 i915_get_hardware_channel_val(REG_Y(operand2), \
448 A2_SRC2_CHANNEL_Y_SHIFT, \
449 A2_SRC2_CHANNEL_Y_NEGATE) | \
450 i915_get_hardware_channel_val(REG_Z(operand2), \
451 A2_SRC2_CHANNEL_Z_SHIFT, \
452 A2_SRC2_CHANNEL_Z_NEGATE) | \
453 i915_get_hardware_channel_val(REG_W(operand2), \
454 A2_SRC2_CHANNEL_W_SHIFT, \
455 A2_SRC2_CHANNEL_W_NEGATE)); \
458 #define _i915_fs_arith(cmd, dest_reg, operand0, operand1, operand2) do {\
459 /* Set up destination register and write mask */ \
461 (REG_TYPE(dest_reg) << A0_DEST_TYPE_SHIFT) | \
462 (REG_NR(dest_reg) << A0_DEST_NR_SHIFT) | \
463 (A0_DEST_CHANNEL_ALL) | \
464 /* Set up operand 0 */ \
465 (REG_TYPE(operand0) << A0_SRC0_TYPE_SHIFT) | \
466 (REG_NR(operand0) << A0_SRC0_NR_SHIFT)); \
467 OUT_DWORD (i915_get_hardware_channel_val(REG_X(operand0), \
468 A1_SRC0_CHANNEL_X_SHIFT, \
469 A1_SRC0_CHANNEL_X_NEGATE) | \
470 i915_get_hardware_channel_val(REG_Y(operand0), \
471 A1_SRC0_CHANNEL_Y_SHIFT, \
472 A1_SRC0_CHANNEL_Y_NEGATE) | \
473 i915_get_hardware_channel_val(REG_Z(operand0), \
474 A1_SRC0_CHANNEL_Z_SHIFT, \
475 A1_SRC0_CHANNEL_Z_NEGATE) | \
476 i915_get_hardware_channel_val(REG_W(operand0), \
477 A1_SRC0_CHANNEL_W_SHIFT, \
478 A1_SRC0_CHANNEL_W_NEGATE) | \
479 /* Set up operand 1 */ \
480 (REG_TYPE(operand1) << A1_SRC1_TYPE_SHIFT) | \
481 (REG_NR(operand1) << A1_SRC1_NR_SHIFT) | \
482 i915_get_hardware_channel_val(REG_X(operand1), \
483 A1_SRC1_CHANNEL_X_SHIFT, \
484 A1_SRC1_CHANNEL_X_NEGATE) | \
485 i915_get_hardware_channel_val(REG_Y(operand1), \
486 A1_SRC1_CHANNEL_Y_SHIFT, \
487 A1_SRC1_CHANNEL_Y_NEGATE)); \
488 OUT_DWORD (i915_get_hardware_channel_val(REG_Z(operand1), \
489 A2_SRC1_CHANNEL_Z_SHIFT, \
490 A2_SRC1_CHANNEL_Z_NEGATE) | \
491 i915_get_hardware_channel_val(REG_W(operand1), \
492 A2_SRC1_CHANNEL_W_SHIFT, \
493 A2_SRC1_CHANNEL_W_NEGATE) | \
494 /* Set up operand 2 */ \
495 (REG_TYPE(operand2) << A2_SRC2_TYPE_SHIFT) | \
496 (REG_NR(operand2) << A2_SRC2_NR_SHIFT) | \
497 i915_get_hardware_channel_val(REG_X(operand2), \
498 A2_SRC2_CHANNEL_X_SHIFT, \
499 A2_SRC2_CHANNEL_X_NEGATE) | \
500 i915_get_hardware_channel_val(REG_Y(operand2), \
501 A2_SRC2_CHANNEL_Y_SHIFT, \
502 A2_SRC2_CHANNEL_Y_NEGATE) | \
503 i915_get_hardware_channel_val(REG_Z(operand2), \
504 A2_SRC2_CHANNEL_Z_SHIFT, \
505 A2_SRC2_CHANNEL_Z_NEGATE) | \
506 i915_get_hardware_channel_val(REG_W(operand2), \
507 A2_SRC2_CHANNEL_W_SHIFT, \
508 A2_SRC2_CHANNEL_W_NEGATE)); \
511 #define i915_fs_mov(dest_reg, operand0) \
512 i915_fs_arith(MOV, dest_reg, \
514 i915_fs_operand_none(), \
515 i915_fs_operand_none())
517 #define i915_fs_mov_masked(dest_reg, dest_mask, operand0) \
518 i915_fs_arith_masked (MOV, dest_reg, dest_mask, \
520 i915_fs_operand_none(), \
521 i915_fs_operand_none())
524 #define i915_fs_frc(dest_reg, operand0) \
525 i915_fs_arith (FRC, dest_reg, \
527 i915_fs_operand_none(), \
528 i915_fs_operand_none())
530 /* Add operand0 and operand1 and put the result in dest_reg */
531 #define i915_fs_add(dest_reg, operand0, operand1) \
532 i915_fs_arith (ADD, dest_reg, \
533 operand0, operand1, \
534 i915_fs_operand_none())
536 /* Multiply operand0 and operand1 and put the result in dest_reg */
537 #define i915_fs_mul(dest_reg, operand0, operand1) \
538 i915_fs_arith (MUL, dest_reg, \
539 operand0, operand1, \
540 i915_fs_operand_none())
542 /* Computes 1/sqrt(operand0.replicate_swizzle) puts the result in dest_reg */
543 #define i915_fs_rsq(dest_reg, dest_mask, operand0) \
546 i915_fs_arith_masked (RSQ, dest_reg, dest_mask, \
548 i915_fs_operand_none (), \
549 i915_fs_operand_none ()); \
551 i915_fs_arith (RSQ, dest_reg, \
553 i915_fs_operand_none (), \
554 i915_fs_operand_none ()); \
558 /* Puts the minimum of operand0 and operand1 in dest_reg */
559 #define i915_fs_min(dest_reg, operand0, operand1) \
560 i915_fs_arith (MIN, dest_reg, \
561 operand0, operand1, \
562 i915_fs_operand_none())
564 /* Puts the maximum of operand0 and operand1 in dest_reg */
565 #define i915_fs_max(dest_reg, operand0, operand1) \
566 i915_fs_arith (MAX, dest_reg, \
567 operand0, operand1, \
568 i915_fs_operand_none())
570 #define i915_fs_cmp(dest_reg, operand0, operand1, operand2) \
571 i915_fs_arith (CMP, dest_reg, operand0, operand1, operand2)
573 /* Perform operand0 * operand1 + operand2 and put the result in dest_reg */
574 #define i915_fs_mad(dest_reg, dest_mask, op0, op1, op2) \
577 i915_fs_arith_masked (MAD, dest_reg, dest_mask, op0, op1, op2); \
579 i915_fs_arith (MAD, dest_reg, op0, op1, op2); \
583 #define i915_fs_dp2add(dest_reg, dest_mask, op0, op1, op2) \
586 i915_fs_arith_masked (DP2ADD, dest_reg, dest_mask, op0, op1, op2); \
588 i915_fs_arith (DP2ADD, dest_reg, op0, op1, op2); \
593 * Perform a 3-component dot-product of operand0 and operand1 and put the
594 * resulting scalar in the channels of dest_reg specified by the dest_mask.
596 #define i915_fs_dp3(dest_reg, dest_mask, op0, op1) \
599 i915_fs_arith_masked (DP3, dest_reg, dest_mask, \
601 i915_fs_operand_none()); \
603 i915_fs_arith (DP3, dest_reg, op0, op1,\
604 i915_fs_operand_none()); \
608 static inline uint32_t cairo_const
609 i915_fs_operand_pure_alpha (int pure)
612 return i915_fs_operand_one ();
614 return i915_fs_operand_zero ();
617 #define I915_TILING_DEFAULT I915_TILING_Y
618 #define I915_BO_CACHE_BUCKETS 13 /* cache surfaces up to 16 MiB */
620 typedef struct i915_surface i915_surface_t;
621 typedef struct i915_device i915_device_t;
622 typedef struct i915_shader i915_shader_t;
624 typedef void (*i915_add_rectangle_func_t) (const i915_shader_t *shader,
628 #define IMAGE_CACHE_WIDTH 1024
629 #define IMAGE_CACHE_HEIGHT 1024
631 typedef struct i915_image_private {
632 cairo_rtree_node_t node;
633 intel_buffer_cache_t *container;
634 } i915_image_private_t;
636 #define I915_BATCH_SIZE (128*1024)
637 #define I915_VBO_SIZE (512*1024)
638 #define I915_MAX_RELOCS 2048
641 I915_DEBUG_EXEC = 0x1,
642 I915_DEBUG_SYNC = 0x2,
643 I915_DEBUG_BATCH = 0x4,
644 I915_DEBUG_BUFFER = 0x8,
645 I915_DEBUG_BUFFER_CACHE = 0x10,
646 I915_DEBUG_BUFFER_ALLOC = 0x20,
647 I915_DEBUG_GLYPHS = 0x40,
648 I915_DEBUG_MAP = 0x80,
649 I915_DEBUG_THROTTLE = 0x100,
653 intel_device_t intel;
657 i915_shader_t *shader; /* note: only valid during geometry emission */
660 intel_bo_t *target_bo[I915_MAX_RELOCS];
661 size_t gtt_avail_size;
663 size_t total_gtt_size;
666 uint16_t fences_avail;
667 uint16_t reloc_count;
671 struct drm_i915_gem_exec_object2 exec[I915_MAX_RELOCS];
672 struct drm_i915_gem_relocation_entry reloc[I915_MAX_RELOCS];
678 uint32_t vbo_max_index;
679 uint32_t vertex_index;
680 uint32_t vertex_count;
681 uint32_t floats_per_vertex;
682 uint32_t rectangle_size;
683 intel_bo_t *last_vbo;
684 uint32_t last_vbo_offset;
685 uint32_t last_vbo_space;
687 i915_surface_t *current_target;
688 uint32_t current_size;
689 uint32_t current_diffuse;
690 uint32_t current_colorbuf;
691 uint32_t *current_source;
692 uint32_t *current_mask;
693 uint32_t *current_clip;
694 uint32_t current_program;
695 uint32_t current_texcoords;
696 uint32_t current_blend;
697 uint32_t current_constants[8*4];
698 uint32_t current_n_constants;
699 uint32_t current_samplers[2*4];
700 uint32_t current_maps[4*4];
701 uint32_t current_n_samplers;
702 uint32_t current_n_maps;
703 uint32_t last_source_fragment;
704 uint32_t clear_alpha;
706 cairo_list_t image_caches[2];
708 uint32_t batch_header[13];
709 uint32_t batch_base[I915_BATCH_SIZE / sizeof (uint32_t)];
710 uint8_t vbo_base[I915_VBO_SIZE];
714 CURRENT_SOURCE = 0x1,
725 } i915_vertex_shader_t;
738 } i915_fragment_shader_t;
740 #define FS_DETAILS_SHIFT 4
748 } i915_shader_channel_t;
750 struct i915_surface {
751 intel_surface_t intel;
756 cairo_bool_t deferred_clear;
758 uint32_t is_current_texture;
760 i915_image_private_t *cache;
763 uint32_t stencil_stride;
764 uint32_t stencil_offset;
774 } i915_packed_pixel_t;
776 /* read-only container */
777 #define I915_PACKED_PIXEL_SURFACE_TYPE 0x1000
778 typedef struct i915_packed_pixel_surface {
779 cairo_surface_t base;
781 i915_packed_pixel_t pixel;
783 i915_device_t *device;
785 uint32_t is_current_texture;
791 uint32_t map0[4], map1[4];
792 } i915_packed_pixel_surface_t;
795 i915_device_t *device;
796 i915_surface_t *target;
801 cairo_content_t content;
803 cairo_bool_t committed;
804 cairo_bool_t need_combine;
806 i915_add_rectangle_func_t add_rectangle;
808 union i915_shader_channel {
810 i915_vertex_shader_t vertex;
811 i915_fragment_shader_t fragment;
812 i915_shader_channel_t pattern;
814 struct i915_shader_base {
815 i915_vertex_shader_t vertex;
816 i915_fragment_shader_t fragment;
817 i915_shader_channel_t pattern;
819 cairo_content_t content;
826 cairo_matrix_t matrix;
828 struct i915_shader_solid {
829 struct i915_shader_base base;
833 struct i915_shader_linear {
834 struct i915_shader_base base;
836 float red, green, blue, alpha;
838 float dx, dy, offset;
840 struct i915_shader_radial {
841 struct i915_shader_base base;
844 struct i915_shader_surface {
845 struct i915_shader_base base;
846 i915_packed_pixel_t pixel;
848 } source, mask, clip, dst;
853 enum i915_shader_linear_mode {
861 enum i915_shader_radial_mode {
866 typedef cairo_status_t
867 (*i915_spans_func_t) (void *closure,
868 cairo_span_renderer_t *renderer,
869 const cairo_rectangle_int_t *extents);
871 cairo_private cairo_status_t
872 i915_clip_and_composite_spans (i915_surface_t *dst,
874 const cairo_pattern_t *pattern,
875 cairo_antialias_t antialias,
876 i915_spans_func_t draw_func,
878 const cairo_composite_rectangles_t*extents,
882 cairo_private cairo_surface_t *
883 i915_surface_create_internal (cairo_drm_device_t *base_dev,
884 cairo_format_t format,
885 int width, int height,
887 cairo_bool_t gpu_target);
889 cairo_private i915_surface_t *
890 i915_surface_create_from_cacheable_image_internal (i915_device_t *device,
891 cairo_image_surface_t *image);
894 i915_surface_scaled_font_fini (cairo_scaled_font_t *scaled_font);
896 cairo_private cairo_int_status_t
897 i915_surface_glyphs (void *abstract_surface,
899 const cairo_pattern_t *source,
900 cairo_glyph_t *glyphs,
902 cairo_scaled_font_t *scaled_font,
906 static inline int cairo_const
907 i915_tiling_height (uint32_t tiling, int height)
911 case I915_TILING_NONE: return (height + 1) & -2;
912 case I915_TILING_X: return (height + 7) & -8;
913 case I915_TILING_Y: return (height + 31) & -32;
917 static inline uint32_t cairo_const
918 i915_tiling_stride (int format, uint32_t stride)
922 /* use 64B alignment so that the buffer may be used as a scanout */
923 if (format == I915_TILING_NONE)
924 return (stride + 63) & -64;
927 /* XXX Currently the kernel enforces a tile_width of 512 for TILING_Y.
929 <jbarnes> the docs are a bit confused on that front
930 <jbarnes> once we enable it on 915 we'll find out what the tile width size should be in the fence setup
931 <jbarnes> it could be that 915 has y tiling but that the minimum width is 512 or something
932 <jbarnes> yeah it's probably 128 on 915 also
933 <jbarnes> it's just that we haven't tested
934 <jbarnes> but I wasn't thinking that the tile widths were the same
935 <jbarnes> only that in order to fence y tiles on 915 you needed pitch to be a multiple of 4 y tiles (or something like that)
937 tile_width = format == I915_TILING_Y ? 128 : 512;
940 /* needs a pot tile width */
941 while (tile_width < stride)
947 static inline uint32_t cairo_const
948 i915_tiling_size (uint32_t tiling, uint32_t size)
952 if (tiling == I915_TILING_NONE)
953 return (size + 4095) & -4096;
955 fence = 1024 * 1024; /* 1 MiB */
962 static inline cairo_bool_t cairo_const
963 i915_texture_filter_is_nearest (cairo_filter_t filter)
966 case CAIRO_FILTER_BEST:
967 case CAIRO_FILTER_GOOD:
968 case CAIRO_FILTER_BILINEAR:
969 case CAIRO_FILTER_GAUSSIAN:
972 case CAIRO_FILTER_FAST:
973 case CAIRO_FILTER_NEAREST:
978 static inline uint32_t cairo_const
979 i915_texture_filter (cairo_filter_t filter)
982 case CAIRO_FILTER_BEST:
983 case CAIRO_FILTER_GOOD:
984 case CAIRO_FILTER_BILINEAR:
985 case CAIRO_FILTER_GAUSSIAN:
987 (FILTER_LINEAR << SS2_MAG_FILTER_SHIFT) |
988 (FILTER_LINEAR << SS2_MIN_FILTER_SHIFT);
990 case CAIRO_FILTER_FAST:
991 case CAIRO_FILTER_NEAREST:
993 (FILTER_NEAREST << SS2_MAG_FILTER_SHIFT) |
994 (FILTER_NEAREST << SS2_MIN_FILTER_SHIFT);
998 static inline uint32_t cairo_const
999 i915_texture_extend (cairo_extend_t extend)
1003 case CAIRO_EXTEND_NONE:
1005 (TEXCOORDMODE_CLAMP_BORDER << SS3_TCX_ADDR_MODE_SHIFT) |
1006 (TEXCOORDMODE_CLAMP_BORDER << SS3_TCY_ADDR_MODE_SHIFT);
1007 case CAIRO_EXTEND_REPEAT:
1009 (TEXCOORDMODE_WRAP << SS3_TCX_ADDR_MODE_SHIFT) |
1010 (TEXCOORDMODE_WRAP << SS3_TCY_ADDR_MODE_SHIFT);
1011 case CAIRO_EXTEND_PAD:
1013 (TEXCOORDMODE_CLAMP_EDGE << SS3_TCX_ADDR_MODE_SHIFT) |
1014 (TEXCOORDMODE_CLAMP_EDGE << SS3_TCY_ADDR_MODE_SHIFT);
1015 case CAIRO_EXTEND_REFLECT:
1017 (TEXCOORDMODE_MIRROR << SS3_TCX_ADDR_MODE_SHIFT) |
1018 (TEXCOORDMODE_MIRROR << SS3_TCY_ADDR_MODE_SHIFT);
1022 static inline uint32_t cairo_const
1023 BUF_tiling (uint32_t tiling)
1027 case I915_TILING_NONE: return 0;
1028 case I915_TILING_X: return BUF_3D_TILED_SURFACE | BUF_3D_TILE_WALK_X;
1029 case I915_TILING_Y: return BUF_3D_TILED_SURFACE | BUF_3D_TILE_WALK_Y;
1033 #define OUT_DWORD(dword) i915_batch_emit_dword (device, dword)
1034 #define OUT_RELOC(surface, read, write) i915_batch_emit_reloc (device, to_intel_bo (surface->intel.drm.bo), surface->offset, read, write, FALSE)
1035 #define OUT_RELOC_FENCED(surface, read, write) i915_batch_emit_reloc (device, to_intel_bo (surface->intel.drm.bo), surface->offset, read, write, TRUE)
1038 uint32_t *_shader_start
1040 #define FS_BEGIN() \
1042 _shader_start = BATCH_PTR (device); \
1043 OUT_DWORD (_3DSTATE_PIXEL_SHADER_PROGRAM); \
1048 *_shader_start |= BATCH_PTR (device) - _shader_start - 2; \
1051 static inline int32_t
1052 i915_batch_space (i915_device_t *device)
1054 /* leave room for RECTLIST(4) + MI_BUFFER_END + MI_NOOP */
1055 return sizeof (device->batch_base) - (device->batch.used << 2) - 32;
1058 static inline cairo_bool_t
1059 i915_check_aperture_size (const i915_device_t *device, int relocs, size_t est_size, size_t size)
1061 return device->batch.reloc_count + relocs < I915_MAX_RELOCS - 2 &&
1062 device->batch.est_gtt_size + est_size <= device->batch.gtt_avail_size &&
1063 device->batch.total_gtt_size + size <= device->intel.gtt_avail_size;
1066 static inline cairo_bool_t
1067 i915_check_aperture (const i915_device_t *device, intel_bo_t **bo_array, int count)
1069 uint32_t relocs = 0, est_size = 0, size = 0;
1072 const intel_bo_t *bo = *bo_array++;
1073 if (bo->exec == NULL) {
1075 size += bo->base.size;
1077 est_size += bo->base.size;
1081 return i915_check_aperture_size (device, relocs, est_size, size);
1084 static inline cairo_bool_t
1085 i915_check_aperture_and_fences (const i915_device_t *device, intel_bo_t **bo_array, int count)
1087 uint32_t relocs = 0, est_size = 0, size = 0;
1088 uint32_t fences = 0;
1091 const intel_bo_t *bo = *bo_array++;
1092 if (bo->exec == NULL) {
1094 size += bo->base.size;
1096 est_size += bo->base.size;
1097 if (bo->tiling != I915_TILING_NONE)
1099 } else if (bo->tiling != I915_TILING_NONE) {
1100 if ((bo->exec->flags & EXEC_OBJECT_NEEDS_FENCE) == 0)
1105 return i915_check_aperture_size (device, relocs, est_size, size) &&
1106 device->batch.fences + fences <= device->batch.fences_avail;
1109 #define BATCH_PTR(device) &(device)->batch_base[(device)->batch.used]
1111 i915_batch_emit_dword (i915_device_t *device, uint32_t dword)
1113 device->batch_base[device->batch.used++] = dword;
1117 i915_batch_add_reloc (i915_device_t *device, uint32_t pos,
1120 uint32_t read_domains,
1121 uint32_t write_domain,
1122 cairo_bool_t needs_fence);
1125 i915_batch_fill_reloc (i915_device_t *device, uint32_t pos,
1128 uint32_t read_domains,
1129 uint32_t write_domain)
1131 i915_batch_add_reloc (device, pos,
1133 read_domains, write_domain,
1135 device->batch_base[pos] = bo->offset + offset;
1139 i915_batch_emit_reloc (i915_device_t *device,
1142 uint32_t read_domains,
1143 uint32_t write_domain,
1144 cairo_bool_t needs_fence)
1146 i915_batch_add_reloc (device, device->batch.used,
1148 read_domains, write_domain,
1150 i915_batch_emit_dword (device, bo->offset + offset);
1154 i915_vbo_flush (i915_device_t *device);
1157 i915_vbo_finish (i915_device_t *device);
1159 cairo_private cairo_status_t
1160 i915_batch_flush (i915_device_t *device);
1162 static inline float *
1163 i915_add_rectangle (i915_device_t *device)
1168 assert (device->floats_per_vertex);
1169 assert (device->rectangle_size == 3*device->floats_per_vertex*sizeof(float));
1171 size = device->rectangle_size;
1172 if (unlikely (device->vbo_offset + size > I915_VBO_SIZE))
1173 i915_vbo_finish (device);
1175 vertices = (float *) (device->vbo_base + device->vbo_offset);
1176 device->vbo_used = device->vbo_offset += size;
1177 device->vertex_count += 3;
1181 static inline i915_device_t *
1182 i915_device (i915_surface_t *surface)
1184 return (i915_device_t *) surface->intel.drm.base.device;
1187 cairo_private cairo_status_t
1188 i915_surface_clear (i915_surface_t *dst);
1191 i915_set_dst (i915_device_t *device, i915_surface_t *dst);
1194 i915_shader_init (i915_shader_t *shader,
1195 i915_surface_t *dst,
1196 cairo_operator_t op,
1199 cairo_private cairo_status_t
1200 i915_shader_acquire_pattern (i915_shader_t *shader,
1201 union i915_shader_channel *src,
1202 const cairo_pattern_t *pattern,
1203 const cairo_rectangle_int_t *extents);
1206 i915_shader_set_clip (i915_shader_t *shader,
1207 cairo_clip_t *clip);
1210 i915_shader_num_texcoords (const i915_shader_t *shader);
1212 static inline double cairo_const
1213 i915_shader_linear_texcoord (const struct i915_shader_linear *l,
1214 double src_x, double src_y)
1216 return l->dx * src_x + l->dy * src_y + l->offset;
1219 cairo_private cairo_status_t
1220 i915_shader_commit (i915_shader_t *shader,
1221 i915_device_t *device);
1224 i915_shader_fini (i915_shader_t *shader);
1226 cairo_private cairo_status_t
1227 i915_fixup_unbounded (i915_surface_t *dst,
1228 const cairo_composite_rectangles_t *extents,
1229 cairo_clip_t *clip);
1231 static inline cairo_bool_t
1232 i915_surface_needs_tiling (i915_surface_t *dst)
1234 return dst->intel.drm.width > 2048 || dst->intel.drm.height > 2048;
1237 cairo_private cairo_status_t
1238 i915_surface_copy_subimage (i915_device_t *device,
1239 i915_surface_t *src,
1240 const cairo_rectangle_int_t *extents,
1242 i915_surface_t **clone_out);
1244 static inline uint32_t
1245 pack_float (float f)
1255 static inline cairo_status_t
1256 i915_surface_fallback_flush (i915_surface_t *surface)
1258 cairo_status_t status;
1260 if (unlikely (surface->intel.drm.fallback != NULL))
1261 return intel_surface_flush (&surface->intel, 0);
1263 status = CAIRO_STATUS_SUCCESS;
1264 if (unlikely (surface->deferred_clear))
1265 status = i915_surface_clear (surface);
1270 #endif /* CAIRO_DRM_I915_PRIVATE_H */