graphics

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 4d19d48ce6c9963daad450af412d6729b254d886
parent f9bcb22704650b9a4098e6f14e8948042b6e34d6
Author: Brian Swetland <swetland@frotz.net>
Date:   Tue,  5 Feb 2013 20:18:11 -0800

dxapp/hello: abstract away more d3disms

Diffstat:
Mcommon/app.h | 43++++++++++++++++++++++++++++++++++++++++---
Mcommon/dxapp.cc | 22++++++++++++++++------
Mcommon/textgrid.cc | 14+++++++-------
Mcommon/textgrid.h | 4++--
Mhello/hello.cc | 23++++++++---------------
5 files changed, 73 insertions(+), 33 deletions(-)

diff --git a/common/app.h b/common/app.h @@ -32,6 +32,38 @@ #define SHADER_DEBUG_FLAGS 0 #endif +enum { + FMT_32x4_FLOAT = DXGI_FORMAT_R32G32B32A32_FLOAT, + FMT_32x3_FLOAT = DXGI_FORMAT_R32G32B32_FLOAT, + FMT_32x2_FLOAT = DXGI_FORMAT_R32G32_FLOAT, + FMT_32x1_FLOAT = DXGI_FORMAT_R32_FLOAT, + + FMT_8x4_SNORM = DXGI_FORMAT_R8G8B8A8_SNORM, + + FMT_8x4_UNORM = DXGI_FORMAT_R8G8B8A8_UNORM, + FMT_8x2_UNORM = DXGI_FORMAT_R8G8_UNORM, + FMT_8x1_UNORM = DXGI_FORMAT_R8_UNORM, + + FMT_8x4_UINT = DXGI_FORMAT_R8G8B8A8_UINT, + FMT_8x2_UINT = DXGI_FORMAT_R8G8_UINT, + FMT_8x1_UINT = DXGI_FORMAT_R8_UINT, +}; + +enum { + VERTEX_DATA = D3D10_INPUT_PER_VERTEX_DATA, + INSTANCE_DATA = D3D10_INPUT_PER_INSTANCE_DATA, +}; + +struct AttribInfo { + const char *name; + unsigned nidx; + unsigned format; + unsigned slot; + unsigned offset; + unsigned type; + unsigned divisor; +}; + struct PixelShader { ID3D10PixelShader *ps; PixelShader() : ps(NULL) {}; @@ -92,11 +124,11 @@ public: // TODO: move away from D3D10_INPUT... int compileShader(VertexShader *vs, const char *fn, void *data, unsigned len, int raw, - D3D10_INPUT_ELEMENT_DESC *layout, unsigned lcount); + AttribInfo *layout, unsigned lcount); int compileShader(PixelShader *ps, const char *fn, void *data, unsigned len, int raw); int loadShader(VertexShader *vs, const char *fn, - D3D10_INPUT_ELEMENT_DESC *layout, unsigned lcount); + AttribInfo *layout, unsigned lcount); int loadShader(PixelShader *ps, const char *fn); int loadTextureRGBA(Texture2D *tex, const char *fn, int genmips); @@ -133,7 +165,12 @@ public: void useTexture(Texture2D *tex, int slot) { device->PSSetShaderResources(slot, 1, &tex->srv); } - + void drawIndexedInstanced(unsigned numindices, unsigned numinstances) { + device->DrawIndexedInstanced(numindices, numinstances, 0, 0, 0); + } + void drawInstanced(unsigned numvertices, unsigned numinstances) { + device->DrawInstanced(numvertices, numinstances, 0, 0); + } protected: int width; int height; diff --git a/common/dxapp.cc b/common/dxapp.cc @@ -149,7 +149,14 @@ void App::eventloop(void) { ((state.rgbButtons[1] >> 6) & 2) | ((state.rgbButtons[2] >> 5) & 4); } - app->render(); + + float rgba[4] = { 0.0f, 0.125f, 0.3f, 1.0f }; + device->ClearRenderTargetView(targetView, rgba); + device->ClearDepthStencilView(depthView, D3D10_CLEAR_DEPTH, 1.0f, 0 ); + + app->render(); + + swapchain->Present(1, 0); frame++; } } @@ -302,6 +309,8 @@ int App::reconfigure(int init) { vp.TopLeftY = 0; device->RSSetViewports(1, &vp); + device->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + device->OMSetRenderTargets(1, &targetView, depthView); return 0; @@ -399,7 +408,7 @@ static int _compile_shader(const char *fn, void *data, unsigned sz, int App::compileShader(VertexShader *_vs, const char *fn, void *data, unsigned len, int raw, - D3D10_INPUT_ELEMENT_DESC *desc, unsigned dcount) { + AttribInfo *desc, unsigned dcount) { ID3D10VertexShader *vs = NULL; ID3D10InputLayout *layout = NULL; ID3D10Blob *bin = NULL; @@ -409,7 +418,8 @@ int App::compileShader(VertexShader *_vs, const char *fn, hr = device->CreateVertexShader(data, len, &vs); if (FAILED(hr)) return error("failed to create shader '%s' 0x%08x", fn, hr); - hr = device->CreateInputLayout(desc, dcount, data, len, &layout); + hr = device->CreateInputLayout((D3D10_INPUT_ELEMENT_DESC*) desc, + dcount, data, len, &layout); } else { if (_compile_shader(fn, data, len, "vs_4_0", &bin)) return -1; @@ -418,7 +428,7 @@ int App::compileShader(VertexShader *_vs, const char *fn, bin->Release(); return error("failed to create shader '%s' 0x%08x", fn, hr); } - hr = device->CreateInputLayout(desc, dcount, + hr = device->CreateInputLayout((D3D10_INPUT_ELEMENT_DESC*) desc, dcount, bin->GetBufferPointer(), bin->GetBufferSize(), &layout); bin->Release(); } @@ -430,7 +440,7 @@ int App::compileShader(VertexShader *_vs, const char *fn, _vs->layout->Release(); _vs->vs = vs; _vs->layout = layout; - _vs->desc = desc; + _vs->desc = (D3D10_INPUT_ELEMENT_DESC*) desc; _vs->dcount = dcount; return 0; } @@ -458,7 +468,7 @@ int App::compileShader(PixelShader *_ps, const char *fn, // TODO choose raw based on content or extension int App::loadShader(VertexShader *vs, const char *fn, - D3D10_INPUT_ELEMENT_DESC *layout, unsigned lcount) { + AttribInfo *layout, unsigned lcount) { void *data; unsigned sz; if (!(data = load_file(fn, &sz))) diff --git a/common/textgrid.cc b/common/textgrid.cc @@ -29,10 +29,10 @@ #include "TextPS.h" #endif -static D3D10_INPUT_ELEMENT_DESC text_layout_desc[] = { - { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, - { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D10_INPUT_PER_VERTEX_DATA, 0 }, - { "CHARACTER", 0, DXGI_FORMAT_R8_UINT, 1, 0, D3D10_INPUT_PER_INSTANCE_DATA, 1 }, +static AttribInfo text_layout_desc[] = { + { "POSITION", 0, FMT_32x2_FLOAT, 0, 0, VERTEX_DATA, 0 }, + { "TEXCOORD", 0, FMT_32x2_FLOAT, 0, 8, VERTEX_DATA, 0 }, + { "CHARACTER", 0, FMT_8x1_UINT, 1, 0, INSTANCE_DATA, 1 }, }; static float unit_box_2d[] = { @@ -44,7 +44,7 @@ static float unit_box_2d[] = { 0, 1, 0, 0, }; -int TextGrid::init(App *a, ID3D10Device *device, int w, int h) { +int TextGrid::init(App *a, int w, int h) { void *data; unsigned int dw, dh; HRESULT hr; @@ -98,7 +98,7 @@ int TextGrid::init(App *a, ID3D10Device *device, int w, int h) { return 0; } -void TextGrid::render(App *a, ID3D10Device *device) { +void TextGrid::render(App *a) { if (dirty) { dirty = 0; a->updateBuffer(&cbuf, grid); @@ -108,7 +108,7 @@ void TextGrid::render(App *a, ID3D10Device *device) { a->useTexture(&texture, 0); a->useBuffer(&vtx, 0, 16, 0); a->useBuffer(&cbuf, 1, 1, 0); - device->DrawInstanced(6, width * height, 0, 0); + a->drawInstanced(6, width * height); } void TextGrid::clear(void) { diff --git a/common/textgrid.h b/common/textgrid.h @@ -18,8 +18,8 @@ class TextGrid { public: - int init(App *a, ID3D10Device *device, int w, int h); - void render(App *a, ID3D10Device *device); + int init(App *a, int w, int h); + void render(App *a); void clear(void); void printf(int x, int y, const char *fmt, ...); diff --git a/hello/hello.cc b/hello/hello.cc @@ -21,11 +21,11 @@ #include <stdio.h> #include <stdlib.h> -static D3D10_INPUT_ELEMENT_DESC obj_layout[] = { - { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, - { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 }, - { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0 }, - { "LOCATION", 0, DXGI_FORMAT_R8G8B8A8_SNORM, 1, 0, D3D10_INPUT_PER_INSTANCE_DATA, 1 }, +static AttribInfo obj_layout[] = { + { "POSITION", 0, FMT_32x3_FLOAT, 0, 0, VERTEX_DATA, 0 }, + { "NORMAL", 0, FMT_32x3_FLOAT, 0, 12, VERTEX_DATA, 0 }, + { "TEXCOORD", 0, FMT_32x2_FLOAT, 0, 24, VERTEX_DATA, 0 }, + { "LOCATION", 0, FMT_8x4_SNORM, 1, 0, INSTANCE_DATA, 1 }, }; static float locationx[] = { @@ -133,7 +133,7 @@ int TestApp::init(void) { build(); zoom = SZ; - if (text.init(this, device, 64, 64)) + if (text.init(this, 64, 64)) return -1; return 0; @@ -176,17 +176,12 @@ oops: if (update) build(); - float rgba[4] = { 0.0f, 0.125f, 0.3f, 1.0f }; - device->ClearRenderTargetView(targetView, rgba); - device->ClearDepthStencilView(depthView, D3D10_CLEAR_DEPTH, 1.0f, 0 ); - useShaders(&ps, &vs); useBuffer(&ubuf, 0); useBuffer(&vbuf, 0, 32, 0); useBuffer(&lbuf, 1, 4, 0); useBuffer(&ibuf); - device->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST); struct { mat4 mvp; mat4 mv; @@ -200,16 +195,14 @@ oops: cb0.mvp = world * view * proj; cb0.mv = world * view; updateBuffer(&ubuf, &cb0); - device->DrawIndexedInstanced(m->icount, lcount, 0, 0, 0); + drawIndexedInstanced(m->icount, lcount); text.clear(); text.printf(0, 0, "rx: %8.4f", rx); text.printf(0, 1, "ry: %8.4f", ry); text.printf(0, 2, "zm: %8.4f", zoom); text.printf(0, -1, "hello.cc"); - text.render(this, device); - - swapchain->Present(1, 0); + text.render(this); } App *createApp(void) {