commit 28657e3e063ff7637f7a31d2a08c71ac62c0d729
parent 6593372d8d8c03052d6110d05fa0044939541a0c
Author: Brian Swetland <swetland@frotz.net>
Date: Fri, 1 Feb 2013 02:00:10 -0800
hello: tidy up, no more effects
Diffstat:
3 files changed, 80 insertions(+), 47 deletions(-)
diff --git a/hello/SimplePS.hlsl b/hello/SimplePS.hlsl
@@ -0,0 +1,17 @@
+
+Texture2D Texture0;
+
+SamplerState samLinear {
+ Filter = MIN_MAG_MIP_LINEAR;
+ AddressU = Wrap;
+ AddressV = Wrap;
+};
+
+struct VS_OUTPUT {
+ float4 Position : SV_POSITION;
+ float2 TexCoord : TEXCOORD;
+};
+
+float4 main(VS_OUTPUT input) : SV_Target {
+ return Texture0.Sample(samLinear, input.TexCoord);
+}
diff --git a/hello/SimpleVS.hlsl b/hello/SimpleVS.hlsl
@@ -0,0 +1,20 @@
+
+cbuffer cb0 {
+ matrix MVP;
+}
+
+struct VS_OUTPUT {
+ float4 Position : SV_POSITION;
+ float2 TexCoord : TEXCOORD;
+};
+
+VS_OUTPUT main(
+ float4 Position : POSITION,
+ float4 Normal : NORMAL,
+ float2 TexCoord : TEXCOORD) {
+ VS_OUTPUT output = (VS_OUTPUT) 0;
+// output.Position = mul(Position, MVP); // DX
+ output.Position = mul(MVP, Position); // GL
+ output.TexCoord = TexCoord;
+ return output;
+}
diff --git a/hello/hello.cc b/hello/hello.cc
@@ -29,79 +29,84 @@ static D3D10_INPUT_ELEMENT_DESC obj_layout[] = {
class TestApp : public App {
public:
TestApp();
- ~TestApp();
int init(void);
void render(void);
+ void release(void);
private:
float t;
DWORD timeStart;
- ID3D10Effect *effect;
- ID3D10EffectTechnique *technique;
ID3D10InputLayout *layout;
ID3D10Buffer *vtxbuf;
ID3D10Buffer *idxbuf;
+ ID3D10Buffer *cbuf;
- ID3D10EffectMatrixVariable *uMVP;
- ID3D10EffectShaderResourceVariable *uShader0;
+ ID3D10PixelShader *PS;
+ ID3D10VertexShader *VS;
+ ID3D10Blob *PSbc;
+ ID3D10Blob *VSbc;
ID3D10ShaderResourceView *rvShader0;
mat4 proj;
struct model *m;
- void *texdata;
- unsigned tw, th;
};
-TestApp::TestApp() : App(), t(0.0f), timeStart(0), vtxbuf(NULL), idxbuf(NULL), layout(NULL), effect(NULL) {
+TestApp::TestApp() : App(), t(0.0f), timeStart(0),
+ layout(NULL), vtxbuf(NULL), idxbuf(NULL), cbuf(NULL),
+ PS(NULL), VS(NULL), PSbc(NULL), VSbc(NULL), rvShader0(NULL) {
}
-TestApp::~TestApp() {
+void TestApp::release(void) {
+ if (PS) PS->Release();
+ if (VS) VS->Release();
if (rvShader0) rvShader0->Release();
+ if (layout) layout->Release();
if (vtxbuf) vtxbuf->Release();
if (idxbuf) idxbuf->Release();
- if (layout) layout->Release();
- if (effect) effect->Release();
+ if (cbuf) cbuf->Release();
}
int TestApp::init(void) {
HRESULT hr;
+ void *data;
+ unsigned dsz, dw, dh;
- if (!(texdata = load_png_rgba("cube-texture.png", &tw, &th, 0)))
- return error("cannot load texture");
- if (!(m = load_wavefront_obj("cube.obj")))
- return error("cannot load model");
-
- hr = D3DX10CreateEffectFromFile("hello.fx", NULL, NULL, "fx_4_0",
- D3D10_SHADER_ENABLE_STRICTNESS | SHADER_DEBUG_FLAGS,
- 0, device, NULL, NULL, &effect, NULL, NULL );
-
- if (FAILED(hr))
- return error("cannot load effect '%s'", "hello.fx");
-
- technique = effect->GetTechniqueByName("Render");
- uMVP = effect->GetVariableByName("MVP")->AsMatrix();
- uShader0 = effect->GetVariableByName("Texture0")->AsShaderResource();
+ if (compilePixelShader("SimplePS.hlsl", &PS, &PSbc))
+ return -1;
+ if (compileVertexShader("SimpleVS.hlsl", &VS, &VSbc))
+ return -1;
- D3D10_PASS_DESC pass;
- technique->GetPassByIndex(0)->GetDesc(&pass);
- hr = device->CreateInputLayout(obj_layout, sizeof(obj_layout)/sizeof(obj_layout[0]),
- pass.pIAInputSignature, pass.IAInputSignatureSize, &layout);
+ hr = device->CreateInputLayout(obj_layout,
+ sizeof(obj_layout) / sizeof(obj_layout[0]),
+ VSbc->GetBufferPointer(), VSbc->GetBufferSize(), &layout);
if (FAILED(hr))
return error("create input layout failed 0x%08x", hr);
+ device->VSSetShader(VS);
+ device->PSSetShader(PS);
+
+ if (!(m = load_wavefront_obj("cube.obj")))
+ return error("cannot load model");
if (createVtxBuffer(m->vdata, 32 * m->vcount, &vtxbuf))
return -1;
if (createIdxBuffer(m->idx, sizeof(short) * m->icount, &idxbuf))
return -1;
- if (createTextureRGBA(texdata, tw, th, 1, &rvShader0))
+
+ if (!(data = load_png_rgba("cube-texture.png", &dw, &dh, 0)))
+ return error("cannot load texture");
+ if (createTextureRGBA(data, dw, dh, 1, &rvShader0))
return -1;
+ free(data);
- uShader0->SetResource(rvShader0);
+ if (createConstantBuffer(16 * 4, &cbuf))
+ return -1;
+ device->VSSetConstantBuffers(0, 1, &cbuf);
+ device->PSSetShaderResources(0, 1, &rvShader0);
- proj.setPerspective(D3DX_PI * 0.5, width / (float) height, 0.1f, 100.0f);
+ proj.setPerspective(M_PI * 0.5, width / (float) height, 0.1f, 100.0f);
return 0;
}
@@ -124,28 +129,19 @@ void TestApp::render(void) {
device->IASetVertexBuffers(0, 1, &vtxbuf, &stride, &offset);
device->IASetIndexBuffer(idxbuf, DXGI_FORMAT_R16_UINT, 0);
device->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
- D3D10_TECHNIQUE_DESC td;
- technique->GetDesc(&td);
- mat4 world, view, mvp;
+ mat4 world, view, mvp, tmp;
view.identity().rotateX(D2R(25)).translate(0, 0, -10);
-
world.identity().rotateY(t);
mvp = world * view * proj;
- uMVP->SetMatrix((float*) mvp.data());
- for (unsigned p = 0; p < td.Passes; ++p) {
- technique->GetPassByIndex(p)->Apply(0);
- device->DrawIndexed(m->icount, 0, 0 );
- }
+ device->UpdateSubresource(cbuf, 0, NULL, mvp.data(), 0, 0);
+ device->DrawIndexed(m->icount, 0, 0);
world.identity().rotateY(t).translate(-5,0,0).rotateY(t);
mvp = world * view * proj;
- uMVP->SetMatrix((float*) mvp.data());
- for (unsigned p = 0; p < td.Passes; ++p) {
- technique->GetPassByIndex(p)->Apply(0);
- device->DrawIndexed(m->icount, 0, 0 );
- }
+ device->UpdateSubresource(cbuf, 0, NULL, mvp.data(), 0, 0);
+ device->DrawIndexed(m->icount, 0, 0);
swap->Present(0, 0);
}