//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	FeatureGrid::BLENDFUNCTION (
		FeatureInstance *featureInstance, 
		unsigned char *pix_buf, 
		int start_span, 
		int end_span,
		int downScale
	)
{
	Feature *feature = featureInstance->feature;

	Verify(feature->featureMask0!=NULL);
	
	unsigned char *mask0data = featureInstance->currentMask0Ptr;

#ifdef SHOW_MATERIAL
	DWORD mat_color = downScale & 0xffffff;
	downScale >>= 24;
#else
	int texture_width = (featureInstance->feature->featureTexture->width) >> downScale;
	int count_down = featureInstance->countDownToBorder;
	WORD *data_ptr_word = (WORD *)featureInstance->currentTexturePtr;
#endif

	int mask0_x_off = featureInstance->mask0_x_off >> downScale;
	int mask0_z_off = featureInstance->mask0_z_off >> downScale;

	int shifter = feature->GetZMask0Scale() + feature->GetXMask0Scale() - 2*downScale;

	int x_stretch = 1<<(feature->GetXMask0Scale()-downScale);
	int z_stretch = 1<<(feature->GetZMask0Scale()-downScale);

#ifdef MASK0_HORIZONTAL_FLIP
	int advancer = x_stretch-1;
#else
	int advancer = ~(x_stretch-1);
#endif

	int adv, mixH, mix0, mix1, mix00, mix01, mix10, mix11;
	int pitch = feature->featureMask0->width + 2;

	mix00 = *mask0data;
	mix01 = *(mask0data + 1);
	mix10 = *(mask0data + pitch);
	mix11 = *(mask0data + pitch + 1);

	mix0 = (mix00*(z_stretch - mask0_z_off) + mix10*mask0_z_off);
	mix1 = (mix01*(z_stretch - mask0_z_off) + mix11*mask0_z_off);

	mixH = (mix0*(x_stretch - mask0_x_off) + mix1*mask0_x_off);

	adv = mix1 - mix0;

	int *pix_buf_int = (int *)pix_buf;
//	for(int i=0;i<end_span-start_span;i++)
	for(int i=0;;)
	{
		int mix = mixH>>shifter;

#ifdef MASK0_HORIZONTAL_FLIP
		mixH -= adv;
#else
		mixH += adv;
#endif

#ifdef SHOW_MATERIAL
		if(mix > 0x80)
		{
			pix_buf_int[i] = mat_color;
		}
#else
		if(mix!=0)
		{
			DWORD dst = pix_buf_int[i];
#ifdef TEXTURE_HORIZONTAL_FLIP
			DWORD src = data_ptr_word[-i];
#else
			DWORD src = data_ptr_word[i];
#endif
			DWORD buf;
			DWORD srcmix = mix<<3;

			mix = 0xff-mix;

			buf = ((dst & 0xff)*mix + (src & 0x1f)*srcmix)>>8;

			buf |= (((dst>>8)&0xff)*mix + ((src & 0x3e0) >> 5)*srcmix)&0xff00;

			buf |= ((((dst>>16)&0xff)*mix + (src >> 10)*srcmix)&0xff00)<<8;

			pix_buf_int[i] = buf;
		}
#endif

		if(++i>=end_span-start_span)
		{
			break;
		}

#ifdef MASK0_HORIZONTAL_FLIP
#ifndef SHOW_MATERIAL
		if(--count_down <= 0)
		{
			data_ptr_word += texture_width;
			count_down = texture_width;
		}
#endif
		if(--mask0_x_off < 0)
		{
			mask0_x_off = advancer;
			mask0data -= feature->featureMask0->depth;

			mix01 = mix00;
			mix00 = *mask0data;
			mix11 = mix10;
			mix10 = *(mask0data + pitch);

			mix1 = mix0;
			mix0 = (mix00*(z_stretch - mask0_z_off) + mix10*mask0_z_off);
#else
#ifndef SHOW_MATERIAL
		if(--count_down <= 0)
		{
			data_ptr_word -= texture_width;
			count_down = texture_width;
		}
#endif

		if(++mask0_x_off & advancer)
		{
			mask0_x_off = 0;
			mask0data += feature->featureMask0->depth;

			mix00 = mix01;
			mix01 = *(mask0data + 1);
			mix10 = mix11;
			mix11 = *(mask0data + pitch + 1);

			mix0 = mix1;
			mix1 = (mix01*(z_stretch - mask0_z_off) + mix11*mask0_z_off);
#endif

			mixH = (mix0*(x_stretch - mask0_x_off) + mix1*mask0_x_off);

			adv = mix1 - mix0;
		}
	}
}
