bool MultipleRenderTargetsWindow::CreateScene() { // Create a visual effect that populates the draw target. std::string filename = mEnvironment.GetPath("MultipleRenderTargets.hlsl"); std::shared_ptr<VisualProgram> program = mProgramFactory.CreateFromFiles(filename, filename, ""); if (!program) { return false; } std::shared_ptr<ConstantBuffer> cbuffer(new ConstantBuffer( sizeof(Matrix4x4<float>), true)); program->GetVShader()->Set("PVWMatrix", cbuffer); std::shared_ptr<PixelShader> pshader = program->GetPShader(); std::shared_ptr<ConstantBuffer> farNearRatio(new ConstantBuffer( sizeof(float), false)); pshader->Set("FarNearRatio", farNearRatio); farNearRatio->SetMember("farNearRatio", mCamera->GetDMax() / mCamera->GetDMin()); std::string path = mEnvironment.GetPath("StoneWall.png"); std::shared_ptr<Texture2> baseTexture(WICFileIO::Load(path, true)); baseTexture->AutogenerateMipmaps(); pshader->Set("baseTexture", baseTexture); std::shared_ptr<SamplerState> baseSampler(new SamplerState()); baseSampler->filter = SamplerState::MIN_L_MAG_L_MIP_L; baseSampler->mode[0] = SamplerState::CLAMP; baseSampler->mode[1] = SamplerState::CLAMP; pshader->Set("baseSampler", baseSampler); std::shared_ptr<VisualEffect> effect = std::make_shared<VisualEffect>(program); // Create a vertex buffer for a two-triangle square. The PNG is stored // in left-handed coordinates. The texture coordinates are chosen to // reflect the texture in the y-direction. struct Vertex { Vector3<float> position; Vector2<float> tcoord; }; VertexFormat vformat; vformat.Bind(VA_POSITION, DF_R32G32B32_FLOAT, 0); vformat.Bind(VA_TEXCOORD, DF_R32G32_FLOAT, 0); std::shared_ptr<VertexBuffer> vbuffer(new VertexBuffer(vformat, 4)); Vertex* vertex = vbuffer->Get<Vertex>(); vertex[0].position = { -1.0f, -1.0f, 0.0f }; vertex[0].tcoord = { 0.0f, 1.0f }; vertex[1].position = { 1.0f, -1.0f, 0.0f }; vertex[1].tcoord = { 1.0f, 1.0f }; vertex[2].position = { -1.0f, 1.0f, 0.0f }; vertex[2].tcoord = { 0.0f, 0.0f }; vertex[3].position = { 1.0f, 1.0f, 0.0f }; vertex[3].tcoord = { 1.0f, 0.0f }; // Create an indexless buffer for a triangle mesh with two triangles. std::shared_ptr<IndexBuffer> ibuffer(new IndexBuffer(IP_TRISTRIP, 2)); // Create the geometric object for drawing and enable automatic updates // of pvw-matrices and w-matrices. mSquare = std::make_shared<Visual>(vbuffer, ibuffer, effect); mTrackball.Attach(mSquare); mTrackball.Update(); mCameraRig.Subscribe(mSquare->worldTransform, cbuffer); return true; }
//---------------------------------------------------------------------------- bool StructuredBuffersWindow::CreateScene() { // Create the shaders and associated resources HLSLDefiner definer; definer.SetInt("WINDOW_WIDTH", mXSize); std::shared_ptr<VertexShader> vshader(ShaderFactory::CreateVertex( mEnvironment.GetPath("StructuredBuffers.hlsl"), definer)); if (!vshader) { return false; } std::shared_ptr<PixelShader> pshader(ShaderFactory::CreatePixel( mEnvironment.GetPath("StructuredBuffers.hlsl"), definer)); if (!pshader) { return false; } std::shared_ptr<ConstantBuffer> cbuffer(new ConstantBuffer( sizeof(Matrix4x4<float>), true)); vshader->Set("PVWMatrix", cbuffer); // Create the pixel shader and associated resources. std::string path = mEnvironment.GetPath("StoneWall.png"); std::shared_ptr<Texture2> baseTexture(WICFileIO::Load(path, false)); pshader->Set("baseTexture", baseTexture); std::shared_ptr<SamplerState> baseSampler(new SamplerState()); baseSampler->filter = SamplerState::MIN_L_MAG_L_MIP_P; baseSampler->mode[0] = SamplerState::CLAMP; baseSampler->mode[1] = SamplerState::CLAMP; pshader->Set("baseSampler", baseSampler); mDrawnPixels.reset(new StructuredBuffer(mXSize*mYSize, sizeof(Vector4<float>))); mDrawnPixels->SetUsage(Resource::SHADER_OUTPUT); mDrawnPixels->SetCopyType(Resource::COPY_BIDIRECTIONAL); memset(mDrawnPixels->GetData(), 0, mDrawnPixels->GetNumBytes()); pshader->Set("drawnPixels", mDrawnPixels); // Create the visual effect for the square. std::shared_ptr<VisualEffect> effect(new VisualEffect(vshader, pshader)); // Create a vertex buffer for a single triangle. The PNG is stored in // left-handed coordinates. The texture coordinates are chosen to reflect // the texture in the y-direction. struct Vertex { Vector3<float> position; Vector2<float> tcoord; }; VertexFormat vformat; vformat.Bind(VA_POSITION, DF_R32G32B32_FLOAT, 0); vformat.Bind(VA_TEXCOORD, DF_R32G32_FLOAT, 0); std::shared_ptr<VertexBuffer> vbuffer(new VertexBuffer(vformat, 4)); Vertex* vertex = vbuffer->Get<Vertex>(); vertex[0].position = Vector3<float>(0.0f, 0.0f, 0.0f); vertex[0].tcoord = Vector2<float>(0.0f, 1.0f); vertex[1].position = Vector3<float>(1.0f, 0.0f, 0.0f); vertex[1].tcoord = Vector2<float>(1.0f, 1.0f); vertex[2].position = Vector3<float>(0.0f, 1.0f, 0.0f); vertex[2].tcoord = Vector2<float>(0.0f, 0.0f); vertex[3].position = Vector3<float>(1.0f, 1.0f, 0.0f); vertex[3].tcoord = Vector2<float>(1.0f, 0.0f); // Create an indexless buffer for a triangle mesh with two triangles. std::shared_ptr<IndexBuffer> ibuffer(new IndexBuffer(IP_TRISTRIP, 2)); // Create the geometric object for drawing. Translate it so that its // center of mass is at the origin. This supports virtual trackball // motion about the object "center". mSquare.reset(new Visual(vbuffer, ibuffer, effect)); mSquare->localTransform.SetTranslation(-0.5f, -0.5f, 0.0f); mSquare->Update(); // Enable automatic updates of pvw-matrices and w-matrices. SubscribeCW(mSquare, cbuffer); // The structured buffer is written in the pixel shader. This texture // will receive a copy of it so that we can write the results to disk // as a PNG file. mDrawnPixelsTexture = new Texture2(DF_R8G8B8A8_UNORM, mXSize, mYSize); return true; }