Path: blob/21.2-virgl/src/gallium/drivers/r300/r300_render.c
4570 views
/*1* Copyright 2009 Corbin Simpson <[email protected]>2* Copyright 2010 Marek Olšák <[email protected]>3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* on the rights to use, copy, modify, merge, publish, distribute, sub8* license, and/or sell copies of the Software, and to permit persons to whom9* the Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL18* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,19* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR20* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE21* USE OR OTHER DEALINGS IN THE SOFTWARE. */2223/* r300_render: Vertex and index buffer primitive emission. Contains both24* HW TCL fastpath rendering, and SW TCL Draw-assisted rendering. */2526#include "draw/draw_context.h"27#include "draw/draw_vbuf.h"2829#include "util/u_inlines.h"3031#include "util/format/u_format.h"32#include "util/u_draw.h"33#include "util/u_memory.h"34#include "util/u_upload_mgr.h"35#include "util/u_prim.h"3637#include "r300_cs.h"38#include "r300_context.h"39#include "r300_screen_buffer.h"40#include "r300_emit.h"41#include "r300_reg.h"4243#include <limits.h>4445#define IMMD_DWORDS 324647static uint32_t r300_translate_primitive(unsigned prim)48{49static const int prim_conv[] = {50R300_VAP_VF_CNTL__PRIM_POINTS,51R300_VAP_VF_CNTL__PRIM_LINES,52R300_VAP_VF_CNTL__PRIM_LINE_LOOP,53R300_VAP_VF_CNTL__PRIM_LINE_STRIP,54R300_VAP_VF_CNTL__PRIM_TRIANGLES,55R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP,56R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN,57R300_VAP_VF_CNTL__PRIM_QUADS,58R300_VAP_VF_CNTL__PRIM_QUAD_STRIP,59R300_VAP_VF_CNTL__PRIM_POLYGON,60-1,61-1,62-1,63-164};65unsigned hwprim = prim_conv[prim];6667assert(hwprim != -1);68return hwprim;69}7071static uint32_t r300_provoking_vertex_fixes(struct r300_context *r300,72unsigned mode)73{74struct r300_rs_state* rs = (struct r300_rs_state*)r300->rs_state.state;75uint32_t color_control = rs->color_control;7677/* By default (see r300_state.c:r300_create_rs_state) color_control is78* initialized to provoking the first vertex.79*80* Triangle fans must be reduced to the second vertex, not the first, in81* Gallium flatshade-first mode, as per the GL spec.82* (http://www.opengl.org/registry/specs/ARB/provoking_vertex.txt)83*84* Quads never provoke correctly in flatshade-first mode. The first85* vertex is never considered as provoking, so only the second, third,86* and fourth vertices can be selected, and both "third" and "last" modes87* select the fourth vertex. This is probably due to D3D lacking quads.88*89* Similarly, polygons reduce to the first, not the last, vertex, when in90* "last" mode, and all other modes start from the second vertex.91*92* ~ C.93*/9495if (rs->rs.flatshade_first) {96switch (mode) {97case PIPE_PRIM_TRIANGLE_FAN:98color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_SECOND;99break;100case PIPE_PRIM_QUADS:101case PIPE_PRIM_QUAD_STRIP:102case PIPE_PRIM_POLYGON:103color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;104break;105default:106color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_FIRST;107break;108}109} else {110color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;111}112113return color_control;114}115116void r500_emit_index_bias(struct r300_context *r300, int index_bias)117{118CS_LOCALS(r300);119120BEGIN_CS(2);121OUT_CS_REG(R500_VAP_INDEX_OFFSET,122(index_bias & 0xFFFFFF) | (index_bias < 0 ? 1<<24 : 0));123END_CS;124}125126static void r300_emit_draw_init(struct r300_context *r300, unsigned mode,127unsigned max_index)128{129CS_LOCALS(r300);130131assert(max_index < (1 << 24));132133BEGIN_CS(5);134OUT_CS_REG(R300_GA_COLOR_CONTROL,135r300_provoking_vertex_fixes(r300, mode));136OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);137OUT_CS(max_index);138OUT_CS(0);139END_CS;140}141142/* This function splits the index bias value into two parts:143* - buffer_offset: the value that can be safely added to buffer offsets144* in r300_emit_vertex_arrays (it must yield a positive offset when added to145* a vertex buffer offset)146* - index_offset: the value that must be manually subtracted from indices147* in an index buffer to achieve negative offsets. */148static void r300_split_index_bias(struct r300_context *r300, int index_bias,149int *buffer_offset, int *index_offset)150{151struct pipe_vertex_buffer *vb, *vbufs = r300->vertex_buffer;152struct pipe_vertex_element *velem = r300->velems->velem;153unsigned i, size;154int max_neg_bias;155156if (index_bias < 0) {157/* See how large index bias we may subtract. We must be careful158* here because negative buffer offsets are not allowed159* by the DRM API. */160max_neg_bias = INT_MAX;161for (i = 0; i < r300->velems->count; i++) {162vb = &vbufs[velem[i].vertex_buffer_index];163size = (vb->buffer_offset + velem[i].src_offset) / vb->stride;164max_neg_bias = MIN2(max_neg_bias, size);165}166167/* Now set the minimum allowed value. */168*buffer_offset = MAX2(-max_neg_bias, index_bias);169} else {170/* A positive index bias is OK. */171*buffer_offset = index_bias;172}173174*index_offset = index_bias - *buffer_offset;175}176177enum r300_prepare_flags {178PREP_EMIT_STATES = (1 << 0), /* call emit_dirty_state and friends? */179PREP_VALIDATE_VBOS = (1 << 1), /* validate VBOs? */180PREP_EMIT_VARRAYS = (1 << 2), /* call emit_vertex_arrays? */181PREP_EMIT_VARRAYS_SWTCL = (1 << 3), /* call emit_vertex_arrays_swtcl? */182PREP_INDEXED = (1 << 4) /* is this draw_elements? */183};184185/**186* Check if the requested number of dwords is available in the CS and187* if not, flush.188* \param r300 The context.189* \param flags See r300_prepare_flags.190* \param cs_dwords The number of dwords to reserve in CS.191* \return TRUE if the CS was flushed192*/193static boolean r300_reserve_cs_dwords(struct r300_context *r300,194enum r300_prepare_flags flags,195unsigned cs_dwords)196{197boolean flushed = FALSE;198boolean emit_states = flags & PREP_EMIT_STATES;199boolean emit_vertex_arrays = flags & PREP_EMIT_VARRAYS;200boolean emit_vertex_arrays_swtcl = flags & PREP_EMIT_VARRAYS_SWTCL;201202/* Add dirty state, index offset, and AOS. */203if (emit_states)204cs_dwords += r300_get_num_dirty_dwords(r300);205206if (r300->screen->caps.is_r500)207cs_dwords += 2; /* emit_index_offset */208209if (emit_vertex_arrays)210cs_dwords += 55; /* emit_vertex_arrays */211212if (emit_vertex_arrays_swtcl)213cs_dwords += 7; /* emit_vertex_arrays_swtcl */214215cs_dwords += r300_get_num_cs_end_dwords(r300);216217/* Reserve requested CS space. */218if (!r300->rws->cs_check_space(&r300->cs, cs_dwords, false)) {219r300_flush(&r300->context, PIPE_FLUSH_ASYNC, NULL);220flushed = TRUE;221}222223return flushed;224}225226/**227* Validate buffers and emit dirty state.228* \param r300 The context.229* \param flags See r300_prepare_flags.230* \param index_buffer The index buffer to validate. The parameter may be NULL.231* \param buffer_offset The offset passed to emit_vertex_arrays.232* \param index_bias The index bias to emit.233* \param instance_id Index of instance to render234* \return TRUE if rendering should be skipped235*/236static boolean r300_emit_states(struct r300_context *r300,237enum r300_prepare_flags flags,238struct pipe_resource *index_buffer,239int buffer_offset,240int index_bias, int instance_id)241{242boolean emit_states = flags & PREP_EMIT_STATES;243boolean emit_vertex_arrays = flags & PREP_EMIT_VARRAYS;244boolean emit_vertex_arrays_swtcl = flags & PREP_EMIT_VARRAYS_SWTCL;245boolean indexed = flags & PREP_INDEXED;246boolean validate_vbos = flags & PREP_VALIDATE_VBOS;247248/* Validate buffers and emit dirty state if needed. */249if (emit_states || (emit_vertex_arrays && validate_vbos)) {250if (!r300_emit_buffer_validate(r300, validate_vbos,251index_buffer)) {252fprintf(stderr, "r300: CS space validation failed. "253"(not enough memory?) Skipping rendering.\n");254return FALSE;255}256}257258if (emit_states)259r300_emit_dirty_state(r300);260261if (r300->screen->caps.is_r500) {262if (r300->screen->caps.has_tcl)263r500_emit_index_bias(r300, index_bias);264else265r500_emit_index_bias(r300, 0);266}267268if (emit_vertex_arrays &&269(r300->vertex_arrays_dirty ||270r300->vertex_arrays_indexed != indexed ||271r300->vertex_arrays_offset != buffer_offset ||272r300->vertex_arrays_instance_id != instance_id)) {273r300_emit_vertex_arrays(r300, buffer_offset, indexed, instance_id);274275r300->vertex_arrays_dirty = FALSE;276r300->vertex_arrays_indexed = indexed;277r300->vertex_arrays_offset = buffer_offset;278r300->vertex_arrays_instance_id = instance_id;279}280281if (emit_vertex_arrays_swtcl)282r300_emit_vertex_arrays_swtcl(r300, indexed);283284return TRUE;285}286287/**288* Check if the requested number of dwords is available in the CS and289* if not, flush. Then validate buffers and emit dirty state.290* \param r300 The context.291* \param flags See r300_prepare_flags.292* \param index_buffer The index buffer to validate. The parameter may be NULL.293* \param cs_dwords The number of dwords to reserve in CS.294* \param buffer_offset The offset passed to emit_vertex_arrays.295* \param index_bias The index bias to emit.296* \param instance_id The instance to render.297* \return TRUE if rendering should be skipped298*/299static boolean r300_prepare_for_rendering(struct r300_context *r300,300enum r300_prepare_flags flags,301struct pipe_resource *index_buffer,302unsigned cs_dwords,303int buffer_offset,304int index_bias,305int instance_id)306{307/* Make sure there is enough space in the command stream and emit states. */308if (r300_reserve_cs_dwords(r300, flags, cs_dwords))309flags |= PREP_EMIT_STATES;310311return r300_emit_states(r300, flags, index_buffer, buffer_offset,312index_bias, instance_id);313}314315static boolean immd_is_good_idea(struct r300_context *r300,316unsigned count)317{318if (DBG_ON(r300, DBG_NO_IMMD)) {319return FALSE;320}321322if (count * r300->velems->vertex_size_dwords > IMMD_DWORDS) {323return FALSE;324}325326/* Buffers can only be used for read by r300 (except query buffers, but327* those can't be bound by an gallium frontend as vertex buffers). */328return TRUE;329}330331/*****************************************************************************332* The HWTCL draw functions. *333****************************************************************************/334335static void r300_draw_arrays_immediate(struct r300_context *r300,336const struct pipe_draw_info *info,337const struct pipe_draw_start_count_bias *draw)338{339struct pipe_vertex_element* velem;340struct pipe_vertex_buffer* vbuf;341unsigned vertex_element_count = r300->velems->count;342unsigned i, v, vbi;343344/* Size of the vertex, in dwords. */345unsigned vertex_size = r300->velems->vertex_size_dwords;346347/* The number of dwords for this draw operation. */348unsigned dwords = 4 + draw->count * vertex_size;349350/* Size of the vertex element, in dwords. */351unsigned size[PIPE_MAX_ATTRIBS];352353/* Stride to the same attrib in the next vertex in the vertex buffer,354* in dwords. */355unsigned stride[PIPE_MAX_ATTRIBS];356357/* Mapped vertex buffers. */358uint32_t* map[PIPE_MAX_ATTRIBS] = {0};359uint32_t* mapelem[PIPE_MAX_ATTRIBS];360361CS_LOCALS(r300);362363if (!r300_prepare_for_rendering(r300, PREP_EMIT_STATES, NULL, dwords, 0, 0, -1))364return;365366/* Calculate the vertex size, offsets, strides etc. and map the buffers. */367for (i = 0; i < vertex_element_count; i++) {368velem = &r300->velems->velem[i];369size[i] = r300->velems->format_size[i] / 4;370vbi = velem->vertex_buffer_index;371vbuf = &r300->vertex_buffer[vbi];372stride[i] = vbuf->stride / 4;373374/* Map the buffer. */375if (!map[vbi]) {376map[vbi] = (uint32_t*)r300->rws->buffer_map(r300->rws,377r300_resource(vbuf->buffer.resource)->buf,378&r300->cs, PIPE_MAP_READ | PIPE_MAP_UNSYNCHRONIZED);379map[vbi] += (vbuf->buffer_offset / 4) + stride[i] * draw->start;380}381mapelem[i] = map[vbi] + (velem->src_offset / 4);382}383384r300_emit_draw_init(r300, info->mode, draw->count-1);385386BEGIN_CS(dwords);387OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size);388OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, draw->count * vertex_size);389OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (draw->count << 16) |390r300_translate_primitive(info->mode));391392/* Emit vertices. */393for (v = 0; v < draw->count; v++) {394for (i = 0; i < vertex_element_count; i++) {395OUT_CS_TABLE(&mapelem[i][stride[i] * v], size[i]);396}397}398END_CS;399}400401static void r300_emit_draw_arrays(struct r300_context *r300,402unsigned mode,403unsigned count)404{405boolean alt_num_verts = count > 65535;406CS_LOCALS(r300);407408if (count >= (1 << 24)) {409fprintf(stderr, "r300: Got a huge number of vertices: %i, "410"refusing to render.\n", count);411return;412}413414r300_emit_draw_init(r300, mode, count-1);415416BEGIN_CS(2 + (alt_num_verts ? 2 : 0));417if (alt_num_verts) {418OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);419}420OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);421OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |422r300_translate_primitive(mode) |423(alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));424END_CS;425}426427static void r300_emit_draw_elements(struct r300_context *r300,428struct pipe_resource* indexBuffer,429unsigned indexSize,430unsigned max_index,431unsigned mode,432unsigned start,433unsigned count,434uint16_t *imm_indices3)435{436uint32_t count_dwords, offset_dwords;437boolean alt_num_verts = count > 65535;438CS_LOCALS(r300);439440if (count >= (1 << 24)) {441fprintf(stderr, "r300: Got a huge number of vertices: %i, "442"refusing to render (max_index: %i).\n", count, max_index);443return;444}445446DBG(r300, DBG_DRAW, "r300: Indexbuf of %u indices, max %u\n",447count, max_index);448449r300_emit_draw_init(r300, mode, max_index);450451/* If start is odd, render the first triangle with indices embedded452* in the command stream. This will increase start by 3 and make it453* even. We can then proceed without a fallback. */454if (indexSize == 2 && (start & 1) &&455mode == PIPE_PRIM_TRIANGLES) {456BEGIN_CS(4);457OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 2);458OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (3 << 16) |459R300_VAP_VF_CNTL__PRIM_TRIANGLES);460OUT_CS(imm_indices3[1] << 16 | imm_indices3[0]);461OUT_CS(imm_indices3[2]);462END_CS;463464start += 3;465count -= 3;466if (!count)467return;468}469470offset_dwords = indexSize * start / sizeof(uint32_t);471472BEGIN_CS(8 + (alt_num_verts ? 2 : 0));473if (alt_num_verts) {474OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);475}476OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 0);477if (indexSize == 4) {478count_dwords = count;479OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |480R300_VAP_VF_CNTL__INDEX_SIZE_32bit |481r300_translate_primitive(mode) |482(alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));483} else {484count_dwords = (count + 1) / 2;485OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |486r300_translate_primitive(mode) |487(alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));488}489490OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER, 2);491OUT_CS(R300_INDX_BUFFER_ONE_REG_WR | (R300_VAP_PORT_IDX0 >> 2) |492(0 << R300_INDX_BUFFER_SKIP_SHIFT));493OUT_CS(offset_dwords << 2);494OUT_CS(count_dwords);495OUT_CS_RELOC(r300_resource(indexBuffer));496END_CS;497}498499static void r300_draw_elements_immediate(struct r300_context *r300,500const struct pipe_draw_info *info,501const struct pipe_draw_start_count_bias *draw)502{503const uint8_t *ptr1;504const uint16_t *ptr2;505const uint32_t *ptr4;506unsigned index_size = info->index_size;507unsigned i, count_dwords = index_size == 4 ? draw->count :508(draw->count + 1) / 2;509CS_LOCALS(r300);510511/* 19 dwords for r300_draw_elements_immediate. Give up if the function fails. */512if (!r300_prepare_for_rendering(r300,513PREP_EMIT_STATES | PREP_VALIDATE_VBOS | PREP_EMIT_VARRAYS |514PREP_INDEXED, NULL, 2+count_dwords, 0, draw->index_bias, -1))515return;516517r300_emit_draw_init(r300, info->mode, info->max_index);518519BEGIN_CS(2 + count_dwords);520OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, count_dwords);521522switch (index_size) {523case 1:524ptr1 = (uint8_t*)info->index.user;525ptr1 += draw->start;526527OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (draw->count << 16) |528r300_translate_primitive(info->mode));529530if (draw->index_bias && !r300->screen->caps.is_r500) {531for (i = 0; i < draw->count-1; i += 2)532OUT_CS(((ptr1[i+1] + draw->index_bias) << 16) |533(ptr1[i] + draw->index_bias));534535if (draw->count & 1)536OUT_CS(ptr1[i] + draw->index_bias);537} else {538for (i = 0; i < draw->count-1; i += 2)539OUT_CS(((ptr1[i+1]) << 16) |540(ptr1[i] ));541542if (draw->count & 1)543OUT_CS(ptr1[i]);544}545break;546547case 2:548ptr2 = (uint16_t*)info->index.user;549ptr2 += draw->start;550551OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (draw->count << 16) |552r300_translate_primitive(info->mode));553554if (draw->index_bias && !r300->screen->caps.is_r500) {555for (i = 0; i < draw->count-1; i += 2)556OUT_CS(((ptr2[i+1] + draw->index_bias) << 16) |557(ptr2[i] + draw->index_bias));558559if (draw->count & 1)560OUT_CS(ptr2[i] + draw->index_bias);561} else {562OUT_CS_TABLE(ptr2, count_dwords);563}564break;565566case 4:567ptr4 = (uint32_t*)info->index.user;568ptr4 += draw->start;569570OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (draw->count << 16) |571R300_VAP_VF_CNTL__INDEX_SIZE_32bit |572r300_translate_primitive(info->mode));573574if (draw->index_bias && !r300->screen->caps.is_r500) {575for (i = 0; i < draw->count; i++)576OUT_CS(ptr4[i] + draw->index_bias);577} else {578OUT_CS_TABLE(ptr4, count_dwords);579}580break;581}582END_CS;583}584585static void r300_draw_elements(struct r300_context *r300,586const struct pipe_draw_info *info,587const struct pipe_draw_start_count_bias *draw,588int instance_id)589{590struct pipe_resource *indexBuffer =591info->has_user_indices ? NULL : info->index.resource;592unsigned indexSize = info->index_size;593struct pipe_resource* orgIndexBuffer = indexBuffer;594unsigned start = draw->start;595unsigned count = draw->count;596boolean alt_num_verts = r300->screen->caps.is_r500 &&597count > 65536;598unsigned short_count;599int buffer_offset = 0, index_offset = 0; /* for index bias emulation */600uint16_t indices3[3];601602if (draw->index_bias && !r300->screen->caps.is_r500) {603r300_split_index_bias(r300, draw->index_bias, &buffer_offset,604&index_offset);605}606607r300_translate_index_buffer(r300, info, &indexBuffer,608&indexSize, index_offset, &start, count);609610/* Fallback for misaligned ushort indices. */611if (indexSize == 2 && (start & 1) && indexBuffer) {612/* If we got here, then orgIndexBuffer == indexBuffer. */613uint16_t *ptr = r300->rws->buffer_map(r300->rws, r300_resource(orgIndexBuffer)->buf,614&r300->cs,615PIPE_MAP_READ |616PIPE_MAP_UNSYNCHRONIZED);617618if (info->mode == PIPE_PRIM_TRIANGLES) {619memcpy(indices3, ptr + start, 6);620} else {621/* Copy the mapped index buffer directly to the upload buffer.622* The start index will be aligned simply from the fact that623* every sub-buffer in the upload buffer is aligned. */624r300_upload_index_buffer(r300, &indexBuffer, indexSize, &start,625count, (uint8_t*)ptr);626}627} else {628if (info->has_user_indices)629r300_upload_index_buffer(r300, &indexBuffer, indexSize,630&start, count,631info->index.user);632}633634/* 19 dwords for emit_draw_elements. Give up if the function fails. */635if (!r300_prepare_for_rendering(r300,636PREP_EMIT_STATES | PREP_VALIDATE_VBOS | PREP_EMIT_VARRAYS |637PREP_INDEXED, indexBuffer, 19, buffer_offset, draw->index_bias,638instance_id))639goto done;640641if (alt_num_verts || count <= 65535) {642r300_emit_draw_elements(r300, indexBuffer, indexSize,643info->max_index, info->mode, start, count,644indices3);645} else {646do {647/* The maximum must be divisible by 4 and 3,648* so that quad and triangle lists are split correctly.649*650* Strips, loops, and fans won't work. */651short_count = MIN2(count, 65532);652653r300_emit_draw_elements(r300, indexBuffer, indexSize,654info->max_index,655info->mode, start, short_count, indices3);656657start += short_count;658count -= short_count;659660/* 15 dwords for emit_draw_elements */661if (count) {662if (!r300_prepare_for_rendering(r300,663PREP_VALIDATE_VBOS | PREP_EMIT_VARRAYS | PREP_INDEXED,664indexBuffer, 19, buffer_offset, draw->index_bias,665instance_id))666goto done;667}668} while (count);669}670671done:672if (indexBuffer != orgIndexBuffer) {673pipe_resource_reference( &indexBuffer, NULL );674}675}676677static void r300_draw_arrays(struct r300_context *r300,678const struct pipe_draw_info *info,679const struct pipe_draw_start_count_bias *draw,680int instance_id)681{682boolean alt_num_verts = r300->screen->caps.is_r500 &&683draw->count > 65536;684unsigned start = draw->start;685unsigned count = draw->count;686unsigned short_count;687688/* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */689if (!r300_prepare_for_rendering(r300,690PREP_EMIT_STATES | PREP_VALIDATE_VBOS | PREP_EMIT_VARRAYS,691NULL, 9, start, 0, instance_id))692return;693694if (alt_num_verts || count <= 65535) {695r300_emit_draw_arrays(r300, info->mode, count);696} else {697do {698/* The maximum must be divisible by 4 and 3,699* so that quad and triangle lists are split correctly.700*701* Strips, loops, and fans won't work. */702short_count = MIN2(count, 65532);703r300_emit_draw_arrays(r300, info->mode, short_count);704705start += short_count;706count -= short_count;707708/* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */709if (count) {710if (!r300_prepare_for_rendering(r300,711PREP_VALIDATE_VBOS | PREP_EMIT_VARRAYS, NULL, 9,712start, 0, instance_id))713return;714}715} while (count);716}717}718719static void r300_draw_arrays_instanced(struct r300_context *r300,720const struct pipe_draw_info *info,721const struct pipe_draw_start_count_bias *draw)722{723int i;724725for (i = 0; i < info->instance_count; i++)726r300_draw_arrays(r300, info, draw, i);727}728729static void r300_draw_elements_instanced(struct r300_context *r300,730const struct pipe_draw_info *info,731const struct pipe_draw_start_count_bias *draw)732{733int i;734735for (i = 0; i < info->instance_count; i++)736r300_draw_elements(r300, info, draw, i);737}738739static unsigned r300_max_vertex_count(struct r300_context *r300)740{741unsigned i, nr = r300->velems->count;742struct pipe_vertex_element *velems = r300->velems->velem;743unsigned result = ~0;744745for (i = 0; i < nr; i++) {746struct pipe_vertex_buffer *vb =747&r300->vertex_buffer[velems[i].vertex_buffer_index];748unsigned size, max_count, value;749750/* We're not interested in constant and per-instance attribs. */751if (!vb->buffer.resource ||752!vb->stride ||753velems[i].instance_divisor) {754continue;755}756757size = vb->buffer.resource->width0;758759/* Subtract buffer_offset. */760value = vb->buffer_offset;761if (value >= size) {762return 0;763}764size -= value;765766/* Subtract src_offset. */767value = velems[i].src_offset;768if (value >= size) {769return 0;770}771size -= value;772773/* Subtract format_size. */774value = r300->velems->format_size[i];775if (value >= size) {776return 0;777}778size -= value;779780/* Compute the max count. */781max_count = 1 + size / vb->stride;782result = MIN2(result, max_count);783}784return result;785}786787788static void r300_draw_vbo(struct pipe_context* pipe,789const struct pipe_draw_info *dinfo,790unsigned drawid_offset,791const struct pipe_draw_indirect_info *indirect,792const struct pipe_draw_start_count_bias *draws,793unsigned num_draws)794{795if (num_draws > 1) {796util_draw_multi(pipe, dinfo, drawid_offset, indirect, draws, num_draws);797return;798}799800struct r300_context* r300 = r300_context(pipe);801struct pipe_draw_info info = *dinfo;802struct pipe_draw_start_count_bias draw = draws[0];803804if (r300->skip_rendering ||805!u_trim_pipe_prim(info.mode, &draw.count)) {806return;807}808809r300_update_derived_state(r300);810811/* Draw. */812if (info.index_size) {813unsigned max_count = r300_max_vertex_count(r300);814815if (!max_count) {816fprintf(stderr, "r300: Skipping a draw command. There is a buffer "817" which is too small to be used for rendering.\n");818return;819}820821if (max_count == ~0) {822/* There are no per-vertex vertex elements. Use the hardware maximum. */823max_count = 0xffffff;824}825826info.max_index = max_count - 1;827828if (info.instance_count <= 1) {829if (draw.count <= 8 && info.has_user_indices) {830r300_draw_elements_immediate(r300, &info, &draw);831} else {832r300_draw_elements(r300, &info, &draw, -1);833}834} else {835r300_draw_elements_instanced(r300, &info, &draw);836}837} else {838if (info.instance_count <= 1) {839if (immd_is_good_idea(r300, draw.count)) {840r300_draw_arrays_immediate(r300, &info, &draw);841} else {842r300_draw_arrays(r300, &info, &draw, -1);843}844} else {845r300_draw_arrays_instanced(r300, &info, &draw);846}847}848}849850/****************************************************************************851* The rest of this file is for SW TCL rendering only. Please be polite and *852* keep these functions separated so that they are easier to locate. ~C. *853***************************************************************************/854855/* SW TCL elements, using Draw. */856static void r300_swtcl_draw_vbo(struct pipe_context* pipe,857const struct pipe_draw_info *info,858unsigned drawid_offset,859const struct pipe_draw_indirect_info *indirect,860const struct pipe_draw_start_count_bias *draws,861unsigned num_draws)862{863if (num_draws > 1) {864util_draw_multi(pipe, info, drawid_offset, indirect, draws, num_draws);865return;866}867868struct r300_context* r300 = r300_context(pipe);869struct pipe_draw_start_count_bias draw = draws[0];870871if (r300->skip_rendering) {872return;873}874875if (!u_trim_pipe_prim(info->mode, &draw.count))876return;877878if (info->index_size) {879draw_set_indexes(r300->draw,880info->has_user_indices ?881info->index.user :882r300_resource(info->index.resource)->malloced_buffer,883info->index_size, ~0);884}885886r300_update_derived_state(r300);887888draw_vbo(r300->draw, info, drawid_offset, NULL, &draw, 1);889draw_flush(r300->draw);890}891892/* Object for rendering using Draw. */893struct r300_render {894/* Parent class */895struct vbuf_render base;896897/* Pipe context */898struct r300_context* r300;899900/* Vertex information */901size_t vertex_size;902unsigned prim;903unsigned hwprim;904905/* VBO */906size_t vbo_max_used;907uint8_t *vbo_ptr;908};909910static inline struct r300_render*911r300_render(struct vbuf_render* render)912{913return (struct r300_render*)render;914}915916static const struct vertex_info*917r300_render_get_vertex_info(struct vbuf_render* render)918{919struct r300_render* r300render = r300_render(render);920struct r300_context* r300 = r300render->r300;921922return &r300->vertex_info;923}924925static boolean r300_render_allocate_vertices(struct vbuf_render* render,926ushort vertex_size,927ushort count)928{929struct r300_render* r300render = r300_render(render);930struct r300_context* r300 = r300render->r300;931struct radeon_winsys *rws = r300->rws;932size_t size = (size_t)vertex_size * (size_t)count;933934DBG(r300, DBG_DRAW, "r300: render_allocate_vertices (size: %d)\n", size);935936if (!r300->vbo || size + r300->draw_vbo_offset > r300->vbo->size) {937pb_reference(&r300->vbo, NULL);938r300->vbo = NULL;939r300render->vbo_ptr = NULL;940941r300->vbo = rws->buffer_create(rws,942MAX2(R300_MAX_DRAW_VBO_SIZE, size),943R300_BUFFER_ALIGNMENT,944RADEON_DOMAIN_GTT,945RADEON_FLAG_NO_INTERPROCESS_SHARING);946if (!r300->vbo) {947return FALSE;948}949r300->draw_vbo_offset = 0;950r300render->vbo_ptr = rws->buffer_map(rws, r300->vbo, &r300->cs,951PIPE_MAP_WRITE);952}953954r300render->vertex_size = vertex_size;955return TRUE;956}957958static void* r300_render_map_vertices(struct vbuf_render* render)959{960struct r300_render* r300render = r300_render(render);961struct r300_context* r300 = r300render->r300;962963DBG(r300, DBG_DRAW, "r300: render_map_vertices\n");964965assert(r300render->vbo_ptr);966return r300render->vbo_ptr + r300->draw_vbo_offset;967}968969static void r300_render_unmap_vertices(struct vbuf_render* render,970ushort min,971ushort max)972{973struct r300_render* r300render = r300_render(render);974struct r300_context* r300 = r300render->r300;975976DBG(r300, DBG_DRAW, "r300: render_unmap_vertices\n");977978r300render->vbo_max_used = MAX2(r300render->vbo_max_used,979r300render->vertex_size * (max + 1));980}981982static void r300_render_release_vertices(struct vbuf_render* render)983{984struct r300_render* r300render = r300_render(render);985struct r300_context* r300 = r300render->r300;986987DBG(r300, DBG_DRAW, "r300: render_release_vertices\n");988989r300->draw_vbo_offset += r300render->vbo_max_used;990r300render->vbo_max_used = 0;991}992993static void r300_render_set_primitive(struct vbuf_render* render,994enum pipe_prim_type prim)995{996struct r300_render* r300render = r300_render(render);997998r300render->prim = prim;999r300render->hwprim = r300_translate_primitive(prim);1000}10011002static void r300_render_draw_arrays(struct vbuf_render* render,1003unsigned start,1004unsigned count)1005{1006struct r300_render* r300render = r300_render(render);1007struct r300_context* r300 = r300render->r300;1008uint8_t* ptr;1009unsigned i;1010unsigned dwords = 6;10111012CS_LOCALS(r300);1013(void) i; (void) ptr;10141015assert(start == 0);1016assert(count < (1 << 16));10171018DBG(r300, DBG_DRAW, "r300: render_draw_arrays (count: %d)\n", count);10191020if (!r300_prepare_for_rendering(r300,1021PREP_EMIT_STATES | PREP_EMIT_VARRAYS_SWTCL,1022NULL, dwords, 0, 0, -1)) {1023return;1024}10251026BEGIN_CS(dwords);1027OUT_CS_REG(R300_GA_COLOR_CONTROL,1028r300_provoking_vertex_fixes(r300, r300render->prim));1029OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count - 1);1030OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);1031OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |1032r300render->hwprim);1033END_CS;1034}10351036static void r300_render_draw_elements(struct vbuf_render* render,1037const ushort* indices,1038uint count)1039{1040struct r300_render* r300render = r300_render(render);1041struct r300_context* r300 = r300render->r300;1042unsigned max_index = (r300->vbo->size - r300->draw_vbo_offset) /1043(r300render->r300->vertex_info.size * 4) - 1;1044struct pipe_resource *index_buffer = NULL;1045unsigned index_buffer_offset;10461047CS_LOCALS(r300);1048DBG(r300, DBG_DRAW, "r300: render_draw_elements (count: %d)\n", count);10491050u_upload_data(r300->uploader, 0, count * 2, 4, indices,1051&index_buffer_offset, &index_buffer);1052if (!index_buffer) {1053return;1054}10551056if (!r300_prepare_for_rendering(r300,1057PREP_EMIT_STATES |1058PREP_EMIT_VARRAYS_SWTCL | PREP_INDEXED,1059index_buffer, 12, 0, 0, -1)) {1060pipe_resource_reference(&index_buffer, NULL);1061return;1062}10631064BEGIN_CS(12);1065OUT_CS_REG(R300_GA_COLOR_CONTROL,1066r300_provoking_vertex_fixes(r300, r300render->prim));1067OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max_index);10681069OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 0);1070OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |1071r300render->hwprim);10721073OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER, 2);1074OUT_CS(R300_INDX_BUFFER_ONE_REG_WR | (R300_VAP_PORT_IDX0 >> 2));1075OUT_CS(index_buffer_offset);1076OUT_CS((count + 1) / 2);1077OUT_CS_RELOC(r300_resource(index_buffer));1078END_CS;10791080pipe_resource_reference(&index_buffer, NULL);1081}10821083static void r300_render_destroy(struct vbuf_render* render)1084{1085FREE(render);1086}10871088static struct vbuf_render* r300_render_create(struct r300_context* r300)1089{1090struct r300_render* r300render = CALLOC_STRUCT(r300_render);10911092r300render->r300 = r300;10931094r300render->base.max_vertex_buffer_bytes = R300_MAX_DRAW_VBO_SIZE;1095r300render->base.max_indices = 16 * 1024;10961097r300render->base.get_vertex_info = r300_render_get_vertex_info;1098r300render->base.allocate_vertices = r300_render_allocate_vertices;1099r300render->base.map_vertices = r300_render_map_vertices;1100r300render->base.unmap_vertices = r300_render_unmap_vertices;1101r300render->base.set_primitive = r300_render_set_primitive;1102r300render->base.draw_elements = r300_render_draw_elements;1103r300render->base.draw_arrays = r300_render_draw_arrays;1104r300render->base.release_vertices = r300_render_release_vertices;1105r300render->base.destroy = r300_render_destroy;11061107return &r300render->base;1108}11091110struct draw_stage* r300_draw_stage(struct r300_context* r300)1111{1112struct vbuf_render* render;1113struct draw_stage* stage;11141115render = r300_render_create(r300);11161117if (!render) {1118return NULL;1119}11201121stage = draw_vbuf_stage(r300->draw, render);11221123if (!stage) {1124render->destroy(render);1125return NULL;1126}11271128draw_set_render(r300->draw, render);11291130return stage;1131}11321133/****************************************************************************1134* End of SW TCL functions *1135***************************************************************************/11361137/* This functions is used to draw a rectangle for the blitter module.1138*1139* If we rendered a quad, the pixels on the main diagonal1140* would be computed and stored twice, which makes the clear/copy codepaths1141* somewhat inefficient. Instead we use a rectangular point sprite. */1142void r300_blitter_draw_rectangle(struct blitter_context *blitter,1143void *vertex_elements_cso,1144blitter_get_vs_func get_vs,1145int x1, int y1, int x2, int y2,1146float depth, unsigned num_instances,1147enum blitter_attrib_type type,1148const union blitter_attrib *attrib)1149{1150struct r300_context *r300 = r300_context(util_blitter_get_pipe(blitter));1151unsigned last_sprite_coord_enable = r300->sprite_coord_enable;1152unsigned width = x2 - x1;1153unsigned height = y2 - y1;1154unsigned vertex_size =1155type == UTIL_BLITTER_ATTRIB_COLOR || !r300->draw ? 8 : 4;1156unsigned dwords = 13 + vertex_size +1157(type == UTIL_BLITTER_ATTRIB_TEXCOORD_XY ? 7 : 0);1158static const union blitter_attrib zeros;1159CS_LOCALS(r300);11601161/* XXX workaround for a lockup in MSAA resolve on SWTCL chipsets, this1162* function most probably doesn't handle type=NONE correctly */1163if ((!r300->screen->caps.has_tcl && type == UTIL_BLITTER_ATTRIB_NONE) ||1164type == UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW ||1165num_instances > 1) {1166util_blitter_draw_rectangle(blitter, vertex_elements_cso, get_vs,1167x1, y1, x2, y2,1168depth, num_instances, type, attrib);1169return;1170}11711172if (r300->skip_rendering)1173return;11741175r300->context.bind_vertex_elements_state(&r300->context, vertex_elements_cso);1176r300->context.bind_vs_state(&r300->context, get_vs(blitter));11771178if (type == UTIL_BLITTER_ATTRIB_TEXCOORD_XY)1179r300->sprite_coord_enable = 1;11801181r300_update_derived_state(r300);11821183/* Mark some states we don't care about as non-dirty. */1184r300->viewport_state.dirty = FALSE;11851186if (!r300_prepare_for_rendering(r300, PREP_EMIT_STATES, NULL, dwords, 0, 0, -1))1187goto done;11881189DBG(r300, DBG_DRAW, "r300: draw_rectangle\n");11901191BEGIN_CS(dwords);1192/* Set up GA. */1193OUT_CS_REG(R300_GA_POINT_SIZE, (height * 6) | ((width * 6) << 16));11941195if (type == UTIL_BLITTER_ATTRIB_TEXCOORD_XY) {1196/* Set up the GA to generate texcoords. */1197OUT_CS_REG(R300_GB_ENABLE, R300_GB_POINT_STUFF_ENABLE |1198(R300_GB_TEX_STR << R300_GB_TEX0_SOURCE_SHIFT));1199OUT_CS_REG_SEQ(R300_GA_POINT_S0, 4);1200OUT_CS_32F(attrib->texcoord.x1);1201OUT_CS_32F(attrib->texcoord.y2);1202OUT_CS_32F(attrib->texcoord.x2);1203OUT_CS_32F(attrib->texcoord.y1);1204}12051206/* Set up VAP controls. */1207OUT_CS_REG(R300_VAP_CLIP_CNTL, R300_CLIP_DISABLE);1208OUT_CS_REG(R300_VAP_VTE_CNTL, R300_VTX_XY_FMT | R300_VTX_Z_FMT);1209OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size);1210OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);1211OUT_CS(1);1212OUT_CS(0);12131214/* Draw. */1215OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, vertex_size);1216OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (1 << 16) |1217R300_VAP_VF_CNTL__PRIM_POINTS);12181219OUT_CS_32F(x1 + width * 0.5f);1220OUT_CS_32F(y1 + height * 0.5f);1221OUT_CS_32F(depth);1222OUT_CS_32F(1);12231224if (vertex_size == 8) {1225if (!attrib)1226attrib = &zeros;1227OUT_CS_TABLE(attrib->color, 4);1228}1229END_CS;12301231done:1232/* Restore the state. */1233r300_mark_atom_dirty(r300, &r300->rs_state);1234r300_mark_atom_dirty(r300, &r300->viewport_state);12351236r300->sprite_coord_enable = last_sprite_coord_enable;1237}12381239void r300_init_render_functions(struct r300_context *r300)1240{1241/* Set draw functions based on presence of HW TCL. */1242if (r300->screen->caps.has_tcl) {1243r300->context.draw_vbo = r300_draw_vbo;1244} else {1245r300->context.draw_vbo = r300_swtcl_draw_vbo;1246}12471248/* Plug in the two-sided stencil reference value fallback if needed. */1249if (!r300->screen->caps.is_r500)1250r300_plug_in_stencil_ref_fallback(r300);1251}125212531254