Entries for tag "rendering", ordered from most recent. Entry count: 187.
# Half-Pixel Offset in DirectX 11
Fri
09
Nov 2012
There is a problem graphics programmers often have to face known as half-pixel/half-texel offset. In Direct3D 9 the probably most official article about it was Directly Mapping Texels to Pixels. In Direct3D 10/11 they changed the way it works so it can be said that the problem is gone now. But I can see the entry about SV_Position in the Semantics article does not explain it clearly, so here is my short explanation.
A pixel on a screen or texel on a texture can be seen as a matrix cell, visualized as square filled with some color. That's the way we treat it in 2D graphics where we index pixels from left-top corner using integer (x, y) coordinates.
![]()
But in 3D graphics, texture can be sampled using floating-point coordinates and interpolation between texel colors can be performed. Texture coordinates in DirectX also starts from left-top corner, but position (0, 0) means the very corner of the texture, NOT the center of the first texel! Similarly, position (1, 1) on texture means its bottom-right corner. So to get exactly the color of the second texel of 8 x 8 texture, we have to sample with coordinates (1.5 / 8, 0.5 / 8).
![]()
Now if we rendered 3D scene onto a texture to perform e.g. deferred shading or some other screen-space postprocessing and we want to redraw it to the target back buffer, how do we determine coordinates for sampling this texture based on position of rendered pixel? There is a system value semantics available at pixel shader input called SV_Position which gives (x, y) pixel position. It is expressed in pixels, so it goes to e.g. (1920, 1080) and not to (1, 1) like texture coordinates. But it turns out that in Direct3D 10/11, the value of SV_Target for first pixels on the screen is not (0, 0), (1, 0), but (0.5, 0.5), (1.5, 0.5)!
![]()
Therefore to sample texture based on pixel position, it's enought to divide it by screen resolution. No need to perform any half-pixel offset - to add or subtract (0.5, 0.5), like it was done in Direct3D 9.
Texture2D g_Texture : register(t0);
SamplerState g_SamplerState : register(s0);
cbuffer : register(b0) {
float2 g_ScreenResolution;
};
void PixelShader(float2 Position : SV_Position, out float4 Color : SV_Target)
{
Color = g_Texture.Sample(g_SamplerState, Position / g_ScreenResolution);
}
Comments | #directx #rendering Share
# Flat Shading in DirectX 11
Fri
02
Nov 2012
I code a terrain heightmap rendering and today I wanted to make it looking like in Darwinia. After preparing approporiate texture it turned out that normals of my mesh used for lighting are smoothed. That's not a suprise - after all there is a single vertex in every particular place, shared by surrounding triangles, refered multiple times from index buffer.
How to make triangles flat shaded? Make every vertex unique to its triangle to have different normal? In Direct3D 9 there was this state used by fixed function pipeline you could set to device->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT); What about Direct3D 10/11?
A simple solution is to use interpolation modifiers. Fields of structure passed from vertex shader to pixel shader can have these modifiers to tell GPU how to interpolate (or not to interpolate) particular value on the surface of a triangle. nointerpolation means the value will be taken from one of vertices. That solves the problem.
struct VS_TO_PS
{
float4 Pos : SV_Position;
nointerpolation float3 Normal : NORMAL;
float2 TexCoord : TEXCOORD0;
};
Comments | #rendering #directx Share
# Developing Graphics Driver
Thu
12
Jul 2012
Want to know what do I do at Intel? Obviously all details are secret, but generally, as a Graphics Software Engineer, I code graphics driver for our GPU. What does this software do? When you write a game these days, you usually use some game engine, but I'm sure you know that on a lower level, everything ends up as a bunch of textured 3D triangles rendered with hardware acceleration by the GPU. To render them, the engine uses one of standard graphics APIs. On Windows it can be DirectX or OpenGL, on Linux and Mac it is OpenGL, on mobile platforms it is OpenGL ES. On the other side, there are many hardware manufacturers - like NVIDIA, AMD, Intel or Imagination Technologies - that make discrete or embedded GPUs. These chips have different capabilities and instruction sets. So graphics driver is needed to translate calls to API (like IDirect3DDevice9::DrawIndexedPrimitive) and shader code to form specific to the hardware.
Want to know more? Intel recently published documentation of the GPU from the new Ivy Bridge processor - see this news. You can find this documentation on intellinuxgraphics.org website. It consists of more than 2000 pages in 17 PDF files. For example, in the last volume (Volume 4 Part 3) you can see how instructions of our programmable execution units look like. They are quite powerful :)
Comments | #intel #driver #rendering #hardware Share
# 4k Executable Graphics with Fractal
Fri
15
Jun 2012
There was a competition announced on forum.warsztat.gd in making a 4k executable graphics. In case you don't know what is it: It is a category well known on demoscene where you have to write a program - Windows executable in assembler, C, C++, Java, HTML or any other language, that fits into 4 KB size limit and generates a static, nice looking image. Obviously there is no way to store a bitmap inside such size, so the graphics must be generated procedurally. For list best prods in this category, see this search query on pouet.net.
I participated in this competition and here is my entry with source code: gfx_4k_Reg.zip. EXE file itself has 1259 bytes. Archive has password "demoscene" (needed because server of my hosting company rejects archives containing unusual EXE files like this, thinking it is a virus :)
Not very impressive, but keep in mind that I coded it in about 2-3 hours, with little previous experience in this kind of coding. If you are interested in how such program is made, I used:
Comments | #rendering #demoscene Share
# Direct2D Class Diagram
Sat
19
May 2012
I've made class diagram for Direct2D library. Enjoy :)
By the way, yEd is a free editor for all kinds of graphs, including UML diagrams like this. It may not allow you to enter list of fields and methods in classes like StarUML or Visio do. It may not allow to freely draw shapes like Inkscape or OpenOffice Draw do. But for creating, editing and arranging graphs of different kinds of nodes, interconnected with different kinds of links, it is really great. I especially like the way it snaps all positions and sizes to the neighbour nodes. Commands for automatic arranging of nodes, called Layout, also look impressive. Plus it supports different file formats, including XML-based, so you can easily generate such file and then open it in yEd to visualize some data.
Comments | #yed #graphs #rendering #direct2d Share
# Direct2D - Unboxing
Mon
07
May 2012
Just as some bloggers record unboxing of products, I'd like to share my first impressions from playing around with Direct2D library. What is it? As you can guess from its name, it is a library from Microsoft for efficient, hardware-accelerated drawing of immediate-mode 2D graphics. It is successor of the old GDI, as well as GDI+. It is native library, object-oriented, based on COM. It works on Windows 7, as well as Windows Vista (after installing SP2 and Platform Update) and Windows 2008. The API seems modern, clean and very elegant, much like Direct3D 10/11. But of course it could be just a matter of taste. You can find its documentation, as well as headers and libraries in DirectX SDK. Docs say something about Windows 7 SDK, so probably it's there too.
What can it interop with? You can create a render target that will send the graphics to either a window (using HWND), a GDI canvas (HDC), DXGI surface (interop with Direct3D 10/11) or WIC Bitmap. Interop with GDI is also possible in the opposite way - you can obtain a HDC from D2D render target. Microsoft advices using WIC (Windows Imaging Component) for loading bitmap files. Another important library that cooperates with Direct2D is DirectWrite, which enables text rendering.
I didn't find any good tutorial about Direct2D, but I haven't look for it very hard either. I think the official documentation is good enough teaching all aspects of the library. After reviewing the documentation, I'm very impressed not only by the looks of the API, but also by the list of features is supports, like:
Things I don't like so far about the library:
My first test, drawn using following code:
using namespace D2D1; const D2D1_SIZE_F rtSize = m_RenderTarget->GetSize(); m_RenderTarget->BeginDraw(); m_RenderTarget->Clear(ColorF(ColorF::White)); m_Brush->SetColor(ColorF(ColorF::Silver)); const float gridStep = 24.f; for(float x = gridStep; x < rtSize.width; x += gridStep) m_RenderTarget->DrawLine(Point2F(x, 0.f), Point2F(x, rtSize.height), m_Brush, 1.f); for(float y = gridStep; y < rtSize.height; y += gridStep) m_RenderTarget->DrawLine(Point2F(0.f, y), Point2F(rtSize.width, y), m_Brush, 1.f); m_Brush->SetColor(ColorF(ColorF::Navy)); m_RenderTarget->DrawRectangle(RectF(200.f, 400.f, 400.f, 550.f), m_Brush); m_RenderTarget->DrawRectangle(RectF(240.f, 450.f, 280.f, 500.f), m_Brush); m_RenderTarget->DrawRectangle(RectF(320.f, 450.f, 360.f, 550.f), m_Brush)); m_RenderTarget->DrawLine(Point2F(200.f, 400.f), Point2F(300.f, 300.f), m_Brush, 1.f); m_RenderTarget->DrawLine(Point2F(300.f, 300.f), Point2F(400.f, 400.f), m_Brush, 1.f); m_RenderTarget->EndDraw();
Comments | #rendering #direct2d Share
# Easiest Way to Generate a Bitmap
Wed
19
Oct 2011
I wanted to visualize results of some mathematical computations so I wondered what is the easiest way to generate and then show or save a bitmap? I considered Scilab, PHP with GD library, C++ with WinAPI GDI, C++ with some library that have image file formats support like FreeImage... Finally I've came up with the following solution, using only C++ and WinAPI to fill appropriate data structures and save the image as 24-bit BMP file.
// Width and height of the bitmap
unsigned size_x = 1280, size_y = 720;
// Rows top-down,
// for each row pixels from left to right,
// for each pixel components B, G, R = 0..255.
unsigned data_size = size_x * size_y * 3;
unsigned char *data = new unsigned char[data_size];
// Here fill the data, for example:
for (unsigned y = 0, i = 0; y < size_y; ++y) {
for (unsigned x = 0; x < size_x; ++x) {
data[i++] = 255; // G
data[i++] = 255; // B
data[i++] = 255; // R
}
}
// Prepare data structures
DWORD data_offset = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
DWORD file_size = data_offset + data_size;
BITMAPFILEHEADER file_header = {
0x4D42, // bfType = "BM"
file_size, // bfSize
0, 0, // bfReserved1, bfReserved2
data_offset, // bfOffBits
};
BITMAPINFOHEADER info_header = {
sizeof(BITMAPINFOHEADER), // biSize
(LONG)size_x, -(LONG)size_y, // biWidth, biHeight
1, // biPlanes
24, // biBitCount
BI_RGB, // biCompression
0, // biSizeImage
72, 72, // biXPelsPerMeter, biYPelsPerMeter
0, 0, // biClrUsed, biClrImportant
};
// Save file
FILE *file = fopen("file_name.bmp", "wb");
fwrite(&file_header, sizeof(file_header), 1, file);
fwrite(&info_header, sizeof(info_header), 1, file);
fwrite(&data[0], 1, data_size, file);
fclose(file);
// Finalize
delete [] data;
Comments | #rendering #winapi #c++ Share
# Social Graph - Application on Facebook
Mon
11
Jul 2011
If you are active Facebook user and you like data visualisation just like me, then I strongly recommend you look at Social Graph application. When you run in, a Flash applet draws circles representing all your friends and connections between them. You are not there because, well, these are your friends so you are connected to all of them. You can only see how are they connected to each other. What's most interesting though is that they form some groups, which are clearly depicted thanks to graph visualisation algorithm used in this app. For example, my graph looks like this:

Several persons are not connected to anyone other I know. They are drawn somewhere outside the graph in a far distance. These are people who I know from some unusual place or who do not participate actively in Facebook and do not collect friends.
Rest of them forms clearly distinctive groups. The group on the right are people from my home town - mostly my friends from high school or university. The biggest group in the middle are passionate game developers from warsztat.gd community. Small group at the bottom consisting of four circles are contacts from my current job. On the left of Warsztat there is a group of professional game developers from Polish game industry. And finally the smaller group on the left consists of people I know from psytrance parties :)
What's also interesting is that there are some people who play role of a "center" of some group - are connected with almost everyone else in that group. Others are "connectors" that join two groups.