设置纹理/sampler的状态,这一部分我是在渲染的时候做的,也可以直接写在HLSL代码中。在后面渲染部分还会见到这些代码,其实是同一段代码,我只是为了表述纹理与sampler关联的一个整体过程,把它预先从渲染部分截了出来,希望不会造成误解。
// Y tex
Device->SetTexture( YTexDesc.RegisterIndex, YTex);
Device->SetSamplerState(YTexDesc.RegisterIndex, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(YTexDesc.RegisterIndex, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(YTexDesc.RegisterIndex, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(YTexDesc.RegisterIndex, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
Device->SetSamplerState(YTexDesc.RegisterIndex, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
// U tex
Device->SetTexture( UTexDesc.RegisterIndex, UTex);
Device->SetSamplerState(UTexDesc.RegisterIndex, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(UTexDesc.RegisterIndex, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(UTexDesc.RegisterIndex, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(UTexDesc.RegisterIndex, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
Device->SetSamplerState(UTexDesc.RegisterIndex, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
// string tex
Device->SetTexture( VTexDesc.RegisterIndex, VTex);
Device->SetSamplerState(VTexDesc.RegisterIndex, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(VTexDesc.RegisterIndex, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(VTexDesc.RegisterIndex, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(VTexDesc.RegisterIndex, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
Device->SetSamplerState(VTexDesc.RegisterIndex, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
if((infile=fopen("test_yuv420p_320x180.yuv", "rb"))==NULL){
printf("cannot open this file\n");
return false;
}
if (fread(buf, 1, Width*Height*3/2, infile) != Width*Height*3/2){
// Loop
fseek(infile, 0, SEEK_SET);
fread(buf, 1, Width*Height*3/2, infile);
}
//
// Render
//
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
plane[0] = buf;
plane[1] = plane[0] + Width*Height;
plane[2] = plane[1] + Width*Height/4;
D3DLOCKED_RECT d3d_rect;
byte *pSrc = buf;
//Locks a rectangle on a texture resource.
//And then we can manipulate pixel data in it.
LRESULT lRet = YTex->LockRect(0, &d3d_rect, 0, 0);
if (FAILED(lRet)){
return false;
}
// Copy pixel data to texture
byte *pDest = (byte *)d3d_rect.pBits;
int stride = d3d_rect.Pitch;
for(int i = 0;i < Height;i ++){
memcpy(pDest + i * stride,plane[0] + i * Width, Width);
}
YTex->UnlockRect(0);
D3DLOCKED_RECT d3d_rect1;
lRet = UTex->LockRect(0, &d3d_rect1, 0, 0);
if (FAILED(lRet)){
return false;
}
// Copy pixel data to texture
byte *pDest1 = (byte *)d3d_rect1.pBits;
int stride1 = d3d_rect1.Pitch;
for(int i = 0;i < Height/2;i ++){
memcpy(pDest1 + i * stride1 /