2 * Copyright © 2015 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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 #include "anv_private.h"
32 #include "genxml/gen_macros.h"
33 #include "genxml/genX_pack.h"
36 clamp_int64(int64_t x
, int64_t min
, int64_t max
)
46 #if GEN_GEN == 7 && !GEN_IS_HASWELL
48 gen7_cmd_buffer_emit_scissor(struct anv_cmd_buffer
*cmd_buffer
)
50 uint32_t count
= cmd_buffer
->state
.dynamic
.scissor
.count
;
51 const VkRect2D
*scissors
= cmd_buffer
->state
.dynamic
.scissor
.scissors
;
52 struct anv_state scissor_state
=
53 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, count
* 8, 32);
55 for (uint32_t i
= 0; i
< count
; i
++) {
56 const VkRect2D
*s
= &scissors
[i
];
58 /* Since xmax and ymax are inclusive, we have to have xmax < xmin or
59 * ymax < ymin for empty clips. In case clip x, y, width height are all
60 * 0, the clamps below produce 0 for xmin, ymin, xmax, ymax, which isn't
61 * what we want. Just special case empty clips and produce a canonical
63 static const struct GEN7_SCISSOR_RECT empty_scissor
= {
64 .ScissorRectangleYMin
= 1,
65 .ScissorRectangleXMin
= 1,
66 .ScissorRectangleYMax
= 0,
67 .ScissorRectangleXMax
= 0
70 const int max
= 0xffff;
71 struct GEN7_SCISSOR_RECT scissor
= {
72 /* Do this math using int64_t so overflow gets clamped correctly. */
73 .ScissorRectangleYMin
= clamp_int64(s
->offset
.y
, 0, max
),
74 .ScissorRectangleXMin
= clamp_int64(s
->offset
.x
, 0, max
),
75 .ScissorRectangleYMax
= clamp_int64((uint64_t) s
->offset
.y
+ s
->extent
.height
- 1, 0, max
),
76 .ScissorRectangleXMax
= clamp_int64((uint64_t) s
->offset
.x
+ s
->extent
.width
- 1, 0, max
)
79 if (s
->extent
.width
<= 0 || s
->extent
.height
<= 0) {
80 GEN7_SCISSOR_RECT_pack(NULL
, scissor_state
.map
+ i
* 8,
83 GEN7_SCISSOR_RECT_pack(NULL
, scissor_state
.map
+ i
* 8, &scissor
);
87 anv_batch_emit(&cmd_buffer
->batch
,
88 GEN7_3DSTATE_SCISSOR_STATE_POINTERS
, ssp
) {
89 ssp
.ScissorRectPointer
= scissor_state
.offset
;
92 if (!cmd_buffer
->device
->info
.has_llc
)
93 anv_state_clflush(scissor_state
);
97 static const uint32_t vk_to_gen_index_type
[] = {
98 [VK_INDEX_TYPE_UINT16
] = INDEX_WORD
,
99 [VK_INDEX_TYPE_UINT32
] = INDEX_DWORD
,
102 static const uint32_t restart_index_for_type
[] = {
103 [VK_INDEX_TYPE_UINT16
] = UINT16_MAX
,
104 [VK_INDEX_TYPE_UINT32
] = UINT32_MAX
,
107 void genX(CmdBindIndexBuffer
)(
108 VkCommandBuffer commandBuffer
,
111 VkIndexType indexType
)
113 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
114 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
116 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_INDEX_BUFFER
;
118 cmd_buffer
->state
.restart_index
= restart_index_for_type
[indexType
];
119 cmd_buffer
->state
.gen7
.index_buffer
= buffer
;
120 cmd_buffer
->state
.gen7
.index_type
= vk_to_gen_index_type
[indexType
];
121 cmd_buffer
->state
.gen7
.index_offset
= offset
;
125 genX(cmd_buffer_flush_dynamic_state
)(struct anv_cmd_buffer
*cmd_buffer
)
127 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
129 if (cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_PIPELINE
|
130 ANV_CMD_DIRTY_RENDER_TARGETS
|
131 ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH
|
132 ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS
)) {
134 const struct anv_image_view
*iview
=
135 anv_cmd_buffer_get_depth_stencil_view(cmd_buffer
);
136 const struct anv_image
*image
= iview
? iview
->image
: NULL
;
137 const bool has_depth
=
138 image
&& (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
);
139 const uint32_t depth_format
= has_depth
?
140 isl_surf_get_depth_format(&cmd_buffer
->device
->isl_dev
,
141 &image
->depth_surface
.isl
) : D16_UNORM
;
143 uint32_t sf_dw
[GENX(3DSTATE_SF_length
)];
144 struct GENX(3DSTATE_SF
) sf
= {
145 GENX(3DSTATE_SF_header
),
146 .DepthBufferSurfaceFormat
= depth_format
,
147 .LineWidth
= cmd_buffer
->state
.dynamic
.line_width
,
148 .GlobalDepthOffsetConstant
= cmd_buffer
->state
.dynamic
.depth_bias
.bias
,
149 .GlobalDepthOffsetScale
= cmd_buffer
->state
.dynamic
.depth_bias
.slope
,
150 .GlobalDepthOffsetClamp
= cmd_buffer
->state
.dynamic
.depth_bias
.clamp
152 GENX(3DSTATE_SF_pack
)(NULL
, sf_dw
, &sf
);
154 anv_batch_emit_merge(&cmd_buffer
->batch
, sf_dw
, pipeline
->gen7
.sf
);
157 if (cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS
|
158 ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE
)) {
159 struct anv_dynamic_state
*d
= &cmd_buffer
->state
.dynamic
;
160 struct anv_state cc_state
=
161 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
162 GENX(COLOR_CALC_STATE_length
) * 4,
164 struct GENX(COLOR_CALC_STATE
) cc
= {
165 .BlendConstantColorRed
= cmd_buffer
->state
.dynamic
.blend_constants
[0],
166 .BlendConstantColorGreen
= cmd_buffer
->state
.dynamic
.blend_constants
[1],
167 .BlendConstantColorBlue
= cmd_buffer
->state
.dynamic
.blend_constants
[2],
168 .BlendConstantColorAlpha
= cmd_buffer
->state
.dynamic
.blend_constants
[3],
169 .StencilReferenceValue
= d
->stencil_reference
.front
& 0xff,
170 .BackfaceStencilReferenceValue
= d
->stencil_reference
.back
& 0xff,
172 GENX(COLOR_CALC_STATE_pack
)(NULL
, cc_state
.map
, &cc
);
173 if (!cmd_buffer
->device
->info
.has_llc
)
174 anv_state_clflush(cc_state
);
176 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_CC_STATE_POINTERS
), ccp
) {
177 ccp
.ColorCalcStatePointer
= cc_state
.offset
;
181 if (cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_PIPELINE
|
182 ANV_CMD_DIRTY_RENDER_TARGETS
|
183 ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK
|
184 ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK
)) {
185 uint32_t depth_stencil_dw
[GENX(DEPTH_STENCIL_STATE_length
)];
186 struct anv_dynamic_state
*d
= &cmd_buffer
->state
.dynamic
;
188 struct GENX(DEPTH_STENCIL_STATE
) depth_stencil
= {
189 .StencilTestMask
= d
->stencil_compare_mask
.front
& 0xff,
190 .StencilWriteMask
= d
->stencil_write_mask
.front
& 0xff,
192 .BackfaceStencilTestMask
= d
->stencil_compare_mask
.back
& 0xff,
193 .BackfaceStencilWriteMask
= d
->stencil_write_mask
.back
& 0xff,
195 GENX(DEPTH_STENCIL_STATE_pack
)(NULL
, depth_stencil_dw
, &depth_stencil
);
197 struct anv_state ds_state
=
198 anv_cmd_buffer_merge_dynamic(cmd_buffer
, depth_stencil_dw
,
199 pipeline
->gen7
.depth_stencil_state
,
200 GENX(DEPTH_STENCIL_STATE_length
), 64);
202 anv_batch_emit(&cmd_buffer
->batch
,
203 GENX(3DSTATE_DEPTH_STENCIL_STATE_POINTERS
), dsp
) {
204 dsp
.PointertoDEPTH_STENCIL_STATE
= ds_state
.offset
;
208 if (cmd_buffer
->state
.gen7
.index_buffer
&&
209 cmd_buffer
->state
.dirty
& (ANV_CMD_DIRTY_PIPELINE
|
210 ANV_CMD_DIRTY_INDEX_BUFFER
)) {
211 struct anv_buffer
*buffer
= cmd_buffer
->state
.gen7
.index_buffer
;
212 uint32_t offset
= cmd_buffer
->state
.gen7
.index_offset
;
215 anv_batch_emit(&cmd_buffer
->batch
, GEN75_3DSTATE_VF
, vf
) {
216 vf
.IndexedDrawCutIndexEnable
= pipeline
->primitive_restart
;
217 vf
.CutIndex
= cmd_buffer
->state
.restart_index
;
221 anv_batch_emit(&cmd_buffer
->batch
, GENX(3DSTATE_INDEX_BUFFER
), ib
) {
223 ib
.CutIndexEnable
= pipeline
->primitive_restart
;
225 ib
.IndexFormat
= cmd_buffer
->state
.gen7
.index_type
;
226 ib
.MemoryObjectControlState
= GENX(MOCS
);
228 ib
.BufferStartingAddress
=
229 (struct anv_address
) { buffer
->bo
, buffer
->offset
+ offset
};
230 ib
.BufferEndingAddress
=
231 (struct anv_address
) { buffer
->bo
, buffer
->offset
+ buffer
->size
};
235 cmd_buffer
->state
.dirty
= 0;
239 genX(cmd_buffer_emit_hz_op
)(struct anv_cmd_buffer
*cmd_buffer
,
240 enum blorp_hiz_op op
)
242 anv_finishme("Implement Gen7 HZ ops");
245 void genX(CmdSetEvent
)(
246 VkCommandBuffer commandBuffer
,
248 VkPipelineStageFlags stageMask
)
253 void genX(CmdResetEvent
)(
254 VkCommandBuffer commandBuffer
,
256 VkPipelineStageFlags stageMask
)
261 void genX(CmdWaitEvents
)(
262 VkCommandBuffer commandBuffer
,
264 const VkEvent
* pEvents
,
265 VkPipelineStageFlags srcStageMask
,
266 VkPipelineStageFlags destStageMask
,
267 uint32_t memoryBarrierCount
,
268 const VkMemoryBarrier
* pMemoryBarriers
,
269 uint32_t bufferMemoryBarrierCount
,
270 const VkBufferMemoryBarrier
* pBufferMemoryBarriers
,
271 uint32_t imageMemoryBarrierCount
,
272 const VkImageMemoryBarrier
* pImageMemoryBarriers
)
276 genX(CmdPipelineBarrier
)(commandBuffer
, srcStageMask
, destStageMask
,
277 false, /* byRegion */
278 memoryBarrierCount
, pMemoryBarriers
,
279 bufferMemoryBarrierCount
, pBufferMemoryBarriers
,
280 imageMemoryBarrierCount
, pImageMemoryBarriers
);