<commit_before>#include <rsx/commands.h>
#include <rsx/nv40.h>
#include <rsx/buffer.h>
#include <rsx/reality.h>

static __inline__ f32 swapF32_16(f32 v)
{
	ieee32 d;
	d.f = v;
	d.u = ( ( ( d.u >> 16 ) & 0xffff ) << 0 ) | ( ( ( d.u >> 0 ) & 0xffff ) << 16 );
	return d.f;
}

void realitySetClearColor(gcmContextData *context, uint32_t color) {
	COMMAND_LENGTH(context, 2);
	commandBufferPutCmd1(context, NV30_3D_CLEAR_COLOR_VALUE, color);
}

void realitySetClearDepthValue(gcmContextData *context, uint32_t value) {
	COMMAND_LENGTH(context, 2);
	commandBufferPutCmd1(context, NV30_3D_CLEAR_DEPTH_VALUE, value);
}

void realityNop(gcmContextData *context) {
	COMMAND_LENGTH(context, 2);
	commandBufferPutCmd1(context, 0x100, 0);
}

void realityClearBuffers(gcmContextData *context, uint32_t buffers) {
	COMMAND_LENGTH(context, 4);
	commandBufferPutCmd1(context, NV30_3D_CLEAR_BUFFERS, buffers);
	commandBufferPutCmd1(context, 0x100, 0); // Nop
}	

const static uint32_t offset_cmds[] = {NV30_3D_COLOR0_OFFSET, NV30_3D_COLOR1_OFFSET,
		NV40_3D_COLOR2_OFFSET, NV40_3D_COLOR3_OFFSET, NV30_3D_ZETA_OFFSET};
const static uint32_t dma_cmds[] = {NV30_3D_DMA_COLOR0, NV30_3D_DMA_COLOR1,
		NV40_3D_DMA_COLOR2, NV40_3D_DMA_COLOR3, NV30_3D_DMA_ZETA};
const static uint32_t pitch_cmds[] = {NV30_3D_COLOR0_PITCH, NV30_3D_COLOR1_PITCH,
		NV40_3D_COLOR2_PITCH, NV40_3D_COLOR3_PITCH, NV40_3D_ZETA_PITCH};

void realitySetRenderSurface(gcmContextData *context, uint8_t surface, uint8_t location,
				uint32_t offset, uint32_t pitch) {
	COMMAND_LENGTH(context, 6);
	commandBufferPutCmd1(context, dma_cmds[surface], location | 0xFEED0000);
	commandBufferPutCmd1(context, offset_cmds[surface], offset);
	commandBufferPutCmd1(context, pitch_cmds[surface], pitch);
}

void realitySelectRenderTarget(gcmContextData *context, uint8_t target, uint32_t format, 
				uint16_t width, uint16_t height, uint16_t x, uint16_t y) {
	COMMAND_LENGTH(context, 11);

	format |= (31 - __builtin_clz(width)) << 16;
	format |= (31 - __builtin_clz(height)) << 24;
	uint32_t conventions = height | 0x1000; // COORD_CONVENTIONS_ORIGIN_INVERTED
	
	commandBufferPutCmd1(context, NV30_3D_RT_FORMAT, format); 
	commandBufferPutCmd1(context, NV30_3D_RT_ENABLE, target);
	commandBufferPutCmd1(context, NV30_3D_VIEWPORT_TX_ORIGIN, x | y << 16);
	commandBufferPutCmd1(context, NV30_3D_COORD_CONVENTIONS, conventions);
	commandBufferPutCmd2(context, NV30_3D_RT_HORIZ, x | width << 16, y | height << 16);
}

void realityViewport(gcmContextData *context, uint16_t width, uint16_t height) {
	COMMAND_LENGTH(context, 3);
	commandBufferPutCmd2(context, NV30_3D_VIEWPORT_HORIZ, width << 16, height << 16);
}

void realityViewportClip(gcmContextData *context, u8 sel, uint16_t width, uint16_t height) {
    COMMAND_LENGTH(context, 3);
	commandBufferPutCmd2(context, NV30_3D_VIEWPORT_CLIP_HORIZ(sel), (width-1) << 16, (height-1) << 16);
}

void realityVertexBegin(gcmContextData *context, uint32_t type) {
	COMMAND_LENGTH(context, 2);
	commandBufferPutCmd1(context, NV30_3D_VERTEX_BEGIN_END, type);
}

void realityVertexEnd(gcmContextData *context) {
	COMMAND_LENGTH(context, 2);
	commandBufferPutCmd1(context, NV30_3D_VERTEX_BEGIN_END, NV30_3D_VERTEX_BEGIN_END_STOP);
}

void realityVertex4f(gcmContextData *context, float x, float y, float z, float w) {
	COMMAND_LENGTH(context, 5);
	commandBufferPutCmd4(context, NV30_3D_VTX_ATTR_4F(0),
				 ((ieee32) x).u, 
				 ((ieee32) y).u,
				 ((ieee32) z).u, 
				 ((ieee32) w).u);
}

void realityTexCoord2f(gcmContextData *context, float s, float t) {
	COMMAND_LENGTH(context, 3);
	commandBufferPutCmd2(context, NV30_3D_VTX_ATTR_2F(8),
				 ((ieee32) s).u, 
				 ((ieee32) t).u);
}

void realityAttr4f(gcmContextData *context, int attr,
    float x, float y, float z, float w) {
	COMMAND_LENGTH(context, 5);
	commandBufferPutCmd4(context, NV30_3D_VTX_ATTR_4F(attr),
				 ((ieee32) x).u,
				 ((ieee32) y).u,
				 ((ieee32) z).u,
				 ((ieee32) w).u);
}

void realityAttr2f(gcmContextData *context, int attr, float x, float y) {
	COMMAND_LENGTH(context, 5);
	commandBufferPutCmd2(context, NV30_3D_VTX_ATTR_2F(attr),
	    ((ieee32) x).u,
	    ((ieee32) y).u);
}


void realityLoadVertexProgram_old(gcmContextData *context, realityVertexProgram_old *prog) {
	int inst, command_size = prog->size * 2 + 7;
	COMMAND_LENGTH(context, command_size);
	
	commandBufferPutCmd1(context, NV30_3D_VP_UPLOAD_FROM_ID, 0);
	
	for(inst = 0; inst < prog->size; inst += 4) {
		commandBufferPutCmd4(context, NV30_3D_VP_UPLOAD_INST(0), 
					prog->data[inst + 0],
					prog->data[inst + 1],
					prog->data[inst + 2],
					prog->data[inst + 3]);
	}

	commandBufferPutCmd1(context, NV30_3D_VP_START_FROM_ID, 0);
	commandBufferPutCmd2(context, NV40_3D_VP_ATTRIB_EN, prog->in_reg, prog->out_reg);
}

void realityLoadVertexProgram(gcmContextData *context, realityVertexProgram *prog) {
	int inst, command_size = prog->num_insn * 5 + 7;
	unsigned int *ucode = (unsigned int*)realityVertexProgramGetUCode(prog);
	realityProgramConst *constants;
	COMMAND_LENGTH(context, command_size);
	
	commandBufferPutCmd1(context, NV30_3D_VP_UPLOAD_FROM_ID, 0);
	
	for(inst = 0; inst < prog->num_insn*4; inst += 4) {
		commandBufferPutCmd4(context, NV30_3D_VP_UPLOAD_INST(0), 
					ucode[inst + 0],
					ucode[inst + 1],
					ucode[inst + 2],
					ucode[inst + 3]);
	}

	commandBufferPutCmd1(context, NV30_3D_VP_START_FROM_ID, 0);
	commandBufferPutCmd2(context, NV40_3D_VP_ATTRIB_EN, prog->input_mask, prog->output_mask);

	constants = realityVertexProgramGetConstants(prog);

	if(constants)
	{
		int c;
		for(c=0;c<prog->num_const;++c)
		{
			realitySetVertexProgramConstant4f(context,constants[c].index,(float*)constants[c].values);
		}
	}
}

void realitySetVertexProgramConstant4f(gcmContextData *context, int num, float values[4]){

	COMMAND_LENGTH(context, 6);

	commandBufferPutCmd5(context, NV30_3D_VP_UPLOAD_CONST_ID, 
							num,
							((ieee32) (values[0])).u,
							((ieee32) (values[1])).u,
							((ieee32) (values[2])).u,
							((ieee32) (values[3])).u
							);
}

void realitySetVertexProgramConstant4fBlock(gcmContextData *context, realityVertexProgram *prog, int index, int num4fConstants, float *values)
{
	int n;
	realityProgramConst *constants = realityVertexProgramGetConstants(prog);

	for(n=0;n<num4fConstants;++n)
	{
		realitySetVertexProgramConstant4f(context,constants[index].index+n,values+4*n);
	}
}

void realityLoadFragmentProgram(gcmContextData *context, realityFragmentProgram *prog,u32 offset,u32 location)
{
	u32 fpcontrol;

	COMMAND_LENGTH(context, 4);
	
	assert(offset!=0);
	
	commandBufferPutCmd1(context, NV30_3D_FP_ACTIVE_PROGRAM,
						 offset | (location + 1));

	fpcontrol = prog->fp_control | (prog->num_regs << NV40_3D_FP_CONTROL_TEMP_COUNT__SHIFT);
	commandBufferPutCmd1(context, NV30_3D_FP_CONTROL, 
 						 fpcontrol);
}

void realitySetFragmentProgramParameter(gcmContextData *context,realityFragmentProgram *program,s32 index,const f32 *value,u32 offset)
{
	f32 params[4] = {0.0f,0.0f,0.0f,0.0f};
	realityProgramConst *consts = realityFragmentProgramGetConsts(program);

	switch(consts[index].type) {
		case PARAM_FLOAT4:
			params[3] = swapF32_16(value[3]);
		case PARAM_FLOAT3:
			params[2] = swapF32_16(value[2]);
		case PARAM_FLOAT2:
			params[1] = swapF32_16(value[1]);
		case PARAM_FLOAT:
			params[0] = swapF32_16(value[0]);
			break;
	}
	realityInlineTransfer(context,offset + consts[index].index,params,4,REALITY_RSX_MEMORY);
}

void realitySetTexture(gcmContextData *context, uint32_t unit, realityTexture *tex) {
	COMMAND_LENGTH(context, 11);<commit_msg>Added upload of 4x4 matrices for fragment program parameters.<commit_after>#include <rsx/commands.h>
#include <rsx/nv40.h>
#include <rsx/buffer.h>
#include <rsx/reality.h>

static __inline__ f32 swapF32_16(f32 v)
{
	ieee32 d;
	d.f = v;
	d.u = ( ( ( d.u >> 16 ) & 0xffff ) << 0 ) | ( ( ( d.u >> 0 ) & 0xffff ) << 16 );
	return d.f;
}

void realitySetClearColor(gcmContextData *context, uint32_t color) {
	COMMAND_LENGTH(context, 2);
	commandBufferPutCmd1(context, NV30_3D_CLEAR_COLOR_VALUE, color);
}

void realitySetClearDepthValue(gcmContextData *context, uint32_t value) {
	COMMAND_LENGTH(context, 2);
	commandBufferPutCmd1(context, NV30_3D_CLEAR_DEPTH_VALUE, value);
}

void realityNop(gcmContextData *context) {
	COMMAND_LENGTH(context, 2);
	commandBufferPutCmd1(context, 0x100, 0);
}

void realityClearBuffers(gcmContextData *context, uint32_t buffers) {
	COMMAND_LENGTH(context, 4);
	commandBufferPutCmd1(context, NV30_3D_CLEAR_BUFFERS, buffers);
	commandBufferPutCmd1(context, 0x100, 0); // Nop
}	

const static uint32_t offset_cmds[] = {NV30_3D_COLOR0_OFFSET, NV30_3D_COLOR1_OFFSET,
		NV40_3D_COLOR2_OFFSET, NV40_3D_COLOR3_OFFSET, NV30_3D_ZETA_OFFSET};
const static uint32_t dma_cmds[] = {NV30_3D_DMA_COLOR0, NV30_3D_DMA_COLOR1,
		NV40_3D_DMA_COLOR2, NV40_3D_DMA_COLOR3, NV30_3D_DMA_ZETA};
const static uint32_t pitch_cmds[] = {NV30_3D_COLOR0_PITCH, NV30_3D_COLOR1_PITCH,
		NV40_3D_COLOR2_PITCH, NV40_3D_COLOR3_PITCH, NV40_3D_ZETA_PITCH};

void realitySetRenderSurface(gcmContextData *context, uint8_t surface, uint8_t location,
				uint32_t offset, uint32_t pitch) {
	COMMAND_LENGTH(context, 6);
	commandBufferPutCmd1(context, dma_cmds[surface], location | 0xFEED0000);
	commandBufferPutCmd1(context, offset_cmds[surface], offset);
	commandBufferPutCmd1(context, pitch_cmds[surface], pitch);
}

void realitySelectRenderTarget(gcmContextData *context, uint8_t target, uint32_t format, 
				uint16_t width, uint16_t height, uint16_t x, uint16_t y) {
	COMMAND_LENGTH(context, 11);

	format |= (31 - __builtin_clz(width)) << 16;
	format |= (31 - __builtin_clz(height)) << 24;
	uint32_t conventions = height | 0x1000; // COORD_CONVENTIONS_ORIGIN_INVERTED
	
	commandBufferPutCmd1(context, NV30_3D_RT_FORMAT, format); 
	commandBufferPutCmd1(context, NV30_3D_RT_ENABLE, target);
	commandBufferPutCmd1(context, NV30_3D_VIEWPORT_TX_ORIGIN, x | y << 16);
	commandBufferPutCmd1(context, NV30_3D_COORD_CONVENTIONS, conventions);
	commandBufferPutCmd2(context, NV30_3D_RT_HORIZ, x | width << 16, y | height << 16);
}

void realityViewport(gcmContextData *context, uint16_t width, uint16_t height) {
	COMMAND_LENGTH(context, 3);
	commandBufferPutCmd2(context, NV30_3D_VIEWPORT_HORIZ, width << 16, height << 16);
}

void realityViewportClip(gcmContextData *context, u8 sel, uint16_t width, uint16_t height) {
    COMMAND_LENGTH(context, 3);
	commandBufferPutCmd2(context, NV30_3D_VIEWPORT_CLIP_HORIZ(sel), (width-1) << 16, (height-1) << 16);
}

void realityVertexBegin(gcmContextData *context, uint32_t type) {
	COMMAND_LENGTH(context, 2);
	commandBufferPutCmd1(context, NV30_3D_VERTEX_BEGIN_END, type);
}

void realityVertexEnd(gcmContextData *context) {
	COMMAND_LENGTH(context, 2);
	commandBufferPutCmd1(context, NV30_3D_VERTEX_BEGIN_END, NV30_3D_VERTEX_BEGIN_END_STOP);
}

void realityVertex4f(gcmContextData *context, float x, float y, float z, float w) {
	COMMAND_LENGTH(context, 5);
	commandBufferPutCmd4(context, NV30_3D_VTX_ATTR_4F(0),
				 ((ieee32) x).u, 
				 ((ieee32) y).u,
				 ((ieee32) z).u, 
				 ((ieee32) w).u);
}

void realityTexCoord2f(gcmContextData *context, float s, float t) {
	COMMAND_LENGTH(context, 3);
	commandBufferPutCmd2(context, NV30_3D_VTX_ATTR_2F(8),
				 ((ieee32) s).u, 
				 ((ieee32) t).u);
}

void realityAttr4f(gcmContextData *context, int attr,
    float x, float y, float z, float w) {
	COMMAND_LENGTH(context, 5);
	commandBufferPutCmd4(context, NV30_3D_VTX_ATTR_4F(attr),
				 ((ieee32) x).u,
				 ((ieee32) y).u,
				 ((ieee32) z).u,
				 ((ieee32) w).u);
}

void realityAttr2f(gcmContextData *context, int attr, float x, float y) {
	COMMAND_LENGTH(context, 5);
	commandBufferPutCmd2(context, NV30_3D_VTX_ATTR_2F(attr),
	    ((ieee32) x).u,
	    ((ieee32) y).u);
}


void realityLoadVertexProgram_old(gcmContextData *context, realityVertexProgram_old *prog) {
	int inst, command_size = prog->size * 2 + 7;
	COMMAND_LENGTH(context, command_size);
	
	commandBufferPutCmd1(context, NV30_3D_VP_UPLOAD_FROM_ID, 0);
	
	for(inst = 0; inst < prog->size; inst += 4) {
		commandBufferPutCmd4(context, NV30_3D_VP_UPLOAD_INST(0), 
					prog->data[inst + 0],
					prog->data[inst + 1],
					prog->data[inst + 2],
					prog->data[inst + 3]);
	}

	commandBufferPutCmd1(context, NV30_3D_VP_START_FROM_ID, 0);
	commandBufferPutCmd2(context, NV40_3D_VP_ATTRIB_EN, prog->in_reg, prog->out_reg);
}

void realityLoadVertexProgram(gcmContextData *context, realityVertexProgram *prog) {
	int inst, command_size = prog->num_insn * 5 + 7;
	unsigned int *ucode = (unsigned int*)realityVertexProgramGetUCode(prog);
	realityProgramConst *constants;
	COMMAND_LENGTH(context, command_size);
	
	commandBufferPutCmd1(context, NV30_3D_VP_UPLOAD_FROM_ID, 0);
	
	for(inst = 0; inst < prog->num_insn*4; inst += 4) {
		commandBufferPutCmd4(context, NV30_3D_VP_UPLOAD_INST(0), 
					ucode[inst + 0],
					ucode[inst + 1],
					ucode[inst + 2],
					ucode[inst + 3]);
	}

	commandBufferPutCmd1(context, NV30_3D_VP_START_FROM_ID, 0);
	commandBufferPutCmd2(context, NV40_3D_VP_ATTRIB_EN, prog->input_mask, prog->output_mask);

	constants = realityVertexProgramGetConstants(prog);

	if(constants)
	{
		int c;
		for(c=0;c<prog->num_const;++c)
		{
			realitySetVertexProgramConstant4f(context,constants[c].index,(float*)constants[c].values);
		}
	}
}

void realitySetVertexProgramConstant4f(gcmContextData *context, int num, float values[4]){

	COMMAND_LENGTH(context, 6);

	commandBufferPutCmd5(context, NV30_3D_VP_UPLOAD_CONST_ID, 
							num,
							((ieee32) (values[0])).u,
							((ieee32) (values[1])).u,
							((ieee32) (values[2])).u,
							((ieee32) (values[3])).u
							);
}

void realitySetVertexProgramConstant4fBlock(gcmContextData *context, realityVertexProgram *prog, int index, int num4fConstants, float *values)
{
	int n;
	realityProgramConst *constants = realityVertexProgramGetConstants(prog);

	for(n=0;n<num4fConstants;++n)
	{
		realitySetVertexProgramConstant4f(context,constants[index].index+n,values+4*n);
	}
}

void realityLoadFragmentProgram(gcmContextData *context, realityFragmentProgram *prog,u32 offset,u32 location)
{
	u32 fpcontrol;

	COMMAND_LENGTH(context, 4);
	
	assert(offset!=0);
	
	commandBufferPutCmd1(context, NV30_3D_FP_ACTIVE_PROGRAM,
						 offset | (location + 1));

	fpcontrol = prog->fp_control | (prog->num_regs << NV40_3D_FP_CONTROL_TEMP_COUNT__SHIFT);
	commandBufferPutCmd1(context, NV30_3D_FP_CONTROL, 
 						 fpcontrol);
}

void realitySetFragmentProgramParameter(gcmContextData *context,realityFragmentProgram *program,s32 index,const f32 *value,u32 offset)
{
	f32 params[4] = {0.0f,0.0f,0.0f,0.0f};
	realityProgramConst *consts = realityFragmentProgramGetConsts(program);

	switch(consts[index].type) {
		case PARAM_FLOAT4x4:
		{
			s32 i,cnt = consts[index].count;
			for(i=0;i<cnt;i++) {
				params[0] = swapF32_16(value[(i*4) + 0]);
				params[1] = swapF32_16(value[(i*4) + 1]);
				params[2] = swapF32_16(value[(i*4) + 2]);
				params[3] = swapF32_16(value[(i*4) + 3]);
				rsxInlineTransfer(context,offset + consts[index + i].index,params,4,GCM_LOCATION_RSX);
			}
		}
		return;

		case PARAM_FLOAT4:
			params[3] = swapF32_16(value[3]);
		case PARAM_FLOAT3:
			params[2] = swapF32_16(value[2]);
		case PARAM_FLOAT2:
			params[1] = swapF32_16(value[1]);
		case PARAM_FLOAT:
			params[0] = swapF32_16(value[0]);
			break;
	}
	realityInlineTransfer(context,offset + consts[index].index,params,4,REALITY_RSX_MEMORY);
}

void realitySetTexture(gcmContextData *context, uint32_t unit, realityTexture *tex) {
	COMMAND_LENGTH(context, 11);
{'fraction_non_alphanumeric': 0.12430868167202572, 'fraction_numerical': 0.0582636655948553, 'mean_word_length': 6.097672295755363, 'pattern_counts': {'":': 0, '<': 68, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6410608', 'n_tokens_mistral': 6838, 'n_tokens_neox': 6076, 'n_words': 1027}