Path: blob/21.2-virgl/src/gallium/drivers/r300/compiler/radeon_code.c
4574 views
/*1* Copyright (C) 2009 Nicolai Haehnle.2*3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining6* a copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sublicense, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial15* portions of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,18* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.20* IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE21* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION22* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION23* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25*/2627#include "radeon_code.h"2829#include <stdlib.h>30#include <stdio.h>31#include <string.h>3233#include "radeon_program.h"3435void rc_constants_init(struct rc_constant_list * c)36{37memset(c, 0, sizeof(*c));38}3940/**41* Copy a constants structure, assuming that the destination structure42* is not initialized.43*/44void rc_constants_copy(struct rc_constant_list * dst, struct rc_constant_list * src)45{46dst->Constants = malloc(sizeof(struct rc_constant) * src->Count);47memcpy(dst->Constants, src->Constants, sizeof(struct rc_constant) * src->Count);48dst->Count = src->Count;49dst->_Reserved = src->Count;50}5152void rc_constants_destroy(struct rc_constant_list * c)53{54free(c->Constants);55memset(c, 0, sizeof(*c));56}5758unsigned rc_constants_add(struct rc_constant_list * c, struct rc_constant * constant)59{60unsigned index = c->Count;6162if (c->Count >= c->_Reserved) {63struct rc_constant * newlist;6465c->_Reserved = c->_Reserved * 2;66if (!c->_Reserved)67c->_Reserved = 16;6869newlist = malloc(sizeof(struct rc_constant) * c->_Reserved);70memcpy(newlist, c->Constants, sizeof(struct rc_constant) * c->Count);7172free(c->Constants);73c->Constants = newlist;74}7576c->Constants[index] = *constant;77c->Count++;7879return index;80}818283/**84* Add a state vector to the constant list, while trying to avoid duplicates.85*/86unsigned rc_constants_add_state(struct rc_constant_list * c, unsigned state0, unsigned state1)87{88unsigned index;89struct rc_constant constant;9091for(index = 0; index < c->Count; ++index) {92if (c->Constants[index].Type == RC_CONSTANT_STATE) {93if (c->Constants[index].u.State[0] == state0 &&94c->Constants[index].u.State[1] == state1)95return index;96}97}9899memset(&constant, 0, sizeof(constant));100constant.Type = RC_CONSTANT_STATE;101constant.Size = 4;102constant.u.State[0] = state0;103constant.u.State[1] = state1;104105return rc_constants_add(c, &constant);106}107108109/**110* Add an immediate vector to the constant list, while trying to avoid111* duplicates.112*/113unsigned rc_constants_add_immediate_vec4(struct rc_constant_list * c, const float * data)114{115unsigned index;116struct rc_constant constant;117118for(index = 0; index < c->Count; ++index) {119if (c->Constants[index].Type == RC_CONSTANT_IMMEDIATE) {120if (!memcmp(c->Constants[index].u.Immediate, data, sizeof(float)*4))121return index;122}123}124125memset(&constant, 0, sizeof(constant));126constant.Type = RC_CONSTANT_IMMEDIATE;127constant.Size = 4;128memcpy(constant.u.Immediate, data, sizeof(float) * 4);129130return rc_constants_add(c, &constant);131}132133134/**135* Add an immediate scalar to the constant list, while trying to avoid136* duplicates.137*/138unsigned rc_constants_add_immediate_scalar(struct rc_constant_list * c, float data, unsigned * swizzle)139{140unsigned index;141int free_index = -1;142struct rc_constant constant;143144for(index = 0; index < c->Count; ++index) {145if (c->Constants[index].Type == RC_CONSTANT_IMMEDIATE) {146unsigned comp;147for(comp = 0; comp < c->Constants[index].Size; ++comp) {148if (c->Constants[index].u.Immediate[comp] == data) {149*swizzle = RC_MAKE_SWIZZLE_SMEAR(comp);150return index;151}152}153154if (c->Constants[index].Size < 4)155free_index = index;156}157}158159if (free_index >= 0) {160unsigned comp = c->Constants[free_index].Size++;161c->Constants[free_index].u.Immediate[comp] = data;162*swizzle = RC_MAKE_SWIZZLE_SMEAR(comp);163return free_index;164}165166memset(&constant, 0, sizeof(constant));167constant.Type = RC_CONSTANT_IMMEDIATE;168constant.Size = 1;169constant.u.Immediate[0] = data;170*swizzle = RC_SWIZZLE_XXXX;171172return rc_constants_add(c, &constant);173}174175void rc_constants_print(struct rc_constant_list * c)176{177unsigned int i;178for(i = 0; i < c->Count; i++) {179if (c->Constants[i].Type == RC_CONSTANT_IMMEDIATE) {180float * values = c->Constants[i].u.Immediate;181fprintf(stderr, "CONST[%u] = "182"{ %10.4f %10.4f %10.4f %10.4f }\n",183i, values[0],values[1], values[2], values[3]);184}185}186}187188189