drawprim.c revision 554f00fe75489f3f3ce7fbb6d126ce1d2c5c922c
/*
* WINED3D draw functions
*
* Copyright 2002-2004 Jason Edmeades
* Copyright 2002-2004 Raphael Junqueira
* Copyright 2004 Christian Costa
* Copyright 2005 Oliver Stieber
* Copyright 2006, 2008 Henri Verbeet
* Copyright 2007-2008 Stefan Dösinger for CodeWeavers
*
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/*
* Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
* other than GPL or LGPL is available it will apply instead, Sun elects to use only
* the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
* a choice of LGPL license versions is made available with the language indicating
* that LGPLv2 or any later version may be used, or where a choice of which version
* of the LGPL is applied is otherwise unspecified.
*/
#include "config.h"
#include "wined3d_private.h"
#include <stdio.h>
#include <math.h>
/* GL locking is done by the caller */
{
if (idx_size)
{
#if 1
checkGLcall("glDrawElements");
#else
checkGLcall("glDrawRangeElements");
#endif
}
else
{
checkGLcall("glDrawArrays");
}
}
/*
* Actually draw using the supplied information.
* Slower GL version which extracts info about each vertex in turn
*/
/* GL locking is done by the caller */
static void drawStridedSlow(IWineD3DDevice *iface, const struct wined3d_stream_info *si, UINT NumVertexes,
{
unsigned int textureNo = 0;
const struct wined3d_stream_info_element *element;
TRACE("Using slow vertex array code\n");
/* Variable Initialization */
if (idxSize != 0) {
/* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
* If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
* idxData will be != NULL
*/
}
} else if (idxData) {
ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
return;
}
/* Start drawing in GL */
else glNormal3f(0, 0, 0);
if (This->activeContext->num_untracked_materials && element->format_desc->format != WINED3DFMT_A8R8G8B8)
{
/* special case where the fog density is stored in the specular alpha channel */
{
if (GL_SUPPORT(EXT_FOG_COORD))
{
}
else
{
if (!warned)
{
/* TODO: Use the fog table code from old ddraw */
FIXME("Implement fog for transformed vertices in software\n");
}
}
}
}
else if (GL_SUPPORT(EXT_SECONDARY_COLOR))
{
GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
}
{
{
FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
continue;
}
if (texture_idx == -1) continue;
if (coordIdx > 7)
{
continue;
}
else if (coordIdx < 0)
{
continue;
}
{
}
else
{
if (GL_SUPPORT(ARB_MULTITEXTURE))
else
glTexCoord4f(0, 0, 0, 1);
}
}
/* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
* Guess it's not necessary(we crash then anyway) and would only eat CPU time
*/
/* For each primitive */
/* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
* function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
*/
/* For indexed data, we need to go a few more strides in */
/* Indexed so work out the number of strides to skip */
if (idxSize == 2) {
} else {
}
}
{
int coord_idx;
const void *ptr;
int texture_idx;
if (!(tmp_tex_mask & 1)) continue;
ptr = texCoords[coord_idx] + (SkipnStrides * si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].stride);
}
/* Diffuse -------------------------------- */
if (diffuse) {
unsigned char i;
float color[4];
}
}
}
/* Specular ------------------------------- */
if (specular) {
if (specular_fog)
{
}
}
/* Normal -------------------------------- */
}
/* Position -------------------------------- */
if (position) {
}
/* For non indexed mode, step onto next parts */
++SkipnStrides;
}
}
glEnd();
checkGLcall("glEnd and previous calls");
}
/* GL locking is done by the caller */
static inline void send_attribute(IWineD3DDeviceImpl *This, WINED3DFORMAT format, const UINT index, const void *ptr)
{
switch(format)
{
case WINED3DFMT_R32_FLOAT:
break;
case WINED3DFMT_R32G32_FLOAT:
break;
break;
break;
case WINED3DFMT_R8G8B8A8_UINT:
break;
case WINED3DFMT_A8R8G8B8:
{
break;
}
/* else fallthrough */
break;
case WINED3DFMT_R16G16_SINT:
break;
break;
case WINED3DFMT_R16G16_SNORM:
{
break;
}
case WINED3DFMT_R16G16_UNORM:
{
break;
}
break;
break;
FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
/*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
break;
FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
/*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
break;
case WINED3DFMT_R16G16_FLOAT:
/* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
* byte float according to the IEEE standard
*/
if (GL_SUPPORT(NV_HALF_FLOAT)) {
/* Not supported by GL_ARB_half_float_vertex */
} else {
float x = float_16_to_32(((const unsigned short *)ptr) + 0);
}
break;
if (GL_SUPPORT(NV_HALF_FLOAT)) {
/* Not supported by GL_ARB_half_float_vertex */
} else {
float x = float_16_to_32(((const unsigned short *)ptr) + 0);
}
break;
default:
break;
}
}
/* GL locking is done by the caller */
static void drawStridedSlowVs(IWineD3DDevice *iface, const struct wined3d_stream_info *si, UINT numberOfVertices,
{
int i;
if (idxSize != 0) {
/* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
* If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
* idxData will be != NULL
*/
}
} else if (idxData) {
ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
return;
}
/* Start drawing in GL */
/* Indexed so work out the number of strides to skip */
if (idxSize == 2) {
} else {
}
}
for(i = MAX_ATTRIBS - 1; i >= 0; i--) {
}
SkipnStrides++;
}
glEnd();
}
/* GL locking is done by the caller */
static inline void drawStridedInstanced(IWineD3DDevice *iface, const struct wined3d_stream_info *si,
{
UINT numInstances = 0, i;
int numInstancedAttribs = 0, j;
if (idxSize == 0) {
/* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
* We don't support this for now
*
* Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
* But the StreamSourceFreq value has a different meaning in that situation.
*/
FIXME("Non-indexed instanced drawing is not supported\n");
return;
}
/* First, figure out how many instances we have to draw */
for(i = 0; i < MAX_STREAMS; i++) {
/* Look at the streams and take the first one which matches */
if(((stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INSTANCEDATA) || (stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INDEXEDDATA)) && stateblock->streamSource[i]) {
/* D3D9 could set streamFreq 0 with (INSTANCEDATA or INDEXEDDATA) and then it is handled as 1. See d3d9/tests/visual.c-> stream_test() */
if(stateblock->streamFreq[i] == 0){
numInstances = 1;
} else {
numInstances = stateblock->streamFreq[i]; /* use the specified number of instances from the first matched stream. See d3d9/tests/visual.c-> stream_test() */
}
break; /* break, because only the first suitable value is interesting */
}
}
{
{
}
}
/* now draw numInstances instances :-) */
for(i = 0; i < numInstances; i++) {
/* Specify the instanced attributes using immediate mode calls */
for(j = 0; j < numInstancedAttribs; j++) {
{
struct wined3d_buffer *vb =
}
}
glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
checkGLcall("glDrawElements");
}
}
{
unsigned int i;
{
struct wined3d_stream_info_element *e = &s->elements[i];
if (e->buffer_object)
{
e->buffer_object = 0;
}
}
}
/* Routine common to the draw primitive and draw indexed primitive routines */
{
unsigned int i;
if (!index_count) return;
/* Invalidate the back buffer memory so LockRect will read it the next time */
if (target) {
}
}
/* Signals other modules that a drawing is in progress and the stateblock finalized */
if (This->stencilBufferTarget) {
/* Note that this depends on the ActivateContext call above to set
* This->render_offscreen properly */
}
/* Ok, we will be updating the screen from here onwards so grab the lock */
ENTER_GL();
{
struct wined3d_stream_info stridedlcl;
{
{
if (!warned) {
FIXME("Using software emulation because not all material properties could be tracked\n");
} else {
TRACE("Using software emulation because not all material properties could be tracked\n");
}
}
/* Either write a pipeline replacement shader or convert the specular alpha from unsigned byte
* to a float in the vertex buffer
*/
if (!warned) {
FIXME("Using software emulation because manual fog coordinates are provided\n");
} else {
TRACE("Using software emulation because manual fog coordinates are provided\n");
}
}
if(emulation) {
}
}
/* Immediate mode drawing */
{
if (!warned) {
FIXME("Using immediate mode with vertex shaders for half float emulation\n");
} else {
TRACE("Using immediate mode with vertex shaders for half float emulation\n");
}
drawStridedSlowVs(iface, stream_info, index_count, glPrimType, idxData, idxSize, minIndex, StartIdx);
} else {
}
} else if(This->instancedDraw) {
/* Instancing emulation with mixing immediate mode and arrays */
} else {
}
}
/* Finished updating the screen, restore lock */
LEAVE_GL();
TRACE("Done all gl drawing\n");
/* Diagnostics */
#ifdef SHOW_FRAME_MAKEUP
{
static long int primCounter = 0;
/* NOTE: set primCounter to the value reported by drawprim
before you want to to write frame makeup to /tmp */
if (primCounter >= 0) {
char buffer[80];
#ifdef SHOW_TEXTURE_MAKEUP
{
int textureNo;
sprintf(buffer, "/tmp/texture_%p_%ld_%d.tga", This->stateBlock->textures[textureNo], primCounter, textureNo);
} else {
FIXME("base Texture isn't of type texture %d\n", IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]));
}
}
}
}
#endif
}
++primCounter;
}
#endif
/* Control goes back to the device, stateblock values may change again */
}
static void normalize_normal(float *n) {
if(length == 0.0) return;
n[0] = n[0] / length;
}
/* Tesselates a high order rectangular patch into single triangles using gl evaluators
*
* The problem is that OpenGL does not offer a direct way to return the tesselated primitives,
* and they can't be sent off for rendering directly either. Tesselating is slow, so we want
* to cache the patches in a vertex buffer. But more importantly, gl can't bind generated
* attributes to numbered shader attributes, so we have to store them and rebind them as needed
* in drawprim.
*
* To read back, the opengl feedback mode is used. This creates a problem because we want
* untransformed, unlit vertices, but feedback runs everything through transform and lighting.
* Thus disable lighting and set identity matrices to get unmodified colors and positions.
* To overcome clipping find the biggest x, y and z values of the vertices in the patch and scale
* them to [-1.0;+1.0] and set the viewport up to scale them back.
*
* Normals are more tricky: Draw white vertices with 3 directional lights, and calculate the
* resulting colors back to the normals.
*
* NOTE: This function activates a context for blitting, modifies matrices & viewport, but
* does not restore it because normally a draw follows immediately afterwards. The caller is
* responsible of taking care that either the gl states are restored, or the context activated
* for drawing to reset the lastWasBlit flag.
*/
struct WineD3DRectPatch *patch) {
struct wined3d_stream_info stream_info;
struct wined3d_stream_info_element *e;
/* First, locate the position data. This is provided in a vertex buffer in the stateblock.
* Beware of vbos
*/
if (e->buffer_object)
{
struct wined3d_buffer *vb;
}
/* Not entirely sure about what happens with transformed vertices */
/* glMap2f reads vertex sizes in GLfloats, the d3d stride is in bytes.
* I don't see how the stride could not be a multiple of 4, but make sure
* to check it
*/
ERR("Vertex stride is not a multiple of sizeof(GLfloat)\n");
}
}
}
/* First, get the boundary cube of the input data */
}
}
/* This needs some improvements in the vertex decl code */
FIXME("Cannot find data to generate. Only generating position and normals\n");
/* Simply activate the context for blitting. This disables all the things we don't want and
* takes care of dirtifying. Dirtifying is preferred over pushing / popping, since drawing the
* patch (as opposed to normal draws) will most likely need different changes anyway
*/
ENTER_GL();
checkGLcall("glMatrixMode(GL_PROJECTION)");
checkGLcall("glLoadIndentity()");
glTranslatef(0, 0, 0.5);
checkGLcall("glScalef");
checkGLcall("glViewport");
/* Some states to take care of. If we're in wireframe opengl will produce lines, and confuse
* our feedback buffer parser
*/
checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)");
if(patch->has_normals) {
checkGLcall("glEnable(GL_LIGHTING)");
checkGLcall("glLightModel for MODEL_AMBIENT");
checkGLcall("glDisable(GL_LIGHT0 + i)");
}
checkGLcall("Setting up light 1\n");
checkGLcall("Setting up light 2\n");
checkGLcall("Setting up light 3\n");
checkGLcall("Setting up materials\n");
}
/* Enable the needed maps.
* GL_MAP2_VERTEX_3 is needed for positional data.
* GL_AUTO_NORMAL to generate normals from the position. Do not use GL_MAP2_NORMAL.
* GL_MAP2_TEXTURE_COORD_4 for texture coords
*/
d3d_out_vertex_size = 3;
FIXME("Texcoords not handled yet\n");
out_vertex_size += 8;
d3d_out_vertex_size += 7;
} else if(patch->has_texcoords) {
FIXME("Texcoords not handled yet\n");
out_vertex_size += 7;
d3d_out_vertex_size += 4;
} else if(patch->has_normals) {
out_vertex_size += 4;
d3d_out_vertex_size += 3;
} else {
}
checkGLcall("glEnable vertex attrib generation");
checkGLcall("glMap2f");
if(patch->has_texcoords) {
checkGLcall("glMap2f");
}
checkGLcall("glMapGrid2f");
checkGLcall("glFeedbackBuffer");
checkGLcall("glEvalMesh2\n");
i = glRenderMode(GL_RENDER);
if(i == -1) {
LEAVE_GL();
return WINED3DERR_DRIVERINTERNALERROR;
} else if(i != buffer_size) {
LEAVE_GL();
return WINED3DERR_DRIVERINTERNALERROR;
} else {
TRACE("Got %d elements as expected\n", i);
}
patch->mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_quads * 6 * d3d_out_vertex_size * sizeof(float) * 8);
i = 0;
if(feedbuffer[j] != GL_POLYGON_TOKEN) {
continue;
}
continue;
}
/* Somehow there are different ideas about back / front facing, so fix up the
* vertex order
*/
patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 2 + 4] - 0.5) * 4 * max_z; /* z, triangle 3 */
if(patch->has_normals) {
}
i += d3d_out_vertex_size;
patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 1 + 4] - 0.5) * 4 * max_z; /* z, triangle 2 */
if(patch->has_normals) {
}
i += d3d_out_vertex_size;
patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 0 + 4] - 0.5) * 4 * max_z; /* z, triangle 1 */
if(patch->has_normals) {
}
i += d3d_out_vertex_size;
}
if(patch->has_normals) {
/* Now do the same with reverse light directions */
static const GLfloat x[] = {-1, 0, 0, 0};
static const GLfloat y[] = { 0, -1, 0, 0};
static const GLfloat z[] = { 0, 0, -1, 0};
checkGLcall("Setting up reverse light directions\n");
checkGLcall("glRenderMode(GL_FEEDBACK)");
checkGLcall("glEvalMesh2\n");
i = glRenderMode(GL_RENDER);
checkGLcall("glRenderMode(GL_RENDER)");
i = 0;
if(feedbuffer[j] != GL_POLYGON_TOKEN) {
continue;
}
continue;
}
i += d3d_out_vertex_size;
i += d3d_out_vertex_size;
i += d3d_out_vertex_size;
}
}
checkGLcall("glDisable vertex attrib generation");
LEAVE_GL();
vtxStride = 3 * sizeof(float);
if(patch->has_normals) {
vtxStride += 3 * sizeof(float);
}
if(patch->has_texcoords) {
vtxStride += 4 * sizeof(float);
}
if(patch->has_normals) {
}
if(patch->has_texcoords) {
if(patch->has_normals) {
}
}
return WINED3D_OK;
}