texture sampling demo

This commit is contained in:
Zack Buhman 2025-12-22 18:47:29 -06:00
parent da92a49ed9
commit e44f06353f
6 changed files with 1800 additions and 12 deletions

13
a8r8b8g8.py Normal file
View File

@ -0,0 +1,13 @@
import sys
with open(sys.argv[1], 'rb') as f:
buf = f.read()
with open(sys.argv[2], 'wb') as f:
for i in range(len(buf) // 4):
r = buf[i*4+0]
g = buf[i*4+1]
b = buf[i*4+2]
a = buf[i*4+3]
f.write(bytes(reversed([a, r, g, b])))

853
bear.data Normal file

File diff suppressed because one or more lines are too long

853
bear_a8r8g8b8.data Normal file

File diff suppressed because one or more lines are too long

View File

@ -27,9 +27,11 @@ IDirect3D9 * g_pD3D = NULL;
IDirect3DDevice9 * g_pd3dDevice = NULL; IDirect3DDevice9 * g_pd3dDevice = NULL;
IDirect3DVertexBuffer9 * g_pVB = NULL; IDirect3DVertexBuffer9 * g_pVB = NULL;
IDirect3DVertexDeclaration9 * g_pVertexDeclaration = NULL; IDirect3DVertexDeclaration9 * g_pVertexDeclaration = NULL;
ID3DXConstantTable * g_pConstantTable = NULL; ID3DXConstantTable * g_pVertexConstantTable = NULL;
IDirect3DVertexShader9 * g_pVertexShader = NULL; IDirect3DVertexShader9 * g_pVertexShader = NULL;
ID3DXConstantTable * g_pPixelConstantTable = NULL;
IDirect3DPixelShader9 * g_pPixelShader = NULL; IDirect3DPixelShader9 * g_pPixelShader = NULL;
IDirect3DTexture9 * g_pTexture = NULL;
IDirectInput8 * g_pDI = NULL; IDirectInput8 * g_pDI = NULL;
IDirectInputDevice8 * g_pdiDevice; IDirectInputDevice8 * g_pdiDevice;
@ -146,12 +148,17 @@ void Render()
g_pd3dDevice->BeginScene(); g_pd3dDevice->BeginScene();
D3DXMATRIX mWorldViewProj = WorldViewProjection(); D3DXMATRIX mWorldViewProj = WorldViewProjection();
g_pConstantTable->SetMatrix(g_pd3dDevice, "mWorldViewProj", &mWorldViewProj); g_pVertexConstantTable->SetMatrix(g_pd3dDevice, "mWorldViewProj", &mWorldViewProj);
g_pd3dDevice->SetVertexDeclaration(g_pVertexDeclaration); g_pd3dDevice->SetVertexDeclaration(g_pVertexDeclaration);
g_pd3dDevice->SetVertexShader(g_pVertexShader); g_pd3dDevice->SetVertexShader(g_pVertexShader);
g_pd3dDevice->SetPixelShader(g_pPixelShader); g_pd3dDevice->SetPixelShader(g_pPixelShader);
g_pd3dDevice->SetTexture(0, g_pTexture);
g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
g_pd3dDevice->SetStreamSource(0, g_pVB, 0, (sizeof (CUSTOMVERTEX))); g_pd3dDevice->SetStreamSource(0, g_pVB, 0, (sizeof (CUSTOMVERTEX)));
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1); g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
g_pd3dDevice->EndScene(); g_pd3dDevice->EndScene();
@ -301,8 +308,10 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine
return 0; return 0;
} }
if (0) {
InitDirectSound(hwnd); InitDirectSound(hwnd);
return 0; return 0;
}
if (!SUCCEEDED(InitDirect3D(hwnd))) if (!SUCCEEDED(InitDirect3D(hwnd)))
return 0; return 0;
@ -362,7 +371,7 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine
dwShaderFlags, // Flags dwShaderFlags, // Flags
&pCode, // ppShader &pCode, // ppShader
NULL, // ppErrorMsgs NULL, // ppErrorMsgs
&g_pConstantTable // ppConstantTable &g_pVertexConstantTable // ppConstantTable
); );
if (FAILED(res)) { if (FAILED(res)) {
fprintf(stderr, "D3DXCompileShader\n"); fprintf(stderr, "D3DXCompileShader\n");
@ -371,9 +380,14 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine
res = g_pd3dDevice->CreateVertexShader((DWORD*)pCode->GetBufferPointer(), &g_pVertexShader); res = g_pd3dDevice->CreateVertexShader((DWORD*)pCode->GetBufferPointer(), &g_pVertexShader);
pCode->Release(); pCode->Release();
if(FAILED(res)) { if (FAILED(res)) {
fprintf(stderr, "CreateVertexShader\n"); fprintf(stderr, "CreateVertexShader\n");
return 0;
_com_error err(res);
LPCTSTR errMsg = err.ErrorMessage();
fprintf(stderr, "errMsg: %ls\n", errMsg);
fflush(stderr);
return res;
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
@ -388,7 +402,7 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine
dwShaderFlags, // Flags dwShaderFlags, // Flags
&pCode, // ppShader &pCode, // ppShader
NULL, // ppErrorMsgs NULL, // ppErrorMsgs
&g_pConstantTable // ppConstantTable &g_pPixelConstantTable // ppConstantTable
); );
if (FAILED(res)) { if (FAILED(res)) {
fprintf(stderr, "D3DXCompileShader\n"); fprintf(stderr, "D3DXCompileShader\n");
@ -396,12 +410,58 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine
} }
res = g_pd3dDevice->CreatePixelShader((DWORD*)pCode->GetBufferPointer(), &g_pPixelShader); res = g_pd3dDevice->CreatePixelShader((DWORD*)pCode->GetBufferPointer(), &g_pPixelShader);
pCode->Release(); pCode->Release();
if(FAILED(res)) { if (FAILED(res)) {
fprintf(stderr, "CreatePixelShader\n"); fprintf(stderr, "CreatePixelShader\n");
return 0;
_com_error err(res);
LPCTSTR errMsg = err.ErrorMessage();
fprintf(stderr, "errMsg: %ls\n", errMsg);
fflush(stderr);
return res;
} }
// //////////////////////////////////////////////////////////////////////
// texture
//////////////////////////////////////////////////////////////////////
res = g_pd3dDevice->CreateTexture(256,
256,
1,
0,
D3DFMT_A8R8G8B8,
D3DPOOL_MANAGED,
&g_pTexture,
NULL
);
if (FAILED(res)) {
fprintf(stderr, "CreateTexture\n");
return res;
}
D3DLOCKED_RECT rect;
res = g_pTexture->LockRect(0,
&rect,
NULL,
0);
if (FAILED(res)) {
fprintf(stderr, "LockRect\n");
return res;
}
fprintf(stderr, "pitch %d\n", rect.Pitch);
FILE * fp = fopen("bear_a8r8g8b8.data", "rb");
assert(fp != NULL);
fread(rect.pBits, 1, 256 * 256 * 4, fp);
fclose(fp);
res = g_pTexture->UnlockRect(0);
if (FAILED(res)) {
fprintf(stderr, "UnlockRect\n");
return res;
}
//////////////////////////////////////////////////////////////////////
fprintf(stderr, "success\n"); fprintf(stderr, "success\n");
fflush(stderr); fflush(stderr);

View File

@ -1,9 +1,16 @@
sampler2D BearTextureSampler;
struct VS_OUTPUT struct VS_OUTPUT
{ {
float4 Position : POSITION; // vertex position float4 Position : POSITION; // vertex position
float4 Diffuse : COLOR0; // vertex diffuse color float4 Diffuse : COLOR0; // vertex diffuse color
float2 Texture : TEXCOORD0;
}; };
float4 Main(VS_OUTPUT In) : COLOR { float4 Main(VS_OUTPUT In) : COLOR {
return float4(1, 1, 1, 1) - In.Diffuse; //return float4(1, 1, 1, 1) - In.Diffuse;
//return float4(In.Texture, 0, 1);
float2 tex = In.Texture * 0.5 + 0.5;
return tex2D(BearTextureSampler, float2(tex.x, 1.0 - tex.y));
} }

View File

@ -8,6 +8,7 @@ struct VS_OUTPUT
{ {
float4 Position : POSITION; // vertex position float4 Position : POSITION; // vertex position
float4 Diffuse : COLOR0; // vertex diffuse color float4 Diffuse : COLOR0; // vertex diffuse color
float2 Texture : TEXCOORD0;
}; };
float4x4 mWorldViewProj; float4x4 mWorldViewProj;
@ -19,6 +20,7 @@ VS_OUTPUT Main(VS_INPUT Input)
//Output.Position = mul(float4(Input.Position, 1.0), mWorldViewProj); //Output.Position = mul(float4(Input.Position, 1.0), mWorldViewProj);
Output.Position = float4(Input.Position, 1.0); Output.Position = float4(Input.Position, 1.0);
Output.Diffuse = Input.Color; Output.Diffuse = Input.Color;
Output.Texture = Input.Position.xy;
return Output; return Output;
} }