• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
lipsryme

Calculating mip level in software rasterizer

4 posts in this topic

After a day of searching for answers I've found out that there seem to be 2 ways of doing this.

 

1. Calculate some kind of texel area of the triangle and divide this by the triangle area. (What exactly is meant by "texel area" and how do I calculate it using my texCoords[0,1] ?)

 

2. This: (GLSL code)

float mipmapLevel(vec2 uv, vec2 textureSize)
{
  //rate of change of the pixels in u and v with respect to window space
  //approximate to au/ax, au/ay, av/ax, av/ay
  vec2 dx = dFdx( uv * textureSize.x);
  vec2 dy = dFdy( uv * textureSize.y);
  
  //select the LOD based on the maximum compression of an edge in texture space.
  //This corresponds to the maximum length of a side in texture space
  //max (sqrt(dUdx*dUdx + dVdx*dVdx),
  //    sqrt(dUdy*dUdy + dVdy*dVdy));
  float d = max( dot (dx, dx), dot( dy, dy));
  
  //convert d length to power-of-two level of detail
  return 0.5*log2(d);
}

I've been trying to get my head around these partial derivatives the entire day but I still don't quite get it.

The best I've come up with was something that "kind of" works but I'm sure it's not entirely correct:

 

I calculate the change of the texCoords between the 2x2 rasterized/shaded area:

float ddx = abs(uv_X.m128_f32[1] - uv_X.m128_f32[0]);
float ddy = abs(uv_Y.m128_f32[2] - uv_Y.m128_f32[1]);

And then get the mip level like so:

ddx *= texSizeX;
ddy *= texSizeY;

Vector4_SSE vec0 = Vector4_SSE(ddx, ddy, 0, 0);
float d = 0.5f * log2f(Vec4_SSE_Dot(vec0, vec0));
return std::max<unsigned int>(std::min<unsigned int>(static_cast<int>(d), 8), 0);

What I just don't understand is why the formular says au/ax, av/ax, au/ay, av/ay. Isn't the rate of change in window coordinates always going to be exactly 1 pixel ?? Doesn't this make the ax and ay variables completely void ? Or I'm just not getting it tongue.png so please enlighten me rolleyes.gif

Edited by lipsryme
0

Share this post


Link to post
Share on other sites

The rate of change in window coordinates is not always 1 pixel per texel. It looks like that's the only thing tripping you up, so I'll just give you two extreme examples.

 

1 texel = 1 pixel of texture. If you have a quad consuming 100x100 pixels on your screen and it has an 8192x8192 texture, it takes ~6700 texels per pixel. On the other end, if you have a quad consuming your entire 1920x1080 display and a 256x256 texture, each texel is representing ~32 pixels. Obviously, the two would use different mipmap levels of detail.

0

Share this post


Link to post
Share on other sites

Hmm okay but what you're talking about is the rate of the texel's change in window coordinates which is the result of the above forumlar, right ?

What I don't get is what ax and ay relates to in my code.

If au and av is the rate of change from one pixel to the neighboring (UV[x+1, y]-UV[x, y] and UV[x, y+1]-UV[x,y]), then ax and ay would be the rate of change of the window coordinate. But wouldn't this always be 1 from one pixel to the neighboring ?

 

edit: or does the equation only mean that it is au in relationship to ax ? And so can be ignored in the code (the divide) ? Still..something is wrong in my code.

Edited by lipsryme
0

Share this post


Link to post
Share on other sites

I think you're just getting tripped up from looking at it backwards. au/ax is what you are not doing. The above is getting the number of texels between pixels (sort of the formula with ax=1, ay=1). So, you are absolutely right about ax and ay being 1. The ax and ay would need to be different values if you were applying the formula somewhere else. For example, if you were applying it to an entire triangle from the 3 vertices, you'd have to calculate how many pixels wide the triangle is for ax and how many texels wide for au.

 

 

It looks like your code is ignoring dFdx(v*textureSize.x) and dFdy(u*textureSize.y) or dVdx and dUdy. That might be a source of your bug. You didn't elaborate too much, and I didn't look too closely.
 

GPUs will probably work on a 2x2 block, so dFdx(p) called at 0,0 is going to return p(0,0) - p(1,0) and dFdx(p) for 1,0 is going to return p(1,0) - p(0,0). I mention this only for clarity and completeness since it can easily be the source of bugs.

1

Share this post


Link to post
Share on other sites

What I'm doing is calculating for the entire 2x2 quad: abs(p(1,0) - p(0,0)) and abs(p(0,1) - p(0,0)) since I thought the rate of change is an absolute value that should be the same for all x and y in the 2x2 quad. Is that correct ?

 

Edit: I assume the part I'm missing is only for non-uniform textures ? (e.g. 128x64 instead of 128x128)

If I understand correct I also need to multiply the derivatives of X and Y both by the texWidth and both by texHeight and then do the max(dot(xx,xx), dot(yy, yy)) right ?  

 

I think I've got it working now happy.png

Edited by lipsryme
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0